use std::borrow::Cow;
use xsd_parser_types::{
misc::{Namespace, NamespacePrefix},
quick_xml::{
DeserializeBytes, DeserializeHelper, Error, SerializeBytes, SerializeHelper,
WithDeserializer, WithDeserializerFromBytes, WithSerializeToBytes, WithSerializer,
},
};
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_XSI: Namespace = Namespace::new_const(b"http://www.w3.org/2001/XMLSchema-instance");
pub const PREFIX_XS: NamespacePrefix = NamespacePrefix::new_const(b"xs");
pub const PREFIX_XML: NamespacePrefix = NamespacePrefix::new_const(b"xml");
pub const PREFIX_XSI: NamespacePrefix = NamespacePrefix::new_const(b"xsi");
#[derive(Debug, Default)]
pub struct EntitiesType(pub Vec<String>);
impl SerializeBytes for EntitiesType {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> 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(helper)? {
if !data.is_empty() {
data.push(' ');
}
data.push_str(&bytes);
}
}
Ok(Some(Cow::Owned(data)))
}
}
impl WithSerializeToBytes for EntitiesType {}
impl DeserializeBytes for EntitiesType {
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
Ok(Self(helper.deserialize_list(bytes)?))
}
}
impl WithDeserializerFromBytes for EntitiesType {}
pub type EntityType = String;
pub type IdType = String;
pub type IdrefType = String;
#[derive(Debug, Default)]
pub struct IdrefsType(pub Vec<String>);
impl SerializeBytes for IdrefsType {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> 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(helper)? {
if !data.is_empty() {
data.push(' ');
}
data.push_str(&bytes);
}
}
Ok(Some(Cow::Owned(data)))
}
}
impl WithSerializeToBytes for IdrefsType {}
impl DeserializeBytes for IdrefsType {
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
Ok(Self(helper.deserialize_list(bytes)?))
}
}
impl WithDeserializerFromBytes for IdrefsType {}
pub type NcNameType = String;
pub type NmtokenType = String;
#[derive(Debug, Default)]
pub struct NmtokensType(pub Vec<String>);
impl SerializeBytes for NmtokensType {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> 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(helper)? {
if !data.is_empty() {
data.push(' ');
}
data.push_str(&bytes);
}
}
Ok(Some(Cow::Owned(data)))
}
}
impl WithSerializeToBytes for NmtokensType {}
impl DeserializeBytes for NmtokensType {
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
Ok(Self(helper.deserialize_list(bytes)?))
}
}
impl WithDeserializerFromBytes for NmtokensType {}
pub type NotationType = String;
pub type NameType = String;
pub type QNameType = String;
pub type AnySimpleType = String;
#[derive(Debug)]
pub struct AnyType;
impl WithSerializer for AnyType {
type Serializer<'x> = quick_xml_serialize::AnyTypeSerializer<'x>;
fn serializer<'ser>(
&'ser self,
name: Option<&'ser str>,
is_root: bool,
) -> Result<Self::Serializer<'ser>, Error> {
Ok(quick_xml_serialize::AnyTypeSerializer {
value: self,
state: Box::new(quick_xml_serialize::AnyTypeSerializerState::Init__),
name: name.unwrap_or("anyType"),
is_root,
})
}
}
impl WithDeserializer for AnyType {
type Deserializer = quick_xml_deserialize::AnyTypeDeserializer;
}
pub type AnyUriType = String;
pub type Base64BinaryType = String;
pub type BooleanType = bool;
pub type ByteType = i8;
pub type DateType = String;
pub type DateTimeType = String;
pub type DecimalType = f64;
pub type DoubleType = f64;
pub type DurationType = String;
pub type FloatType = f32;
pub type GDayType = String;
pub type GMonthType = String;
pub type GMonthDayType = String;
pub type GYearType = String;
pub type GYearMonthType = String;
pub type HexBinaryType = String;
pub type IntType = i32;
pub type IntegerType = i32;
pub type LanguageType = String;
pub type LongType = i64;
pub type NegativeIntegerType = isize;
pub type NonNegativeIntegerType = usize;
pub type NonPositiveIntegerType = isize;
pub type NormalizedStringType = String;
pub type PositiveIntegerType = usize;
pub type ShortType = i16;
pub type StringType = String;
pub type TimeType = String;
pub type TokenType = String;
pub type UnsignedByteType = u8;
pub type UnsignedIntType = u32;
pub type UnsignedLongType = u64;
pub type UnsignedShortType = u16;
pub mod quick_xml_deserialize {
use core::mem::replace;
use xsd_parser_types::quick_xml::{
BytesStart, DeserializeHelper, Deserializer, DeserializerArtifact, DeserializerEvent,
DeserializerOutput, DeserializerResult, Error, Event,
};
#[derive(Debug)]
pub struct AnyTypeDeserializer {
state__: Box<AnyTypeDeserializerState>,
}
#[derive(Debug)]
enum AnyTypeDeserializerState {
Init__,
Unknown__,
}
impl AnyTypeDeserializer {
fn from_bytes_start(
helper: &mut DeserializeHelper,
bytes_start: &BytesStart<'_>,
) -> Result<Self, Error> {
Ok(Self {
state__: Box::new(AnyTypeDeserializerState::Init__),
})
}
fn finish_state(
&mut self,
helper: &mut DeserializeHelper,
state: AnyTypeDeserializerState,
) -> Result<(), Error> {
Ok(())
}
}
impl<'de> Deserializer<'de, super::AnyType> for AnyTypeDeserializer {
fn init(
helper: &mut DeserializeHelper,
event: Event<'de>,
) -> DeserializerResult<'de, super::AnyType> {
helper.init_deserializer_from_start_event(event, Self::from_bytes_start)
}
fn next(
mut self,
helper: &mut DeserializeHelper,
event: Event<'de>,
) -> DeserializerResult<'de, super::AnyType> {
if let Event::End(_) = &event {
Ok(DeserializerOutput {
artifact: DeserializerArtifact::Data(self.finish(helper)?),
event: DeserializerEvent::None,
allow_any: false,
})
} else if matches!(&event, Event::Text(_) | Event::CData(_)) {
Ok(DeserializerOutput {
artifact: DeserializerArtifact::Deserializer(self),
event: DeserializerEvent::None,
allow_any: true,
})
} else {
Ok(DeserializerOutput {
artifact: DeserializerArtifact::Deserializer(self),
event: DeserializerEvent::Break(event),
allow_any: true,
})
}
}
fn finish(mut self, helper: &mut DeserializeHelper) -> Result<super::AnyType, Error> {
let state = replace(&mut *self.state__, AnyTypeDeserializerState::Unknown__);
self.finish_state(helper, state)?;
Ok(super::AnyType {})
}
}
}
pub mod quick_xml_serialize {
use xsd_parser_types::quick_xml::{BytesStart, Error, Event, SerializeHelper, Serializer};
#[derive(Debug)]
pub struct AnyTypeSerializer<'ser> {
pub(super) value: &'ser super::AnyType,
pub(super) state: Box<AnyTypeSerializerState<'ser>>,
pub(super) name: &'ser str,
pub(super) is_root: bool,
}
#[derive(Debug)]
pub(super) enum AnyTypeSerializerState<'ser> {
Init__,
Done__,
Phantom__(&'ser ()),
}
impl<'ser> AnyTypeSerializer<'ser> {
fn next_event(
&mut self,
helper: &mut SerializeHelper,
) -> Result<Option<Event<'ser>>, Error> {
loop {
match &mut *self.state {
AnyTypeSerializerState::Init__ => {
*self.state = AnyTypeSerializerState::Done__;
let bytes = BytesStart::new(self.name);
return Ok(Some(Event::Empty(bytes)));
}
AnyTypeSerializerState::Done__ => return Ok(None),
AnyTypeSerializerState::Phantom__(_) => unreachable!(),
}
}
}
}
impl<'ser> Serializer<'ser> for AnyTypeSerializer<'ser> {
fn next(&mut self, helper: &mut SerializeHelper) -> Option<Result<Event<'ser>, Error>> {
match self.next_event(helper) {
Ok(Some(event)) => Some(Ok(event)),
Ok(None) => None,
Err(error) => {
*self.state = AnyTypeSerializerState::Done__;
Some(Err(error))
}
}
}
}
}