fix: move model trait to lib module

This commit is contained in:
Vincent Herlemont
2023-12-17 18:45:29 +01:00
parent b3398c6dad
commit 1de6743017
2 changed files with 64 additions and 69 deletions
+64 -4
View File
@@ -31,12 +31,8 @@ mod codec;
))]
pub use codec::*;
mod header;
pub mod model;
pub mod wrapper;
// Re-export
pub use model::*;
// Macro to generate a [`native_model`] implementation for a struct.
pub use native_model_macro::*;
@@ -133,3 +129,67 @@ pub fn encode_downgrade<T: crate::Model>(model: T, version: u32) -> Result<Vec<u
pub fn decode<T: crate::Model>(data: Vec<u8>) -> Result<(T, u32)> {
T::native_model_decode(data)
}
pub trait Model: Sized {
fn native_model_id() -> u32;
fn native_model_id_str() -> &'static str;
fn native_model_version() -> u32;
fn native_model_version_str() -> &'static str;
// --------------- Decode ---------------
fn native_model_decode_body(data: Vec<u8>, id: u32) -> DecodeResult<Self>
where
Self: Sized;
fn native_model_decode_upgrade_body(data: Vec<u8>, id: u32, version: u32) -> Result<Self>
where
Self: Sized;
fn native_model_decode(data: Vec<u8>) -> Result<(Self, u32)>
where
Self: Sized,
{
let native_model = crate::Wrapper::deserialize(&data[..]).unwrap();
let source_id = native_model.get_id();
let source_version = native_model.get_version();
let result = Self::native_model_decode_upgrade_body(
native_model.value().to_vec(),
source_id,
source_version,
)?;
Ok((result, source_version))
}
// --------------- Encode ---------------
fn native_model_encode_body(&self) -> EncodeResult<Vec<u8>>
where
Self: Sized;
fn native_model_encode_downgrade_body(self, version: u32) -> Result<Vec<u8>>
where
Self: Sized;
fn native_model_encode(&self) -> Result<Vec<u8>>
where
Self: Sized,
{
let mut data = self.native_model_encode_body()?;
let data = crate::native_model_encode(
&mut data,
Self::native_model_id(),
Self::native_model_version(),
);
Ok(data)
}
fn native_model_encode_downgrade(self, version: u32) -> Result<Vec<u8>>
where
Self: Sized,
{
let version = version.clone();
let mut data = self.native_model_encode_downgrade_body(version)?;
let data = crate::native_model_encode(&mut data, Self::native_model_id(), version);
Ok(data)
}
}
-65
View File
@@ -1,65 +0,0 @@
use crate::{DecodeResult, EncodeResult, Result};
pub trait Model: Sized {
fn native_model_id() -> u32;
fn native_model_id_str() -> &'static str;
fn native_model_version() -> u32;
fn native_model_version_str() -> &'static str;
// --------------- Decode ---------------
fn native_model_decode_body(data: Vec<u8>, id: u32) -> DecodeResult<Self>
where
Self: Sized;
fn native_model_decode_upgrade_body(data: Vec<u8>, id: u32, version: u32) -> Result<Self>
where
Self: Sized;
fn native_model_decode(data: Vec<u8>) -> Result<(Self, u32)>
where
Self: Sized,
{
let native_model = crate::Wrapper::deserialize(&data[..]).unwrap();
let source_id = native_model.get_id();
let source_version = native_model.get_version();
let result = Self::native_model_decode_upgrade_body(
native_model.value().to_vec(),
source_id,
source_version,
)?;
Ok((result, source_version))
}
// --------------- Encode ---------------
fn native_model_encode_body(&self) -> EncodeResult<Vec<u8>>
where
Self: Sized;
fn native_model_encode_downgrade_body(self, version: u32) -> Result<Vec<u8>>
where
Self: Sized;
fn native_model_encode(&self) -> Result<Vec<u8>>
where
Self: Sized,
{
let mut data = self.native_model_encode_body()?;
let data = crate::native_model_encode(
&mut data,
Self::native_model_id(),
Self::native_model_version(),
);
Ok(data)
}
fn native_model_encode_downgrade(self, version: u32) -> Result<Vec<u8>>
where
Self: Sized,
{
let version = version.clone();
let mut data = self.native_model_encode_downgrade_body(version)?;
let data = crate::native_model_encode(&mut data, Self::native_model_id(), version);
Ok(data)
}
}