Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Implemented display_some and display_some_or#1014

Open
vallentin wants to merge 3 commits intoaskama-rs:mainfrom
vallentin:display-some-filter
Open

Implemented display_some and display_some_or#1014
vallentin wants to merge 3 commits intoaskama-rs:mainfrom
vallentin:display-some-filter

Conversation

@vallentin
Copy link
Copy Markdown
Collaborator

@vallentin vallentin commented Apr 21, 2024

Resolves #1007

Questions:

  • Should we #[doc(hidden)] DisplaySome and DisplaySomeOr?
  • Should we move them into a separate module? (Maybe #[doc(hidden)] that?)
  • Maybe even expand it to also hide the JSON related types in json.rs

Alternatively, we could also turn DisplaySome private, and then use:

fn display_some(...) -> askama::Result<impl fmt::Display + 'a>

Comment thread askama/src/filters/mod.rs
}
}

pub fn display_some<T>(value: &Option<T>) -> Result<DisplaySome<'_, T>>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the filter functions before the types that need it.

Can we make DisplaySome and DisplaySomeOr private and yield impl Display from the filter functions?

Comment thread book/src/filters.md

[#display_some]: #display_some

The `display_some` filter is essentially a shorthand for:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we don't need the "a" before "shorthand".

@Kijewski
Copy link
Copy Markdown
Member

Kijewski commented Apr 28, 2024

I think we can omit the explicit lifetimes:

pub struct DisplaySome<T>(Option<T>);

impl<T: fmt::Display> fmt::Display for DisplaySome<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(val) = &self.0 {
            write!(f, "{val}")?;
        }
        Ok(())
    }
}

/// See [`display_some` in the Askama book] for more information.
///
/// See also [`display_some_or`].
///
/// [`display_some` in the Askama book]: https://djc.github.io/askama/filters.html#display_some
pub fn display_some<T: fmt::Display>(value: &Option<T>) -> Result<DisplaySome<&T>> {
    Ok(DisplaySome(value.as_ref()))
}

pub enum DisplaySomeOr<T, U> {
    Value(T),
    Otherwise(U),
}

impl<T: fmt::Display, U: fmt::Display> fmt::Display for DisplaySomeOr<T, U> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Value(val) => write!(f, "{val}"),
            Self::Otherwise(val) => write!(f, "{val}"),
        }
    }
}

/// See [`display_some_or` in the Askama book] for more information.
///
/// See also [`display_some`].
///
/// [`display_some_or` in the Askama book]: https://djc.github.io/askama/filters.html#display_some_or
pub fn display_some_or<T: fmt::Display, U: fmt::Display>(
    value: &Option<T>,
    otherwise: U,
) -> Result<DisplaySomeOr<&T, U>> {
    Ok(value
        .as_ref()
        .map_or(DisplaySomeOr::Otherwise(otherwise), DisplaySomeOr::Value))
}

@djc djc mentioned this pull request May 7, 2024
7 tasks
@Kijewski Kijewski force-pushed the main branch 2 times, most recently from 5633250 to 704f8f1 Compare March 11, 2025 19:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add display_some filter and friends

3 participants