Skip to content

Commit

Permalink
let Element implement IntoIterator<Item=Self>
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Nov 3, 2024
1 parent 256b0a7 commit cb47699
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
14 changes: 1 addition & 13 deletions src/elt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! It is also possible to inline raw HTML with [`raw`] and [`raw_unsafe`]
use alloc::{borrow::Cow, string::String, vec::Vec};
use alloc::{borrow::Cow, string::String};

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

Expand Down Expand Up @@ -383,15 +383,3 @@ impl From<String> for Element {
text(value)
}
}

impl<const N: usize> From<[Element; N]> for Element {
fn from(value: [Element; N]) -> Self {
Vec::from(value).into()
}
}

impl From<Vec<Element>> for Element {
fn from(value: Vec<Element>) -> Self {
Self(ElementInner::Multiple(value))
}
}
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ fn assert_valid_attribute_name(name: &str) {
);
}

impl IntoIterator for Element {
type Item = Self;
type IntoIter = core::iter::Once<Self>;
fn into_iter(self) -> Self::IntoIter {
core::iter::once(self)
}
}

impl<const N: usize> From<[Element; N]> for Element {
fn from(value: [Element; N]) -> Self {
Vec::from(value).into()
}
}

impl From<Vec<Element>> for Element {
fn from(value: Vec<Element>) -> Self {
Self(ElementInner::Multiple(value))
}
}

/// Create an HTML [`Document`]
///
/// You must pass the [`elt::head`] and [`elt::body`] element as you would with any other element.
Expand Down
1 change: 1 addition & 0 deletions tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn should_render_attribute(#[case] attr: Attribute, #[case] expected: &str) {
#[rstest]
#[case(none(), "")]
#[case([div([], []), div([], [])].into(), "<div></div><div></div>")]
#[case(div([], div([], [])), "<div><div></div></div>")]
#[case(text("hello"), "hello")]
#[case(text("hello".to_string()), "hello")]
#[case("hello".into(), "hello")]
Expand Down

0 comments on commit cb47699

Please sign in to comment.