xsd-parser 1.5.2

Rust code generator for XML schema files
Documentation
use xsd_parser_types::misc::{Namespace, NamespacePrefix};
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 NS_TNS: Namespace = Namespace::new_const(b"http://example.com");
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");
pub const PREFIX_TNS: NamespacePrefix = NamespacePrefix::new_const(b"tns");
pub mod tns {
    use core::str::FromStr;
    use num::BigInt;
    use xsd_parser_types::quick_xml::{Error, WithDeserializer, WithSerializer};
    pub type Foo = FooType;
    #[derive(Debug)]
    pub struct FooType {
        pub a_int: BigInt,
        pub b_int: BigInt,
    }
    impl FooType {
        #[must_use]
        pub fn default_a_int() -> BigInt {
            <BigInt as FromStr>::from_str("123").unwrap()
        }
        #[must_use]
        pub fn default_b_int() -> BigInt {
            <BigInt as FromStr>::from_str("456").unwrap()
        }
    }
    impl WithSerializer for FooType {
        type Serializer<'x> = quick_xml_serialize::FooTypeSerializer<'x>;
        fn serializer<'ser>(
            &'ser self,
            name: Option<&'ser str>,
            is_root: bool,
        ) -> Result<Self::Serializer<'ser>, Error> {
            Ok(quick_xml_serialize::FooTypeSerializer {
                value: self,
                state: Box::new(quick_xml_serialize::FooTypeSerializerState::Init__),
                name: name.unwrap_or("tns:FooType"),
                is_root,
            })
        }
    }
    impl WithDeserializer for FooType {
        type Deserializer = quick_xml_deserialize::FooTypeDeserializer;
    }
    pub mod quick_xml_deserialize {
        use core::mem::replace;
        use num::BigInt;
        use xsd_parser_types::quick_xml::{
            BytesStart, DeserializeHelper, Deserializer, DeserializerArtifact, DeserializerEvent,
            DeserializerOutput, DeserializerResult, Error, Event,
        };
        #[derive(Debug)]
        pub struct FooTypeDeserializer {
            a_int: BigInt,
            b_int: BigInt,
            state__: Box<FooTypeDeserializerState>,
        }
        #[derive(Debug)]
        enum FooTypeDeserializerState {
            Init__,
            Unknown__,
        }
        impl FooTypeDeserializer {
            fn from_bytes_start(
                helper: &mut DeserializeHelper,
                bytes_start: &BytesStart<'_>,
            ) -> Result<Self, Error> {
                let mut a_int: Option<BigInt> = None;
                let mut b_int: Option<BigInt> = None;
                for attrib in helper.filter_xmlns_attributes(bytes_start) {
                    let attrib = attrib?;
                    if matches!(
                        helper.resolve_local_name(attrib.key, &super::super::NS_TNS),
                        Some(b"a-int")
                    ) {
                        helper.read_attrib(&mut a_int, b"a-int", &attrib.value)?;
                    } else if matches!(
                        helper.resolve_local_name(attrib.key, &super::super::NS_TNS),
                        Some(b"b-int")
                    ) {
                        helper.read_attrib(&mut b_int, b"b-int", &attrib.value)?;
                    } else {
                        helper.raise_unexpected_attrib_checked(&attrib)?;
                    }
                }
                Ok(Self {
                    a_int: a_int.unwrap_or_else(super::FooType::default_a_int),
                    b_int: b_int.unwrap_or_else(super::FooType::default_b_int),
                    state__: Box::new(FooTypeDeserializerState::Init__),
                })
            }
            fn finish_state(
                &mut self,
                helper: &mut DeserializeHelper,
                state: FooTypeDeserializerState,
            ) -> Result<(), Error> {
                Ok(())
            }
        }
        impl<'de> Deserializer<'de, super::FooType> for FooTypeDeserializer {
            fn init(
                helper: &mut DeserializeHelper,
                event: Event<'de>,
            ) -> DeserializerResult<'de, super::FooType> {
                helper.init_deserializer_from_start_event(event, Self::from_bytes_start)
            }
            fn next(
                mut self,
                helper: &mut DeserializeHelper,
                event: Event<'de>,
            ) -> DeserializerResult<'de, super::FooType> {
                if let Event::End(_) = &event {
                    Ok(DeserializerOutput {
                        artifact: DeserializerArtifact::Data(self.finish(helper)?),
                        event: DeserializerEvent::None,
                        allow_any: false,
                    })
                } else {
                    Ok(DeserializerOutput {
                        artifact: DeserializerArtifact::Deserializer(self),
                        event: DeserializerEvent::Break(event),
                        allow_any: false,
                    })
                }
            }
            fn finish(mut self, helper: &mut DeserializeHelper) -> Result<super::FooType, Error> {
                let state = replace(&mut *self.state__, FooTypeDeserializerState::Unknown__);
                self.finish_state(helper, state)?;
                Ok(super::FooType {
                    a_int: self.a_int,
                    b_int: self.b_int,
                })
            }
        }
    }
    pub mod quick_xml_serialize {
        use xsd_parser_types::quick_xml::{BytesStart, Error, Event, SerializeHelper, Serializer};
        #[derive(Debug)]
        pub struct FooTypeSerializer<'ser> {
            pub(super) value: &'ser super::FooType,
            pub(super) state: Box<FooTypeSerializerState<'ser>>,
            pub(super) name: &'ser str,
            pub(super) is_root: bool,
        }
        #[derive(Debug)]
        pub(super) enum FooTypeSerializerState<'ser> {
            Init__,
            Done__,
            Phantom__(&'ser ()),
        }
        impl<'ser> FooTypeSerializer<'ser> {
            fn next_event(
                &mut self,
                helper: &mut SerializeHelper,
            ) -> Result<Option<Event<'ser>>, Error> {
                loop {
                    match &mut *self.state {
                        FooTypeSerializerState::Init__ => {
                            *self.state = FooTypeSerializerState::Done__;
                            let mut bytes = BytesStart::new(self.name);
                            helper.begin_ns_scope();
                            if self.is_root {
                                helper.write_xmlns(
                                    &mut bytes,
                                    Some(&super::super::PREFIX_TNS),
                                    &super::super::NS_TNS,
                                );
                            }
                            helper.write_attrib(&mut bytes, "a-int", &self.value.a_int)?;
                            helper.write_attrib(&mut bytes, "b-int", &self.value.b_int)?;
                            helper.end_ns_scope();
                            return Ok(Some(Event::Empty(bytes)));
                        }
                        FooTypeSerializerState::Done__ => return Ok(None),
                        FooTypeSerializerState::Phantom__(_) => unreachable!(),
                    }
                }
            }
        }
        impl<'ser> Serializer<'ser> for FooTypeSerializer<'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 = FooTypeSerializerState::Done__;
                        Some(Err(error))
                    }
                }
            }
        }
    }
}