Implement FromStr and TryFrom<&str> for content types.

This commit is contained in:
Jimmy Cuadra 2019-06-28 12:47:41 -07:00
parent 065cb770af
commit 733dfa7ef3

View File

@ -344,6 +344,48 @@ impl ToTokens for RumaEvent {
TokenStream::new()
};
let impl_conversions_for_content = if let Content::Struct(content_fields) = &self.content {
let mut content_field_values: Vec<TokenStream> =
Vec::with_capacity(content_fields.len());
for content_field in content_fields {
let content_field_ident = content_field.ident.clone().unwrap();
let span = content_field.span();
let token_stream = quote_spanned! {span=>
#content_field_ident: raw.#content_field_ident,
};
content_field_values.push(token_stream);
}
quote! {
impl std::str::FromStr for #content_name {
type Err = crate::InvalidEvent;
/// Attempt to create `Self` from parsing a string of JSON data.
fn from_str(json: &str) -> Result<Self, Self::Err> {
let raw = serde_json::from_str::<raw::#content_name>(json)?;
Ok(Self {
#(#content_field_values)*
})
}
}
impl<'a> std::convert::TryFrom<&'a str> for #content_name {
type Error = crate::InvalidEvent;
/// Attempt to create `Self` from parsing a string of JSON data.
fn try_from(json: &'a str) -> Result<Self, Self::Error> {
std::str::FromStr::from_str(json)
}
}
}
} else {
TokenStream::new()
};
let output = quote!(
#(#attrs)*
#[derive(Clone, PartialEq, Debug)]
@ -375,6 +417,8 @@ impl ToTokens for RumaEvent {
}
}
#impl_conversions_for_content
use serde::ser::SerializeStruct as _;
impl serde::Serialize for #name {