Skip to content
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

Implement Render trait for Option #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions sailfish/src/runtime/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,26 @@ impl<T: Render> Render for Wrapping<T> {
}
}

impl<T: Render> Render for Option<T> {
#[inline]
fn render(&self, b: &mut Buffer) -> Result<(), RenderError> {
if let Some(inner) = self {
inner.render(b)
} else {
Ok(())
}
}

#[inline]
fn render_escaped(&self, b: &mut Buffer) -> Result<(), RenderError> {
if let Some(inner) = self {
inner.render_escaped(b)
} else {
Ok(())
}
}
}

/// The error type which is returned from template function
#[derive(Clone, Debug)]
pub enum RenderError {
Expand Down Expand Up @@ -521,6 +541,18 @@ mod tests {
assert_eq!(b.as_str(), "10-20");
}

#[test]
fn test_option() {
let mut b = Buffer::new();
Render::render(&Some("apple"), &mut b).unwrap();
Render::render_escaped(&Some("apple"), &mut b).unwrap();

Render::render(&Option::<&str>::None, &mut b).unwrap();
Render::render_escaped(&Option::<&str>::None, &mut b).unwrap();

assert_eq!(b.as_str(), "appleapple");
}

#[test]
fn render_error() {
let err = RenderError::new("custom error");
Expand Down