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

View File

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

View File

@ -1,6 +1,6 @@
use std::collections::{BTreeMap, VecDeque};
use super::{Error, Id};
use super::Error;
use xmlparser::{ElementEnd, Token, Tokenizer};
pub struct Deserializer<'cx, 'xml> {
@ -315,3 +315,23 @@ pub struct Attribute<'xml> {
pub local: &'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::str::FromStr;
use crate::de::{Visitor, XmlRecord};
use crate::{Deserializer, Error, FieldAttribute, FromXml, Kind, Serializer, ToXml};
use crate::de::{Visitor, XmlRecord, Kind};
use crate::ser::FieldAttribute;
use crate::{Deserializer, Error, FromXml, Serializer, ToXml};
// Deserializer
struct FromStrToVisitor<T: FromStr>(PhantomData<T>)

View File

@ -7,8 +7,8 @@ pub use macros::{FromXml, ToXml};
#[doc(hidden)]
pub mod de;
mod impls;
use de::Context;
pub use de::Deserializer;
use de::{Context, Kind};
#[doc(hidden)]
pub mod ser;
pub use ser::Serializer;
@ -20,12 +20,6 @@ pub trait ToXml {
) -> Result<(), Error>;
}
pub enum FieldAttribute<'xml> {
Prefix(&'xml str),
Namespace(&'xml str),
Attribute,
}
pub trait FromXml<'xml>: Sized {
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))
}
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> {}
#[derive(Clone, Debug, Eq, Error, PartialEq)]

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::fmt::{self, Write};
use super::{Error, FieldAttribute};
use super::Error;
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
@ -132,3 +132,9 @@ pub struct FieldContext<'xml> {
#[doc(hidden)]
pub attribute: Option<FieldAttribute<'xml>>,
}
pub enum FieldAttribute<'xml> {
Prefix(&'xml str),
Namespace(&'xml str),
Attribute,
}