use bincode::{config, Decode, Encode}; use native_model_macro::native_model; fn native_model_encode_body(obj: &T) -> Result, bincode::error::EncodeError> { bincode::encode_to_vec(obj, config::standard()) } fn native_model_decode_body(data: Vec) -> Result { bincode::decode_from_slice(&data, config::standard()).map(|(result, _)| result) } #[derive(Debug, Encode, Decode, PartialEq)] #[native_model(id = 1, version = 1)] struct Foo1 { x: i32, } #[derive(Debug, Encode, Decode, PartialEq)] #[native_model(id = 1, version = 2, try_from = (Foo1, anyhow::Error))] struct Foo2 { x: i32, } impl TryFrom for Foo2 { type Error = anyhow::Error; fn try_from(foo1: Foo1) -> Result { if foo1.x > 10 { return Err(anyhow::anyhow!("x > 10")); } Ok(Foo2 { x: foo1.x }) } } impl TryFrom for Foo1 { type Error = anyhow::Error; fn try_from(foo2: Foo2) -> Result { if foo2.x > 10 { return Err(anyhow::anyhow!("x > 10")); } Ok(Foo1 { x: foo2.x }) } } #[test] fn test_foo1_to_foo1() { let foo1 = Foo1 { x: 1 }; let foo1_packed = native_model::encode(&foo1).unwrap(); let (foo1_decoded, _) = native_model::decode::(foo1_packed.clone()).unwrap(); assert_eq!(foo1, foo1_decoded); } #[test] fn test_foo1_to_foo2() { let foo1 = Foo1 { x: 1 }; let foo1_packed = native_model::encode(&foo1).unwrap(); let (foo2_decoded, _) = native_model::decode::(foo1_packed.clone()).unwrap(); assert_eq!(Foo2 { x: 1 }, foo2_decoded); } #[test] fn test_foo1_to_foo2_error() { let foo1 = Foo1 { x: 1000 }; let foo1_packed = native_model::encode(&foo1).unwrap(); let foo2_decoded = native_model::decode::(foo1_packed.clone()); assert!(foo2_decoded.is_err()); assert!(matches!( foo2_decoded.unwrap_err(), native_model::Error::UpgradeError(_) )); }