~eanyanwu/toph

b9339d9f8c36abdc38457f29981e782c40832dfb — Eze 2 months ago 8a84311
Make more types implement Into<Node>
4 files changed, 32 insertions(+), 18 deletions(-)

M src/node.rs
M src/node/attribute.rs
M src/node/tag.rs
M src/node/visitor.rs
M src/node.rs => src/node.rs +25 -15
@@ 292,9 292,8 @@ impl Node {
    /// // An owned string
    /// span_.set(String::from("hello"));
    ///
    /// // These are equivalent
    /// // A span with nothing in it.
    /// span_;
    /// span_.set([]);
    ///
    /// // An array of nodes
    /// span_.set([div_, span_]);


@@ 306,7 305,12 @@ impl Node {
    ///     "bare string".into(),
    ///     span_,
    /// ]);
    ///
    /// // An option of anything that can be converted into a node
    /// span_.set(Some("hello"));
    /// ```
    ///
    /// Refer to the [trait implementations](#trait-implementations) for a comprehensive list
    pub fn set(mut self, child: impl Into<Node>) -> Node {
        if let Self::Element(ref mut el) = self {
            el.child = Some(Box::new(child.into()));


@@ 349,31 353,37 @@ impl From<String> for Node {
    }
}

impl From<Option<Node>> for Node {
    fn from(value: Option<Node>) -> Self {
        value.unwrap_or_default()
impl<I: Into<Node>> From<Option<I>> for Node {
    fn from(value: Option<I>) -> Self {
        value.map(|v| v.into()).unwrap_or_default()
    }
}

impl<I> From<Vec<I>> for Node
where
    I: Into<Node>,
{
    fn from(value: Vec<I>) -> Self {
        let nodes = value.into_iter().map(|v| v.into()).collect::<Vec<_>>();
        Self::Fragment(Fragment(nodes))
    }
}

macro_rules! impl_node_for_array_of_nodes {
    ($($n:expr),+) => {
        $(
            impl From<[Node; $n]> for Node {
                fn from(value: [Node; $n]) -> Self {
                    Node::Fragment(Fragment(value.to_vec()))
            impl<I: Into<Node>> From<[I; $n]> for Node {
                fn from(value: [I; $n]) -> Self {
                    let nodes = value.into_iter().map(|v| v.into()).collect::<Vec<_>>();
                    Node::Fragment(Fragment(nodes))
                }
            }
        )+
    };
}

impl From<Vec<Node>> for Node {
    fn from(value: Vec<Node>) -> Self {
        Self::Fragment(Fragment(value))
    }
}
impl From<[Node; 0]> for Node {
    fn from(_value: [Node; 0]) -> Self {
impl<I: Into<Node>> From<[I; 0]> for Node {
    fn from(_value: [I; 0]) -> Self {
        Self::Text(Text("".into()))
    }
}

M src/node/attribute.rs => src/node/attribute.rs +1 -1
@@ 72,7 72,7 @@ impl AttributeMap {
            self.boolean.insert(key);
        } else {
            let value = if URL_ATTRIBUTES.contains(&key) {
                encode::url(&value)
                encode::url(value)
            } else {
                Some(value.into())
            };

M src/node/tag.rs => src/node/tag.rs +4 -0
@@ 65,6 65,10 @@ pub(crate) const script_: Node = Node::Element(Element {
    variables: CSSVariableMap::new(),
});

/// An empty Node that does not render as anything
#[allow(non_upper_case_globals)]
pub const empty_: Node = Node::Text(Text(String::new()));

#[allow(non_upper_case_globals)]
pub(crate) const style_: Node = Node::Element(Element {
    tag: "style",

M src/node/visitor.rs => src/node/visitor.rs +2 -2
@@ 174,7 174,7 @@ impl<W: fmt::Write> NodeVisitor for HtmlStringWriter<W> {
        if !el.variables.is_empty() {
            match el.attributes.entry("style") {
                Entry::Vacant(v) => {
                    v.insert(el.variables.to_string().into());
                    v.insert(el.variables.to_string());
                }
                Entry::Occupied(mut o) => {
                    let existing = o.get_mut();


@@ 235,7 235,7 @@ impl<W: io::Write> NodeVisitor for HtmlWriter<W> {
        if !el.variables.is_empty() {
            match el.attributes.entry("style") {
                Entry::Vacant(v) => {
                    v.insert(el.variables.to_string().into());
                    v.insert(el.variables.to_string());
                }
                Entry::Occupied(mut o) => {
                    let existing = o.get_mut();