use std::borrow::Cow;
use xsd_parser::{
quick_xml::{DeserializeBytes, DeserializeReader, Error, SerializeBytes},
schema::Namespace,
};
pub const NS_XS: Namespace = Namespace::new_const(b"http://www.w3.org/2001/XMLSchema");
pub const NS_XML: Namespace = Namespace::new_const(b"http://www.w3.org/XML/1998/namespace");
pub const NS_TNS: Namespace = Namespace::new_const(b"http://example.com");
pub type Foo = FooType;
#[derive(Debug, Default)]
pub struct FooType(pub Vec<StringType>);
impl SerializeBytes for FooType {
fn serialize_bytes(&self) -> Result<Option<Cow<'_, str>>, Error> {
if self.0.is_empty() {
return Ok(None);
}
let mut data = String::new();
for item in &self.0 {
if let Some(bytes) = item.serialize_bytes()? {
if !data.is_empty() {
data.push(' ');
}
data.push_str(&bytes);
}
}
Ok(Some(Cow::Owned(data)))
}
}
impl DeserializeBytes for FooType {
fn deserialize_bytes<R>(reader: &R, bytes: &[u8]) -> Result<Self, Error>
where
R: DeserializeReader,
{
Ok(Self(
bytes
.split(|b| *b == b' ' || *b == b'|' || *b == b',' || *b == b';')
.map(|bytes| StringType::deserialize_bytes(reader, bytes))
.collect::<Result<Vec<_>, _>>()?,
))
}
}
pub type StringType = String;