mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-26 01:34:38 +10:00
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};
|
use syn::{parse_macro_input, DeriveInput, LitInt, Path, Token};
|
||||||
|
|
||||||
// Inspiration: https://docs.rs/syn/2.0.29/syn/meta/fn.parser.html#example-1
|
// Inspiration: https://docs.rs/syn/2.0.29/syn/meta/fn.parser.html#example-1
|
||||||
#[derive(Default)]
|
|
||||||
pub(crate) struct ModelAttributes {
|
pub(crate) struct ModelAttributes {
|
||||||
pub(crate) id: Option<LitInt>,
|
pub(crate) id: Option<LitInt>,
|
||||||
pub(crate) version: Option<LitInt>,
|
pub(crate) version: Option<LitInt>,
|
||||||
@@ -28,6 +27,18 @@ pub(crate) struct ModelAttributes {
|
|||||||
pub(crate) try_from: Option<(Path, Path)>,
|
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 {
|
impl ModelAttributes {
|
||||||
fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> {
|
fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> {
|
||||||
if meta.path.is_ident("id") {
|
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 with = attrs.with.clone().expect("`with` is required");
|
||||||
let gen = quote! {
|
let gen = quote! {
|
||||||
fn native_model_decode_body(data: Vec<u8>, id: u32) -> std::result::Result<Self, native_model::DecodeBodyError> {
|
fn native_model_decode_body(data: Vec<u8>, id: u32) -> std::result::Result<Self, native_model::DecodeBodyError> {
|
||||||
println!("id: {}, {}", id, #id);
|
|
||||||
if id != #id {
|
if id != #id {
|
||||||
return Err(native_model::DecodeBodyError::MismatchedModelId);
|
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_1_3;
|
||||||
mod bincode_2_rc;
|
mod bincode_2_rc;
|
||||||
mod postcard_1_0;
|
mod postcard_1_0;
|
||||||
@@ -2,7 +2,8 @@ use native_model::{native_model};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::Serialize;
|
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)]
|
#[native_model(id = 1, version = 1, with = native_model::postcard_1_0::PostCard)]
|
||||||
struct Example {
|
struct Example {
|
||||||
a: u32,
|
a: u32,
|
||||||
|
|||||||
Reference in New Issue
Block a user