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

【WIP】add new xml library #638

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
155 changes: 148 additions & 7 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions engine/runtime/xml2-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
edition = "2018"
name = "xml2-macro"
version = "0.1.0"

[dependencies]
proc-macro2 = "1"
quote = "1"
syn = "2"

[lib]
proc-macro = true
73 changes: 73 additions & 0 deletions engine/runtime/xml2-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

mod tuple;
mod union;

#[proc_macro_derive(UtilsTupleIo)]
pub fn tuple_serde(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
tuple::serde(&ast)
.unwrap_or_else(|err| err.to_compile_error())
.into()
}

// Adds YaSerialize and YaDeserialize implementations for types that support FromStr and Display traits.
#[proc_macro_derive(UtilsDefaultSerde)]
pub fn default_serde(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);

let struct_name = &ast.ident;
let struct_name_literal = &ast.ident.to_string();

let serde = quote! {
impl ::yaserde::YaSerialize for #struct_name {
fn serialize<W: ::std::io::Write>(
&self,
writer: &mut ::yaserde::ser::Serializer<W>,
) -> ::std::result::Result<(), ::std::string::String> {
crate::types::yaserde::serialize(
self,
#struct_name_literal,
writer, |s| s.to_string(),
)
}

fn serialize_attributes(
&self,
attributes: ::std::vec::Vec<::xml::attribute::OwnedAttribute>,
namespace: ::xml::namespace::Namespace,
) -> ::std::result::Result<
(
::std::vec::Vec<::xml::attribute::OwnedAttribute>,
::xml::namespace::Namespace,
),
::std::string::String,
> {
Ok((attributes, namespace))
}
}

impl ::yaserde::YaDeserialize for #struct_name {
fn deserialize<R: ::std::io::Read>(
reader: &mut ::yaserde::de::Deserializer<R>,
) -> ::std::result::Result<Self, ::std::string::String> {
crate::types::yaserde::deserialize(
reader,
|s| #struct_name::from_str(s).map_err(|e| e.to_string()),
)
}
}
};

serde.into()
}

#[proc_macro_derive(UtilsUnionSerDe)]
pub fn union_serde(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
union::serde(&ast)
.unwrap_or_else(|err| err.to_compile_error())
.into()
}
Loading
Loading