Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for serializing namespaces in serde addressing #218 #466

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ encoding_rs = { version = "0.8", optional = true }
serde = { version = "1.0", optional = true }
tokio = { version = "1.20", optional = true, default-features = false, features = ["io-util"] }
memchr = "2.5"
quick_xml_derived = { path = "quick-xml-derived" }
q_meta = { path = "quick-xml-derived/q_meta"}

[dev-dependencies]
criterion = "0.3"
Expand All @@ -26,6 +28,9 @@ serde = { version = "1.0", features = ["derive"] }
serde-value = "0.7"
tokio = { version = "1.20", default-features = false, features = ["macros", "rt"] }
tokio-test = "0.4"
phf = { version = "0.11.1", features = ["macros"] }
q_meta = { path = "quick-xml-derived/q_meta"}


[lib]
bench = false
Expand Down Expand Up @@ -113,7 +118,7 @@ serialize = ["serde"]

[package.metadata.docs.rs]
# document all features
all-features = true
all-features = false
# defines the configuration attribute `docs_rs` to enable feature requirements
# See https://stackoverflow.com/questions/61417452
rustdoc-args = ["--cfg", "docs_rs"]
Expand All @@ -134,6 +139,10 @@ required-features = ["serialize"]
name = "serde-migrated"
required-features = ["serialize"]

[[test]]
name = "serde-namespace-se"
required-features = ["serialize"]

[[test]]
name = "async-tokio"
required-features = ["async-tokio"]
23 changes: 23 additions & 0 deletions quick-xml-derived/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "quick_xml_derived"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proc-macro2 = "1.0.43"
quote = "1.0.21"
syn = { version = "1.0.99", features = ["full"] }
serde = { version = "1.0", optional = true }
phf = { version = "0.11.1", features = ["macros"] }

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
phf = { version = "0.11.1", features = ["macros"] }
macrotest = "1.0.9"
q_meta = { path = "q_meta"}
trybuild = { version = "1.0.66", features = ["diff"] }

[lib]
proc-macro = true
8 changes: 8 additions & 0 deletions quick-xml-derived/q_meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "q_meta"
version = "0.1.0"
edition = "2018"

[dependencies]
phf = { version = "0.11.1", features = ["macros"] }
syllogism = "0.1.3"
80 changes: 80 additions & 0 deletions quick-xml-derived/q_meta/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use phf::phf_map;

#[derive(Debug)]
pub struct QuickXmlMeta {
pub namespace_declarations: &'static [(&'static str, &'static str)],
pub identifier_prefix_map: phf::Map<&'static str, &'static str>
}
pub trait CurrentItemVisitorQXmlMeta {
fn visit_current_item_as_self<T>(
&self,
contained_visitor: &mut T,
ident_in_parent: Option<&'static str>,
parent_meta: Option<&'static QuickXmlMeta>,
) -> &'static QuickXmlMeta
where
T: ContainedItemVisitorQXmlMeta;
}

impl CurrentItemVisitorQXmlMeta for String {
fn visit_current_item_as_self<T>(
&self,
contained_visitor: &mut T,
ident_in_parent: Option<&'static str>,
parent_meta: Option<&'static QuickXmlMeta>,
) -> &'static QuickXmlMeta
where
T: ContainedItemVisitorQXmlMeta
{
&QuickXmlMeta {
namespace_declarations: &[],
identifier_prefix_map: phf_map! {},
}
}
}

impl CurrentItemVisitorQXmlMeta for u32 {
fn visit_current_item_as_self<T>(
&self,
contained_visitor: &mut T,
ident_in_parent: Option<&'static str>,
parent_meta: Option<&'static QuickXmlMeta>,
) -> &'static QuickXmlMeta
where
T: ContainedItemVisitorQXmlMeta
{
&QuickXmlMeta {
namespace_declarations: &[],
identifier_prefix_map: phf_map! {},
}
}
}

impl<X: CurrentItemVisitorQXmlMeta> CurrentItemVisitorQXmlMeta for Vec<X> {
fn visit_current_item_as_self<T>(
&self,
contained_visitor: &mut T,
ident_in_parent: Option<&'static str>,
parent_meta: Option<&'static QuickXmlMeta>,
) -> &'static QuickXmlMeta
where
T: ContainedItemVisitorQXmlMeta
{
let self_meta = &QuickXmlMeta {
namespace_declarations: &[],
identifier_prefix_map: phf_map! {},
};
self.iter().for_each(|item| T::visit_contained_item(contained_visitor, item, ident_in_parent, parent_meta, true));
self_meta
}
}

pub trait ContainedItemVisitorQXmlMeta {
fn visit_contained_item<T: CurrentItemVisitorQXmlMeta>(
&mut self,
obj_to_ser: &T,
ident_in_parent: Option<&'static str>,
parent_meta: Option<&'static QuickXmlMeta>,
is_pseudoobject: bool,
);
}
13 changes: 13 additions & 0 deletions quick-xml-derived/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate proc_macro;
mod quick_xml_derive;
use quick_xml_derive::impl_quick_xml_derive;

use proc_macro::{TokenStream};
use syn;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(QuickXml, attributes(qxml,serde))]
pub fn quick_xml_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
impl_quick_xml_derive(input).unwrap_or_else(syn::Error::into_compile_error).into()
}
Loading