From 47c68f7d70cfa8c639f72361d8d0362048647075 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 20 Aug 2022 15:14:13 +0200 Subject: docs: improve the factory example --- examples/factory/bootstrap.rs | 7 ++++ examples/factory/interfaces/mod.rs | 1 + examples/factory/interfaces/user_manager.rs | 6 ++++ examples/factory/main.rs | 32 ++++-------------- examples/factory/user.rs | 6 ++-- examples/factory/user_manager.rs | 50 +++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 examples/factory/interfaces/user_manager.rs create mode 100644 examples/factory/user_manager.rs (limited to 'examples') diff --git a/examples/factory/bootstrap.rs b/examples/factory/bootstrap.rs index a44ccfb..b752764 100644 --- a/examples/factory/bootstrap.rs +++ b/examples/factory/bootstrap.rs @@ -3,14 +3,21 @@ use syrette::DIContainer; // Interfaces use crate::interfaces::user::{IUser, IUserFactory}; +use crate::interfaces::user_manager::IUserManager; // // Concrete implementations use crate::user::User; +use crate::user_manager::UserManager; pub fn bootstrap() -> DIContainer { let mut di_container: DIContainer = DIContainer::new(); + di_container + .bind::() + .to::() + .unwrap(); + di_container .bind::() .to_factory(&|name, date_of_birth, password| { diff --git a/examples/factory/interfaces/mod.rs b/examples/factory/interfaces/mod.rs index 22d12a3..2342750 100644 --- a/examples/factory/interfaces/mod.rs +++ b/examples/factory/interfaces/mod.rs @@ -1 +1,2 @@ pub mod user; +pub mod user_manager; diff --git a/examples/factory/interfaces/user_manager.rs b/examples/factory/interfaces/user_manager.rs new file mode 100644 index 0000000..83febb8 --- /dev/null +++ b/examples/factory/interfaces/user_manager.rs @@ -0,0 +1,6 @@ +pub trait IUserManager +{ + fn fill_with_users(&mut self); + + fn print_users(&self); +} diff --git a/examples/factory/main.rs b/examples/factory/main.rs index e2e80f1..bf3d43b 100644 --- a/examples/factory/main.rs +++ b/examples/factory/main.rs @@ -5,22 +5,11 @@ mod bootstrap; mod interfaces; mod user; +mod user_manager; use bootstrap::bootstrap; -use interfaces::user::IUser; -use interfaces::user::IUserFactory; -use syrette::ptr::FactoryPtr; -use syrette::ptr::TransientPtr; - -fn add_users( - users: &mut Vec>, - user_factory: &FactoryPtr, -) -{ - users.push(user_factory("Bob", "1983-04-13", "abc1234")); - users.push(user_factory("Anna", "1998-01-20", "IlovemYCat")); - users.push(user_factory("David", "2000-11-05", "12345678")); -} + +use crate::interfaces::user_manager::IUserManager; fn main() { @@ -28,20 +17,11 @@ fn main() let di_container = bootstrap(); - let user_factory = di_container.get_factory::().unwrap(); - - let mut users = Vec::>::new(); + let mut user_manager = di_container.get::().unwrap(); - add_users(&mut users, &user_factory); + user_manager.fill_with_users(); println!("Printing user information"); - for user in users { - println!( - "{}, born {}, password is '{}'", - user.get_name(), - user.get_date_of_birth(), - user.get_password() - ); - } + user_manager.print_users(); } diff --git a/examples/factory/user.rs b/examples/factory/user.rs index 121ad25..97a7632 100644 --- a/examples/factory/user.rs +++ b/examples/factory/user.rs @@ -27,16 +27,16 @@ impl IUser for User { fn get_name(&self) -> &'static str { - self.name.clone() + self.name } fn get_date_of_birth(&self) -> &'static str { - self.date_of_birth.clone() + self.date_of_birth } fn get_password(&self) -> &'static str { - self.password.clone() + self.password } } diff --git a/examples/factory/user_manager.rs b/examples/factory/user_manager.rs new file mode 100644 index 0000000..32ccbe9 --- /dev/null +++ b/examples/factory/user_manager.rs @@ -0,0 +1,50 @@ +use syrette::injectable; +use syrette::ptr::{FactoryPtr, TransientPtr}; + +use crate::interfaces::user::{IUser, IUserFactory}; +use crate::interfaces::user_manager::IUserManager; + +pub struct UserManager +{ + users: Vec>, + user_factory: FactoryPtr, +} + +#[injectable(IUserManager)] +impl UserManager +{ + pub fn new(user_factory: FactoryPtr) -> Self + { + Self { + users: Vec::new(), + user_factory, + } + } +} + +impl IUserManager for UserManager +{ + fn fill_with_users(&mut self) + { + self.users + .push((self.user_factory)("Bob", "1983-04-13", "abc1234")); + + self.users + .push((self.user_factory)("Anna", "1998-01-20", "IlovemYCat")); + + self.users + .push((self.user_factory)("David", "2000-11-05", "12345678")); + } + + fn print_users(&self) + { + for user in &self.users { + println!( + "{}, born {}, password is '{}'", + user.get_name(), + user.get_date_of_birth(), + user.get_password() + ); + } + } +} -- cgit v1.2.3-18-g5258