use hard_xml::{XmlRead, XmlWrite};
use std::borrow::Cow;
use crate::{__string_enum, __xml_test_suites};
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:t")]
pub struct Text<'a> {
#[xml(attr = "xml:space")]
pub space: Option<TextSpace>,
#[xml(text)]
pub text: Cow<'a, str>,
}
impl From<String> for Text<'_> {
fn from(val: String) -> Self {
Text {
text: val.into(),
space: None,
}
}
}
impl<'a> From<&'a str> for Text<'a> {
fn from(val: &'a str) -> Self {
Text {
text: val.into(),
space: None,
}
}
}
impl From<(String, TextSpace)> for Text<'_> {
fn from(val: (String, TextSpace)) -> Self {
Text {
text: val.0.into(),
space: Some(val.1),
}
}
}
impl<'a> From<(&'a str, TextSpace)> for Text<'a> {
fn from(val: (&'a str, TextSpace)) -> Self {
Text {
text: val.0.into(),
space: Some(val.1),
}
}
}
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:delText")]
pub struct DelText<'a> {
#[xml(attr = "xml:space")]
pub space: Option<TextSpace>,
#[xml(text)]
pub text: Cow<'a, str>,
}
impl From<String> for DelText<'_> {
fn from(val: String) -> Self {
DelText {
text: val.into(),
space: None,
}
}
}
impl<'a> From<&'a str> for DelText<'a> {
fn from(val: &'a str) -> Self {
DelText {
text: val.into(),
space: None,
}
}
}
impl From<(String, TextSpace)> for DelText<'_> {
fn from(val: (String, TextSpace)) -> Self {
DelText {
text: val.0.into(),
space: Some(val.1),
}
}
}
impl<'a> From<(&'a str, TextSpace)> for DelText<'a> {
fn from(val: (&'a str, TextSpace)) -> Self {
DelText {
text: val.0.into(),
space: Some(val.1),
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum TextSpace {
Default,
Preserve,
}
__string_enum! {
TextSpace {
Default = "default",
Preserve = "preserve",
}
}
__xml_test_suites!(
Text,
Text::from("text"),
"<w:t>text</w:t>",
Text::from(String::from("text")),
"<w:t>text</w:t>",
Text::from(("text", TextSpace::Preserve)),
r#"<w:t xml:space="preserve">text</w:t>"#,
Text::from((String::from("text"), TextSpace::Default)),
r#"<w:t xml:space="default">text</w:t>"#,
);