feat: bincode_1_3 by default
This commit is contained in:
@@ -16,7 +16,6 @@ use syn::token;
|
||||
use syn::{parse_macro_input, DeriveInput, LitInt, Path, Token};
|
||||
|
||||
// Inspiration: https://docs.rs/syn/2.0.29/syn/meta/fn.parser.html#example-1
|
||||
#[derive(Default)]
|
||||
pub(crate) struct ModelAttributes {
|
||||
pub(crate) id: Option<LitInt>,
|
||||
pub(crate) version: Option<LitInt>,
|
||||
@@ -28,6 +27,18 @@ pub(crate) struct ModelAttributes {
|
||||
pub(crate) try_from: Option<(Path, Path)>,
|
||||
}
|
||||
|
||||
impl Default for ModelAttributes {
|
||||
fn default() -> Self {
|
||||
ModelAttributes {
|
||||
id: None,
|
||||
version: None,
|
||||
with: Some(syn::parse_str::<Path>("native_model::bincode_1_3::Bincode").unwrap()),
|
||||
from: None,
|
||||
try_from: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ModelAttributes {
|
||||
fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> {
|
||||
if meta.path.is_ident("id") {
|
||||
|
||||
@@ -7,7 +7,6 @@ pub(crate) fn generate_native_model_decode_body(attrs: &ModelAttributes) -> Toke
|
||||
let with = attrs.with.clone().expect("`with` is required");
|
||||
let gen = quote! {
|
||||
fn native_model_decode_body(data: Vec<u8>, id: u32) -> std::result::Result<Self, native_model::DecodeBodyError> {
|
||||
println!("id: {}, {}", id, #id);
|
||||
if id != #id {
|
||||
return Err(native_model::DecodeBodyError::MismatchedModelId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
use native_model::native_model;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
#[native_model(id = 1, version = 1)]
|
||||
struct Example {
|
||||
a: u32,
|
||||
b: u32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_decode() {
|
||||
let example = Example { a: 1, b: 2 };
|
||||
let bytes = native_model::encode(&example).unwrap();
|
||||
let (example, _) = native_model::decode::<Example>(bytes).unwrap();
|
||||
assert_eq!(example, Example { a: 1, b: 2 });
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
mod default;
|
||||
mod bincode_1_3;
|
||||
mod bincode_2_rc;
|
||||
mod postcard_1_0;
|
||||
@@ -2,7 +2,8 @@ use native_model::{native_model};
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
#[derive(Serialize)]
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
#[native_model(id = 1, version = 1, with = native_model::postcard_1_0::PostCard)]
|
||||
struct Example {
|
||||
a: u32,
|
||||
|
||||
Reference in New Issue
Block a user