Move types to better scoped modules

This commit is contained in:
Dirkjan Ochtman 2022-09-06 23:15:20 +02:00
parent 685189f00d
commit ebb6ebb52c
6 changed files with 47 additions and 43 deletions

View File

@ -109,8 +109,9 @@ impl Deserializer {
let mut out = TokenStream::new(); let mut out = TokenStream::new();
out.extend(quote!( out.extend(quote!(
fn deserialize<'cx>(deserializer: &'cx mut ::instant_xml::Deserializer<'cx, 'xml>) -> Result<Self, ::instant_xml::Error> { fn deserialize<'cx>(deserializer: &'cx mut ::instant_xml::Deserializer<'cx, 'xml>) -> Result<Self, ::instant_xml::Error> {
use ::instant_xml::de::{XmlRecord, Deserializer, Visitor}; use ::instant_xml::de::{Deserializer, Id, Visitor, XmlRecord};
use ::instant_xml::Error; use ::instant_xml::Error;
use ::core::marker::PhantomData;
enum __Elements { enum __Elements {
#elements_enum #elements_enum
@ -123,15 +124,15 @@ impl Deserializer {
} }
struct StructVisitor #xml_ty_generics { struct StructVisitor #xml_ty_generics {
marker: std::marker::PhantomData<#ident #ty_generics>, marker: PhantomData<#ident #ty_generics>,
lifetime: std::marker::PhantomData<&'xml ()>, lifetime: PhantomData<&'xml ()>,
} }
impl #xml_impl_generics Visitor<'xml> for StructVisitor #xml_ty_generics #xml_where_clause { impl #xml_impl_generics Visitor<'xml> for StructVisitor #xml_ty_generics #xml_where_clause {
type Value = #ident #ty_generics; type Value = #ident #ty_generics;
fn visit_struct<'cx>( fn visit_struct<'cx>(
deserializer: &'cx mut ::instant_xml::Deserializer<'cx, 'xml>, deserializer: &'cx mut Deserializer<'cx, 'xml>,
) -> Result<Self::Value, Error> { ) -> Result<Self::Value, Error> {
#declare_values #declare_values
loop { loop {
@ -189,7 +190,7 @@ impl Deserializer {
)); ));
out.extend(quote!( out.extend(quote!(
const KIND: ::instant_xml::Kind = ::instant_xml::Kind::Element(::instant_xml::Id { const KIND: ::instant_xml::de::Kind = ::instant_xml::de::Kind::Element(::instant_xml::de::Id {
ns: #default_namespace, ns: #default_namespace,
name: #name, name: #name,
}); });
@ -235,8 +236,8 @@ impl Deserializer {
}; };
tokens.consts.extend(quote!( tokens.consts.extend(quote!(
const #const_field_var_str: ::instant_xml::Id<'static> = <#no_lifetime_type>::KIND.name( const #const_field_var_str: Id<'static> = <#no_lifetime_type>::KIND.name(
::instant_xml::Id { ns: #ns, name: #field_var_str } Id { ns: #ns, name: #field_var_str }
); );
)); ));

View File

@ -41,8 +41,10 @@ pub fn to_xml(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
&self, &self,
serializer: &mut instant_xml::Serializer<W>, serializer: &mut instant_xml::Serializer<W>,
) -> Result<(), instant_xml::Error> { ) -> Result<(), instant_xml::Error> {
use ::instant_xml::ser::{FieldAttribute, FieldContext};
let _ = serializer.consume_field_context(); let _ = serializer.consume_field_context();
let mut field_context = instant_xml::ser::FieldContext { let mut field_context = FieldContext {
name: #root_name, name: #root_name,
attribute: None, attribute: None,
}; };
@ -147,7 +149,7 @@ impl<'a> Serializer {
let field_value = field.ident.as_ref().unwrap(); let field_value = field.ident.as_ref().unwrap();
let declaration = quote!( let declaration = quote!(
let mut field = instant_xml::ser::FieldContext { let mut field = FieldContext {
name: #name, name: #name,
attribute: None, attribute: None,
}; };
@ -159,7 +161,7 @@ impl<'a> Serializer {
#declaration #declaration
serializer.add_attribute_key(&#name)?; serializer.add_attribute_key(&#name)?;
field.attribute = Some(instant_xml::FieldAttribute::Attribute); field.attribute = Some(FieldAttribute::Attribute);
serializer.set_field_context(field)?; serializer.set_field_context(field)?;
self.#field_value.serialize(serializer)?; self.#field_value.serialize(serializer)?;
)); ));
@ -177,8 +179,8 @@ impl<'a> Serializer {
body.extend(quote!( body.extend(quote!(
#declaration #declaration
match serializer.parent_namespaces.get(#ns) { match serializer.parent_namespaces.get(#ns) {
Some(prefix) => field.attribute = Some(::instant_xml::FieldAttribute::Prefix(prefix)), Some(prefix) => field.attribute = Some(FieldAttribute::Prefix(prefix)),
None => field.attribute = Some(::instant_xml::FieldAttribute::Namespace(#ns)), None => field.attribute = Some(FieldAttribute::Namespace(#ns)),
} }
serializer.set_field_context(field)?; serializer.set_field_context(field)?;
self.#field_value.serialize(serializer)?; self.#field_value.serialize(serializer)?;

View File

@ -1,6 +1,6 @@
use std::collections::{BTreeMap, VecDeque}; use std::collections::{BTreeMap, VecDeque};
use super::{Error, Id}; use super::Error;
use xmlparser::{ElementEnd, Token, Tokenizer}; use xmlparser::{ElementEnd, Token, Tokenizer};
pub struct Deserializer<'cx, 'xml> { pub struct Deserializer<'cx, 'xml> {
@ -315,3 +315,23 @@ pub struct Attribute<'xml> {
pub local: &'xml str, pub local: &'xml str,
pub value: &'xml str, pub value: &'xml str,
} }
pub enum Kind {
Scalar,
Element(Id<'static>),
}
impl Kind {
pub const fn name(&self, field: Id<'static>) -> Id<'static> {
match self {
Kind::Scalar => field,
Kind::Element(name) => *name,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Id<'a> {
pub ns: &'a str,
pub name: &'a str,
}

View File

@ -3,8 +3,9 @@ use std::fmt;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::str::FromStr; use std::str::FromStr;
use crate::de::{Visitor, XmlRecord}; use crate::de::{Visitor, XmlRecord, Kind};
use crate::{Deserializer, Error, FieldAttribute, FromXml, Kind, Serializer, ToXml}; use crate::ser::FieldAttribute;
use crate::{Deserializer, Error, FromXml, Serializer, ToXml};
// Deserializer // Deserializer
struct FromStrToVisitor<T: FromStr>(PhantomData<T>) struct FromStrToVisitor<T: FromStr>(PhantomData<T>)

View File

@ -7,8 +7,8 @@ pub use macros::{FromXml, ToXml};
#[doc(hidden)] #[doc(hidden)]
pub mod de; pub mod de;
mod impls; mod impls;
use de::Context;
pub use de::Deserializer; pub use de::Deserializer;
use de::{Context, Kind};
#[doc(hidden)] #[doc(hidden)]
pub mod ser; pub mod ser;
pub use ser::Serializer; pub use ser::Serializer;
@ -20,12 +20,6 @@ pub trait ToXml {
) -> Result<(), Error>; ) -> Result<(), Error>;
} }
pub enum FieldAttribute<'xml> {
Prefix(&'xml str),
Namespace(&'xml str),
Attribute,
}
pub trait FromXml<'xml>: Sized { pub trait FromXml<'xml>: Sized {
fn deserialize<'cx>(deserializer: &'cx mut Deserializer<'cx, 'xml>) -> Result<Self, Error>; fn deserialize<'cx>(deserializer: &'cx mut Deserializer<'cx, 'xml>) -> Result<Self, Error>;
@ -66,26 +60,6 @@ pub fn to_writer(
value.serialize(&mut Serializer::new(output)) value.serialize(&mut Serializer::new(output))
} }
pub enum Kind {
Scalar,
Element(Id<'static>),
}
impl Kind {
pub const fn name<'a>(&self, field: Id<'static>) -> Id<'static> {
match self {
Kind::Scalar => field,
Kind::Element(name) => *name,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Id<'a> {
pub ns: &'a str,
pub name: &'a str,
}
pub trait FromXmlOwned: for<'xml> FromXml<'xml> {} pub trait FromXmlOwned: for<'xml> FromXml<'xml> {}
#[derive(Clone, Debug, Eq, Error, PartialEq)] #[derive(Clone, Debug, Eq, Error, PartialEq)]

View File

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use super::{Error, FieldAttribute}; use super::Error;
pub struct Serializer<'xml, W: fmt::Write + ?Sized> { pub struct Serializer<'xml, W: fmt::Write + ?Sized> {
// For parent namespaces the key is the namespace and the value is the prefix. We are adding to map // For parent namespaces the key is the namespace and the value is the prefix. We are adding to map
@ -132,3 +132,9 @@ pub struct FieldContext<'xml> {
#[doc(hidden)] #[doc(hidden)]
pub attribute: Option<FieldAttribute<'xml>>, pub attribute: Option<FieldAttribute<'xml>>,
} }
pub enum FieldAttribute<'xml> {
Prefix(&'xml str),
Namespace(&'xml str),
Attribute,
}