Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shumon84 committed Dec 13, 2024
1 parent 2fa9aeb commit 30978c8
Show file tree
Hide file tree
Showing 51 changed files with 3,615 additions and 7 deletions.
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

0 comments on commit 30978c8

Please sign in to comment.