Skip to content

Commit

Permalink
feat: add native_model_id_str and native_model_version_str
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Dec 17, 2023
1 parent 3b88418 commit 5eacdb8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions native_model_macro/src/method/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub(crate) fn generate_native_model_id(model_attributes: &ModelAttributes) -> To
fn native_model_id() -> u32 {
#native_model_id
}

fn native_model_id_str() -> &'static str {
stringify!(#native_model_id)
}
};
gen
}
4 changes: 4 additions & 0 deletions native_model_macro/src/method/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub(crate) fn generate_native_model_version(model_attributes: &ModelAttributes)
fn native_model_version() -> u32 {
#native_model_version
}

fn native_model_version_str() -> &'static str {
stringify!(#native_model_version)
}
};
gen
}
2 changes: 2 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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>
Expand Down
11 changes: 10 additions & 1 deletion tests_crate/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ impl From<Foo2> for Foo1 {
}

#[test]
fn test_simple() {
fn get_id_version_int() {
assert_eq!(Foo1::native_model_id(), 1);
assert_eq!(Foo1::native_model_version(), 1);

assert_eq!(Foo2::native_model_id(), 1);
assert_eq!(Foo2::native_model_version(), 2);
}

#[test]
fn get_id_version_str() {
assert_eq!(Foo1::native_model_id_str(), "1");
assert_eq!(Foo1::native_model_version_str(), "1");

assert_eq!(Foo2::native_model_id_str(), "1");
assert_eq!(Foo2::native_model_version_str(), "2");
}

0 comments on commit 5eacdb8

Please sign in to comment.