mirror of
https://github.com/instant-labs/instant-xml.git
synced 2025-02-17 15:32:07 +00:00
Passing first test
This commit is contained in:
commit
6dd096123d
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
Cargo.lock
|
2
Cargo.toml
Normal file
2
Cargo.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[workspace]
|
||||||
|
members = ["instant-xml", "instant-xml-macros"]
|
13
instant-xml-macros/Cargo.toml
Normal file
13
instant-xml-macros/Cargo.toml
Normal file
@ -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"] }
|
22
instant-xml-macros/src/lib.rs
Normal file
22
instant-xml-macros/src/lib.rs
Normal file
@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
10
instant-xml/Cargo.toml
Normal file
10
instant-xml/Cargo.toml
Normal file
@ -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"
|
27
instant-xml/src/lib.rs
Normal file
27
instant-xml/src/lib.rs
Normal file
@ -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),
|
||||||
|
}
|
9
instant-xml/tests/all.rs
Normal file
9
instant-xml/tests/all.rs
Normal file
@ -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
Block a user