inline-xml-macros 0.2.0

Macros for the inline-xml crate
Documentation
use std::fmt::{self, Display, Formatter};
use proc_macro2::Span;
use super::*;

impl SimpleNameSeg {
    fn span(&self) -> Span {
        match self {
            Self::Ident(i)  => i.span(),
            Self::Minus(m)  => m.span,
        }
    }
}

impl SimpleName {
    fn span(&self) -> Span {
        let mut iter = self.0.iter();
        let mut span = iter.next().unwrap().span();
        while let Some(e) = iter.next() {
            span = span.join(e.span()).unwrap();
        }
        span
    }
}

impl Name {
    pub fn span(&self) -> Span {
        self.namespace
            .as_ref()
            .map_or_else(
                || self.name.span(),
                |(i, _)| self.name.span().join(i.span()).unwrap()
            )
    }
}

impl PartialEq for SimpleNameSeg {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Ident(a), Self::Ident(b))    => a == b,
            (Self::Minus(_), Self::Minus(_))    => true,
            _                                   => false,
        }
    }
}

impl PartialEq for Name {
    fn eq(&self, other: &Self) -> bool {
        let ns1 = self.namespace.as_ref().map(|(i, _)| i);
        let ns2 = other.namespace.as_ref().map(|(i, _)| i);
        ns1 == ns2 && self.name == other.name
    }
}

impl Display for SimpleNameSeg {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ident(i)  => write!(f, "{i}"),
            Self::Minus(_)  => write!(f, "-"),
        }
    }
}

impl Display for SimpleName {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.0.iter().try_for_each(|e| write!(f, "{e}"))
    }
}

impl Display for Name {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some((ns, _)) = &self.namespace {
            write!(f, "{}:{}", ns, self.name)
        } else {
            write!(f, "{}", self.name)
        }
    }
}