1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
use std::fmt::{self, Display, Formatter};
use super::write_children;
use crate::Node;
/// A fragment.
///
/// ```html
/// <>
/// I'm in a fragment!
/// </>
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Fragment {
/// The children of the fragment.
///
/// ```html
/// <>
/// <!-- I'm a child! -->
/// <child>I'm another child!</child>
/// </>
pub children: Vec<Node>,
}
impl Fragment {
/// A fragment with no children.
pub const EMPTY: Self = Self {
children: Vec::new(),
};
}
impl Default for Fragment {
fn default() -> Self {
Self::EMPTY
}
}
impl Display for Fragment {
/// Format the fragment's childrent as HTML elements.
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write_children(f, &self.children, true)
}
}
impl<N> FromIterator<N> for Fragment
where
N: Into<Node>,
{
/// Create a new fragment from an iterator of anything that
/// can be converted into a [`crate::Node`].
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = N>,
{
Self {
children: iter.into_iter().map(Into::into).collect(),
}
}
}
impl<I, N> From<I> for Fragment
where
I: IntoIterator<Item = N>,
N: Into<Node>,
{
/// Create a new fragment from any iterator of anything that
/// can be converted into a [`crate::Node`].
fn from(iter: I) -> Self {
Self::from_iter(iter)
}
}