Rename types and variables to better reflect contents

This commit is contained in:
Dirkjan Ochtman 2022-09-06 23:20:04 +02:00
parent ebb6ebb52c
commit ebc29b5761
4 changed files with 52 additions and 42 deletions

View File

@ -109,7 +109,7 @@ 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::{Deserializer, Id, Visitor, XmlRecord}; use ::instant_xml::de::{Deserializer, Id, Visitor, Node};
use ::instant_xml::Error; use ::instant_xml::Error;
use ::core::marker::PhantomData; use ::core::marker::PhantomData;
@ -142,7 +142,7 @@ impl Deserializer {
}; };
match node { match node {
XmlRecord::Attribute(attr) => { Node::Attribute(attr) => {
let id = deserializer.attribute_id(&attr)?; let id = deserializer.attribute_id(&attr)?;
let field = { let field = {
#attributes_consts #attributes_consts
@ -157,7 +157,7 @@ impl Deserializer {
__Attributes::__Ignore => {} __Attributes::__Ignore => {}
} }
} }
XmlRecord::Open(data) => { Node::Open(data) => {
let id = deserializer.element_id(&data)?; let id = deserializer.element_id(&data)?;
let element = { let element = {
#elements_consts #elements_consts

View File

@ -5,7 +5,7 @@ mod ser;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use proc_macro2::{Delimiter, Group, Ident, Punct, TokenStream, TokenTree, Literal}; use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, TokenStream, TokenTree};
use quote::{quote, ToTokens}; use quote::{quote, ToTokens};
use syn::parse_macro_input; use syn::parse_macro_input;
use syn::punctuated::Punctuated; use syn::punctuated::Punctuated;
@ -299,11 +299,21 @@ impl NamespaceMeta {
match state { match state {
NsState::Start | NsState::Comma => {} NsState::Start | NsState::Comma => {}
NsState::Path { colon1: None, colon2: None, path: Some(path) } => { NsState::Path {
colon1: None,
colon2: None,
path: Some(path),
} => {
new.uri = Some(Namespace::Path(path)); new.uri = Some(Namespace::Path(path));
} }
NsState::PrefixPath { prefix, colon1: None, colon2: None, path: Some(path) } => { NsState::PrefixPath {
new.prefixes.insert(prefix.to_string(), Namespace::Path(path)); prefix,
colon1: None,
colon2: None,
path: Some(path),
} => {
new.prefixes
.insert(prefix.to_string(), Namespace::Path(path));
} }
state => panic!("invalid ns end state in xml attribute ({})", state.name()), state => panic!("invalid ns end state in xml attribute ({})", state.name()),
} }

View File

@ -12,24 +12,24 @@ pub struct Deserializer<'cx, 'xml> {
} }
impl<'cx, 'xml> Deserializer<'cx, 'xml> { impl<'cx, 'xml> Deserializer<'cx, 'xml> {
pub(crate) fn new(data: TagData<'xml>, context: &'cx mut Context<'xml>) -> Self { pub(crate) fn new(element: Element<'xml>, context: &'cx mut Context<'xml>) -> Self {
let level = context.stack.len(); let level = context.stack.len();
context.stack.push(data.level); context.stack.push(element.level);
Self { Self {
local: data.key, local: element.local,
prefix: data.prefix, prefix: element.prefix,
level, level,
done: false, done: false,
context, context,
} }
} }
pub fn nested<'a>(&'a mut self, data: TagData<'xml>) -> Deserializer<'a, 'xml> pub fn nested<'a>(&'a mut self, element: Element<'xml>) -> Deserializer<'a, 'xml>
where where
'cx: 'a, 'cx: 'a,
{ {
Deserializer::new(data, self.context) Deserializer::new(element, self.context)
} }
pub fn for_attr<'a>(&'a mut self, attr: Attribute<'xml>) -> Deserializer<'a, 'xml> pub fn for_attr<'a>(&'a mut self, attr: Attribute<'xml>) -> Deserializer<'a, 'xml>
@ -38,7 +38,7 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> {
{ {
self.context self.context
.records .records
.push_front(XmlRecord::AttributeValue(attr.value)); .push_front(Node::AttributeValue(attr.value));
Deserializer { Deserializer {
local: self.local, local: self.local,
@ -53,8 +53,8 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> {
loop { loop {
match self.next() { match self.next() {
Some(Err(e)) => return Err(e), Some(Err(e)) => return Err(e),
Some(Ok(XmlRecord::Open(data))) => { Some(Ok(Node::Open(element))) => {
let mut nested = self.nested(data); let mut nested = self.nested(element);
nested.ignore()?; nested.ignore()?;
} }
Some(_) => continue, Some(_) => continue,
@ -64,8 +64,8 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> {
} }
#[inline] #[inline]
pub fn element_id(&self, item: &TagData<'xml>) -> Result<Id<'xml>, Error> { pub fn element_id(&self, element: &Element<'xml>) -> Result<Id<'xml>, Error> {
self.context.element_id(item) self.context.element_id(element)
} }
#[inline] #[inline]
@ -75,7 +75,7 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> {
} }
impl<'xml> Iterator for Deserializer<'_, 'xml> { impl<'xml> Iterator for Deserializer<'_, 'xml> {
type Item = Result<XmlRecord<'xml>, Error>; type Item = Result<Node<'xml>, Error>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.done { if self.done {
@ -83,7 +83,7 @@ impl<'xml> Iterator for Deserializer<'_, 'xml> {
} }
let (prefix, local) = match self.context.next() { let (prefix, local) = match self.context.next() {
Some(Ok(XmlRecord::Close { prefix, local })) => (prefix, local), Some(Ok(Node::Close { prefix, local })) => (prefix, local),
item => return item, item => return item,
}; };
@ -99,11 +99,11 @@ impl<'xml> Iterator for Deserializer<'_, 'xml> {
pub(crate) struct Context<'xml> { pub(crate) struct Context<'xml> {
parser: Tokenizer<'xml>, parser: Tokenizer<'xml>,
stack: Vec<Level<'xml>>, stack: Vec<Level<'xml>>,
records: VecDeque<XmlRecord<'xml>>, records: VecDeque<Node<'xml>>,
} }
impl<'xml> Context<'xml> { impl<'xml> Context<'xml> {
pub(crate) fn new(input: &'xml str) -> Result<(Self, TagData<'xml>), Error> { pub(crate) fn new(input: &'xml str) -> Result<(Self, Element<'xml>), Error> {
let mut new = Self { let mut new = Self {
parser: Tokenizer::from(input), parser: Tokenizer::from(input),
stack: Vec::new(), stack: Vec::new(),
@ -112,7 +112,7 @@ impl<'xml> Context<'xml> {
let root = match new.next() { let root = match new.next() {
Some(result) => match result? { Some(result) => match result? {
XmlRecord::Open(data) => data, Node::Open(element) => element,
_ => return Err(Error::UnexpectedState), _ => return Err(Error::UnexpectedState),
}, },
None => return Err(Error::UnexpectedEndOfStream), None => return Err(Error::UnexpectedEndOfStream),
@ -121,8 +121,8 @@ impl<'xml> Context<'xml> {
Ok((new, root)) Ok((new, root))
} }
pub(crate) fn element_id(&self, item: &TagData<'xml>) -> Result<Id<'xml>, Error> { pub(crate) fn element_id(&self, element: &Element<'xml>) -> Result<Id<'xml>, Error> {
let ns = match (item.ns, item.prefix) { let ns = match (element.default_ns, element.prefix) {
(_, Some(prefix)) => match self.lookup(prefix) { (_, Some(prefix)) => match self.lookup(prefix) {
Some(ns) => ns, Some(ns) => ns,
None => return Err(Error::WrongNamespace), None => return Err(Error::WrongNamespace),
@ -133,7 +133,7 @@ impl<'xml> Context<'xml> {
Ok(Id { Ok(Id {
ns, ns,
name: &item.key, name: element.local,
}) })
} }
@ -169,7 +169,7 @@ impl<'xml> Context<'xml> {
} }
impl<'xml> Iterator for Context<'xml> { impl<'xml> Iterator for Context<'xml> {
type Item = Result<XmlRecord<'xml>, Error>; type Item = Result<Node<'xml>, Error>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if let Some(record) = self.records.pop_front() { if let Some(record) = self.records.pop_front() {
@ -200,14 +200,14 @@ impl<'xml> Iterator for Context<'xml> {
None => return Some(Err(Error::UnexpectedState)), None => return Some(Err(Error::UnexpectedState)),
}; };
let data = TagData { let element = Element {
key: level.local, local: level.local,
prefix: level.prefix, prefix: level.prefix,
ns: level.default_ns, default_ns: level.default_ns,
level, level,
}; };
return Some(Ok(XmlRecord::Open(data))); return Some(Ok(Node::Open(element)));
} }
ElementEnd::Close(prefix, v) => { ElementEnd::Close(prefix, v) => {
let level = match self.stack.pop() { let level = match self.stack.pop() {
@ -218,7 +218,7 @@ impl<'xml> Iterator for Context<'xml> {
let prefix = (!prefix.is_empty()).then_some(prefix.as_str()); let prefix = (!prefix.is_empty()).then_some(prefix.as_str());
match v.as_str() == level.local && prefix == level.prefix { match v.as_str() == level.local && prefix == level.prefix {
true => { true => {
return Some(Ok(XmlRecord::Close { return Some(Ok(Node::Close {
prefix, prefix,
local: level.local, local: level.local,
})) }))
@ -250,7 +250,7 @@ impl<'xml> Iterator for Context<'xml> {
} }
} else { } else {
let prefix = (!prefix.is_empty()).then_some(prefix.as_str()); let prefix = (!prefix.is_empty()).then_some(prefix.as_str());
self.records.push_back(XmlRecord::Attribute(Attribute { self.records.push_back(Node::Attribute(Attribute {
prefix, prefix,
local: local.as_str(), local: local.as_str(),
value: value.as_str(), value: value.as_str(),
@ -258,7 +258,7 @@ impl<'xml> Iterator for Context<'xml> {
} }
} }
Ok(Token::Text { text }) => { Ok(Token::Text { text }) => {
return Some(Ok(XmlRecord::Element(text.as_str()))); return Some(Ok(Node::Element(text.as_str())));
} }
Ok(_) => return Some(Err(Error::UnexpectedToken)), Ok(_) => return Some(Err(Error::UnexpectedToken)),
Err(e) => return Some(Err(Error::Parse(e))), Err(e) => return Some(Err(Error::Parse(e))),
@ -282,7 +282,7 @@ pub trait Visitor<'xml>: Sized {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum XmlRecord<'xml> { pub enum Node<'xml> {
Attribute(Attribute<'xml>), Attribute(Attribute<'xml>),
AttributeValue(&'xml str), AttributeValue(&'xml str),
Close { Close {
@ -290,13 +290,13 @@ pub enum XmlRecord<'xml> {
local: &'xml str, local: &'xml str,
}, },
Element(&'xml str), Element(&'xml str),
Open(TagData<'xml>), Open(Element<'xml>),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct TagData<'xml> { pub struct Element<'xml> {
key: &'xml str, local: &'xml str,
ns: Option<&'xml str>, default_ns: Option<&'xml str>,
prefix: Option<&'xml str>, prefix: Option<&'xml str>,
level: Level<'xml>, level: Level<'xml>,
} }

View File

@ -3,7 +3,7 @@ use std::fmt;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::str::FromStr; use std::str::FromStr;
use crate::de::{Visitor, XmlRecord, Kind}; use crate::de::{Kind, Node, Visitor};
use crate::ser::FieldAttribute; use crate::ser::FieldAttribute;
use crate::{Deserializer, Error, FromXml, Serializer, ToXml}; use crate::{Deserializer, Error, FromXml, Serializer, ToXml};
@ -369,8 +369,8 @@ where
V::Value: FromXml<'xml>, V::Value: FromXml<'xml>,
{ {
let value = match deserializer.next() { let value = match deserializer.next() {
Some(Ok(XmlRecord::AttributeValue(s))) => return V::visit_str(s), Some(Ok(Node::AttributeValue(s))) => return V::visit_str(s),
Some(Ok(XmlRecord::Element(s))) => V::visit_str(s)?, Some(Ok(Node::Element(s))) => V::visit_str(s)?,
Some(Ok(_)) => return Err(Error::ExpectedScalar), Some(Ok(_)) => return Err(Error::ExpectedScalar),
Some(Err(e)) => return Err(e), Some(Err(e)) => return Err(e),
None => return <V::Value as FromXml<'_>>::missing_value(), None => return <V::Value as FromXml<'_>>::missing_value(),