Skip to content

Commit

Permalink
support none element
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Nov 3, 2024
1 parent 8e1948c commit b4c143b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ assert_eq!(greeting.to_string(), r#"<h1 class="bold">Hello world!</h1>"#);
Because those are simple rust functions, it is easy to leverage rust features like conditions, loops and iterators:

```rust
let list = ul([], (1..=3).map(|n| li([], [n.to_string().into()])));
assert_eq!(list.to_string(), "<ul><li>1</li><li>2</li><li>3</li></ul>");
let list_values = true;
let element = if list_values {
ul([], (1..=3).map(|n| li([], [n.to_string().into()])))
} else {
text("no value")
};

assert_eq!(element.to_string(), "<ul><li>1</li><li>2</li><li>3</li></ul>")
```


Expand Down
18 changes: 18 additions & 0 deletions src/elt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ use alloc::{borrow::Cow, string::String, vec::Vec};

use crate::{Attribute, Element, ElementInner};

/// Renders nothing. Useful fo conditional rendering.
///
/// # Example
///
/// ```
/// use fun_html::{Element, elt::{text, none}};
///
/// let say_hello = true;
/// let element: Element = if say_hello {
/// text("Hello")
/// } else {
/// none()
/// };
/// ```
pub fn none() -> Element {
Element(ElementInner::None)
}

/// `<div>`
pub fn div(
attributes: impl IntoIterator<Item = Attribute>,
Expand Down
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@
//! Because those are simple rust functions, it is easy to leverage rust features like conditions, loops and iterators:
//!
//! ```
//! # use fun_html::{elt::{li,ul}};
//! let list = ul([], (1..=3).map(|n| li([], [n.to_string().into()])));
//! assert_eq!(list.to_string(), "<ul><li>1</li><li>2</li><li>3</li></ul>");
//! # use fun_html::{elt::{li,ul,text}};
//!
//! let list_values = true;
//! let element = if list_values {
//! ul([], (1..=3).map(|n| li([], [n.to_string().into()])))
//! } else {
//! text("no value")
//! };
//!
//! assert_eq!(element.to_string(), "<ul><li>1</li><li>2</li><li>3</li></ul>")
//! ```
//!
//! ## Escape hatches
Expand Down Expand Up @@ -103,6 +110,7 @@ enum ElementInner {
Script(Cow<'static, str>),
Raw(Cow<'static, str>),
Multiple(Vec<Element>),
None,
}

/// An attribute
Expand Down Expand Up @@ -214,6 +222,7 @@ impl Display for Element {
write!(f, "{elt}")?;
}
}
ElementInner::None => (),
}
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ fn should_render_attribute(#[case] attr: Attribute, #[case] expected: &str) {
}

#[rstest]
#[case(none(), "")]
#[case([div([], []), div([], [])].into(), "<div></div><div></div>")]
#[case(text("hello"), "hello")]
#[case(text("hello".to_string()), "hello")]
#[case("hello".into(), "hello")]
Expand Down

0 comments on commit b4c143b

Please sign in to comment.