-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
3,615 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.