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
Open
Implemented display_some and display_some_or#1014vallentin wants to merge 3 commits intoaskama-rs:mainfrom
vallentin wants to merge 3 commits intoaskama-rs:mainfrom
Conversation
djc
reviewed
Apr 22, 2024
| } | ||
| } | ||
|
|
||
| pub fn display_some<T>(value: &Option<T>) -> Result<DisplaySome<'_, T>> |
Collaborator
There was a problem hiding this comment.
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?
djc
reviewed
Apr 22, 2024
|
|
||
| [#display_some]: #display_some | ||
|
|
||
| The `display_some` filter is essentially a shorthand for: |
Collaborator
There was a problem hiding this comment.
Nit: I think we don't need the "a" before "shorthand".
Member
|
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))
} |
5633250 to
704f8f1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #1007
Questions:
#[doc(hidden)]DisplaySomeandDisplaySomeOr?#[doc(hidden)]that?)json.rs|tojsonand few more filters faster #1008Alternatively, we could also turn
DisplaySomeprivate, and then use: