feat: add with macro option

This commit is contained in:
Vincent Herlemont
2023-10-29 09:34:45 +01:00
parent dc725a3380
commit fd5bbfd964
17 changed files with 240 additions and 120 deletions
@@ -21,6 +21,8 @@ pub(crate) struct ModelAttributes {
pub(crate) id: Option<LitInt>,
pub(crate) version: Option<LitInt>,
// type
pub(crate) with: Option<Path>,
// type
pub(crate) from: Option<Path>,
// (type, try_from::Error type)
pub(crate) try_from: Option<(Path, Path)>,
@@ -32,6 +34,8 @@ impl ModelAttributes {
self.id = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("version") {
self.version = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("with") {
self.with = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("from") {
self.from = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("try_from") {
@@ -72,6 +76,7 @@ impl Parse for TupleTryFrom {
/// Attributes:
/// - `id = u32`: The unique identifier of the model.
/// - `version = u32`: The version of the model.
/// - `with` = type: Required, the serialization/deserialization library that you use. Must implement `native_model::Encode` and `native_model::Decode`.
/// - `from = type`: Optional, the previous version of the model.
/// - `type`: The previous version of the model that you use for the From implementation.
/// - `try_from = (type, error)`: Optional, the previous version of the model with error handling.
@@ -92,7 +97,7 @@ pub fn native_model(args: TokenStream, input: TokenStream) -> TokenStream {
let native_model_id_fn = generate_native_model_id(&attrs);
let native_model_version_fn = generate_native_model_version(&attrs);
let native_model_encode_body_fn = generate_native_model_encode_body();
let native_model_encode_body_fn = generate_native_model_encode_body(&attrs);
let native_model_encode_downgrade_body_fn = generate_native_model_encode_downgrade_body(&attrs);
let native_model_decode_body_fn = generate_native_model_decode_body(&attrs);
let native_model_decode_upgrade_body_fn = generate_native_model_decode_upgrade_body(&attrs);
@@ -3,7 +3,8 @@ use proc_macro2::TokenStream;
use quote::quote;
pub(crate) fn generate_native_model_decode_body(attrs: &ModelAttributes) -> TokenStream {
let id = attrs.id.clone().expect("id is required");
let id = attrs.id.clone().expect("`id` is required");
let with = attrs.with.clone().expect("`with` is required");
let gen = quote! {
fn native_model_decode_body(data: Vec<u8>, id: u32) -> Result<Self, native_model::DecodeBodyError> {
println!("id: {}, {}", id, #id);
@@ -11,7 +12,8 @@ pub(crate) fn generate_native_model_decode_body(attrs: &ModelAttributes) -> Toke
return Err(native_model::DecodeBodyError::MismatchedModelId);
}
native_model_decode_body(data).map_err(|e| native_model::DecodeBodyError::DecodeError {
use native_model::Decode;
#with::decode(data).map_err(|e| native_model::DecodeBodyError::DecodeError {
msg: format!("{}", e),
source: e.into(),
})
@@ -1,10 +1,13 @@
use crate::ModelAttributes;
use proc_macro2::TokenStream;
use quote::quote;
pub(crate) fn generate_native_model_encode_body() -> TokenStream {
pub(crate) fn generate_native_model_encode_body(attrs: &ModelAttributes) -> TokenStream {
let with = attrs.with.clone().expect("`with` is required");
let gen = quote! {
fn native_model_encode_body(&self) -> Result<Vec<u8>, native_model::EncodeBodyError> {
native_model_encode_body(self).map_err(|e| native_model::EncodeBodyError {
use native_model::Encode;
#with::encode(self).map_err(|e| native_model::EncodeBodyError {
msg: format!("{}", e),
source: e.into(),
})
@@ -12,4 +15,4 @@ pub(crate) fn generate_native_model_encode_body() -> TokenStream {
};
gen.into()
}
}