svgwriter 0.1.1

Typed SVG Writer
Documentation
use crate::xmlwriter::XmlWriter;
use std::borrow::Cow;

pub trait Tag {
	fn write_to(&self, w: &mut XmlWriter, pretty: bool);
}

impl<T> Tag for Box<T>
where
	T: ?Sized + Tag
{
	fn write_to(&self, w: &mut XmlWriter, pretty: bool) {
		self.as_ref().write_to(w, pretty)
	}
}

macro_rules! impl_as_ref_str {
	($($ty:ty),*) => {
		$(impl Tag for $ty {
			fn write_to(&self, w: &mut XmlWriter, _pretty: bool) {
				let s: &str = self.as_ref();
				w.write_text(s);
			}
		})*
	};
}

impl_as_ref_str!(String, str, &'static str, Cow<'static, str>);

pub trait Container<Child> {
	fn push(&mut self, content: Child);

	#[inline]
	fn append(mut self, content: Child) -> Self
	where
		Self: Sized
	{
		self.push(content);
		self
	}
}

#[cfg(feature = "raw")]
#[derive(Debug)]
pub struct RawXml(String);

#[cfg(feature = "raw")]
impl RawXml {
	/// Creates raw XML from a string. The XML content is not checked. It will be written
	/// as-is into the XML document. Use with caution.
	pub fn new_unchecked<T: Into<String>>(xml: T) -> Self {
		Self(xml.into())
	}
}

#[cfg(feature = "raw")]
impl Tag for RawXml {
	fn write_to(&self, w: &mut XmlWriter, _pretty: bool) {
		w.write_raw_xml_unchecked(&self.0);
	}
}