-
-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented display_some and display_some_or #1014
base: main
Are you sure you want to change the base?
Conversation
} | ||
} | ||
|
||
pub fn display_some<T>(value: &Option<T>) -> Result<DisplaySome<'_, T>> |
There was a problem hiding this comment.
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?
|
||
[#display_some]: #display_some | ||
|
||
The `display_some` filter is essentially a shorthand for: |
There was a problem hiding this comment.
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".
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))
} |
Resolves #1007
Questions:
#[doc(hidden)]
DisplaySome
andDisplaySomeOr
?#[doc(hidden)]
that?)json.rs
|tojson
and few more filters faster #1008Alternatively, we could also turn
DisplaySome
private, and then use: