Skip to content

Commit 1ad833f

Browse files
committed
Added doctest for spreading option
1 parent 7875229 commit 1ad833f

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

crates/vertigo/src/fetch/lazy_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<T: PartialEq + Clone> LazyCache<T> {
264264
}
265265

266266
impl<T: JsJsonDeserialize> LazyCache<T> {
267-
/// Helper to easily create a lazy cache of Vec<T> deserialized from provided URL base and route
267+
/// Helper to easily create a lazy cache of `Vec<T>` deserialized from provided URL base and route
268268
///
269269
/// ```rust
270270
/// use vertigo::{Computed, LazyCache, RequestBuilder, AutoJsJson, Resource};

crates/vertigo/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ pub use log;
294294

295295
/// Allows to create [DomNode] using HTML tags.
296296
///
297+
/// Simple DOM with a param embedded:
298+
///
297299
/// ```rust
298300
/// use vertigo::dom;
299301
///
@@ -306,6 +308,24 @@ pub use log;
306308
/// </div>
307309
/// };
308310
/// ```
311+
///
312+
/// Mapping and embedding an `Option`:
313+
///
314+
/// ```rust
315+
/// use vertigo::dom;
316+
///
317+
/// let name = "John";
318+
/// let occupation = Some("Lumberjack");
319+
///
320+
/// dom! {
321+
/// <div>
322+
/// <h3>"Hello " {name} "!"</h3>
323+
/// {..occupation.map(|occupation| dom! { <p>"Occupation: " {occupation}</p> })}
324+
/// </div>
325+
/// };
326+
/// ```
327+
///
328+
/// Note the spread operator which utilizes the fact that `Option` is iterable in Rust.
309329
pub use vertigo_macro::dom;
310330

311331
/// Allows to create [DomElement] using HTML tags.

crates/vertigo/src/tests/dom/list_spread.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use crate::{self as vertigo, DomNode};
33

44
#[test]
55
fn children_from_iter() {
6-
let list = (0..10)
7-
.map(|i| dom! { <li>{i}</li> });
6+
let list = (0..10).map(|i| dom! { <li>{i}</li> });
87

98
let node = dom! {
109
<ul>
@@ -35,3 +34,22 @@ fn children_from_iter_inline() {
3534

3635
assert_eq!(node.get_children().len(), 11);
3736
}
37+
38+
#[test]
39+
fn iter_option() {
40+
let some_label = Some("Label".to_string());
41+
let none_label = Option::<String>::None;
42+
43+
let node = dom! {
44+
<div>
45+
{..some_label}
46+
{..none_label}
47+
</div>
48+
};
49+
50+
let DomNode::Node { node } = node else {
51+
panic!("Expected DomNode::Node")
52+
};
53+
54+
assert_eq!(node.get_children().len(), 1);
55+
}

0 commit comments

Comments
 (0)