Add proc macro to implement models
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
37
heromodels/examples/custom_model_example.rs
Normal file
37
heromodels/examples/custom_model_example.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use heromodels::model;
|
||||
use heromodels::models::core::model::{BaseModelData, Model, Index, IndexKey};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
// Define a custom attribute for indexing
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[model]
|
||||
pub struct CustomUser {
|
||||
pub base_data: BaseModelData,
|
||||
|
||||
// Mark fields for indexing with a comment
|
||||
// #[index]
|
||||
pub login: String,
|
||||
|
||||
// #[index]
|
||||
pub is_active: bool,
|
||||
|
||||
pub full_name: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hero Models - Custom Model Example");
|
||||
println!("==================================");
|
||||
|
||||
// Example usage of the generated implementation
|
||||
println!("CustomUser DB Prefix: {}", CustomUser::db_prefix());
|
||||
|
||||
let user = CustomUser {
|
||||
base_data: BaseModelData::new(1),
|
||||
login: "johndoe".to_string(),
|
||||
is_active: true,
|
||||
full_name: "John Doe".to_string(),
|
||||
};
|
||||
|
||||
println!("\nCustomUser ID: {}", user.get_id());
|
||||
println!("CustomUser DB Keys: {:?}", user.db_keys());
|
||||
}
|
58
heromodels/examples/model_macro_example.rs
Normal file
58
heromodels/examples/model_macro_example.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use heromodels::model;
|
||||
use heromodels::models::core::model::{BaseModelData, Model, Index, IndexKey};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
// Basic usage
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[model]
|
||||
pub struct SimpleUser {
|
||||
pub base_data: BaseModelData,
|
||||
|
||||
/// @index
|
||||
pub login: String,
|
||||
|
||||
pub full_name: String,
|
||||
}
|
||||
|
||||
// With customization options
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[model(prefix = "custom_user")]
|
||||
pub struct CustomUser {
|
||||
pub base_data: BaseModelData,
|
||||
|
||||
/// @index(name = "user_name", key_type = "str")
|
||||
pub login_name: String,
|
||||
|
||||
/// @index(key_type = "bool")
|
||||
pub is_active: bool,
|
||||
|
||||
pub full_name: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hero Models - Model Macro Example");
|
||||
println!("=================================");
|
||||
|
||||
// Example usage of the generated implementations
|
||||
println!("SimpleUser DB Prefix: {}", SimpleUser::db_prefix());
|
||||
println!("CustomUser DB Prefix: {}", CustomUser::db_prefix());
|
||||
|
||||
let user = SimpleUser {
|
||||
base_data: BaseModelData::new(1),
|
||||
login: "johndoe".to_string(),
|
||||
full_name: "John Doe".to_string(),
|
||||
};
|
||||
|
||||
let custom_user = CustomUser {
|
||||
base_data: BaseModelData::new(2),
|
||||
login_name: "janesmith".to_string(),
|
||||
is_active: true,
|
||||
full_name: "Jane Smith".to_string(),
|
||||
};
|
||||
|
||||
println!("\nSimpleUser ID: {}", user.get_id());
|
||||
println!("SimpleUser DB Keys: {:?}", user.db_keys());
|
||||
|
||||
println!("\nCustomUser ID: {}", custom_user.get_id());
|
||||
println!("CustomUser DB Keys: {:?}", custom_user.db_keys());
|
||||
}
|
29
heromodels/examples/simple_model_example.rs
Normal file
29
heromodels/examples/simple_model_example.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use heromodels::model;
|
||||
use heromodels::models::core::model::{BaseModelData, Model, IndexKey};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
// Basic usage
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[model]
|
||||
pub struct SimpleUser {
|
||||
pub base_data: BaseModelData,
|
||||
pub login: String,
|
||||
pub full_name: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hero Models - Simple Model Example");
|
||||
println!("==================================");
|
||||
|
||||
// Example usage of the generated implementation
|
||||
println!("SimpleUser DB Prefix: {}", SimpleUser::db_prefix());
|
||||
|
||||
let user = SimpleUser {
|
||||
base_data: BaseModelData::new(1),
|
||||
login: "johndoe".to_string(),
|
||||
full_name: "John Doe".to_string(),
|
||||
};
|
||||
|
||||
println!("\nSimpleUser ID: {}", user.get_id());
|
||||
println!("SimpleUser DB Keys: {:?}", user.db_keys());
|
||||
}
|
Reference in New Issue
Block a user