Passing first test
This commit is contained in:
commit
6dd096123d
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
|
@ -0,0 +1,2 @@
|
|||
[workspace]
|
||||
members = ["instant-xml", "instant-xml-macros"]
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "instant-xml-macros"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
workspace = ".."
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
heck = "0.4"
|
||||
quote = "1.0.18"
|
||||
syn = { version = "1.0.86", features = ["full"] }
|
|
@ -0,0 +1,22 @@
|
|||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::parse_macro_input;
|
||||
|
||||
#[proc_macro_derive(ToXml)]
|
||||
pub fn to_xml(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as syn::ItemStruct);
|
||||
let ident = &ast.ident;
|
||||
let name = ident.to_string();
|
||||
TokenStream::from(quote!(
|
||||
impl ToXml for #ident {
|
||||
fn write_xml<W: ::std::fmt::Write>(&self, write: &mut W) -> Result<(), instant_xml::Error> {
|
||||
write.write_str("<")?;
|
||||
write.write_fmt(format_args!("{}", #name))?;
|
||||
write.write_str("/>")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "instant-xml"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
macros = { package = "instant-xml-macros", version = "0.1", path = "../instant-xml-macros" }
|
||||
thiserror = "1.0.29"
|
||||
xmlparser = "0.13.3"
|
|
@ -0,0 +1,27 @@
|
|||
use std::fmt;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
pub use macros::ToXml;
|
||||
|
||||
pub trait ToXml {
|
||||
fn write_xml<W: fmt::Write>(&self, write: &mut W) -> Result<(), Error>;
|
||||
|
||||
fn to_xml(&self) -> Result<String, Error> {
|
||||
let mut out = String::new();
|
||||
self.write_xml(&mut out)?;
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromXml<'xml>: Sized {
|
||||
fn from_xml(input: &str) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
pub trait FromXmlOwned: for<'xml> FromXml<'xml> {}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("format: {0}")]
|
||||
Format(#[from] fmt::Error),
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
use instant_xml::ToXml;
|
||||
|
||||
#[derive(ToXml)]
|
||||
struct Unit;
|
||||
|
||||
#[test]
|
||||
fn unit() {
|
||||
assert_eq!(Unit.to_xml().unwrap(), "<Unit/>");
|
||||
}
|
Loading…
Reference in New Issue