aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-20 15:14:13 +0200
committerHampusM <hampus@hampusmat.com>2022-08-21 18:17:50 +0200
commit47c68f7d70cfa8c639f72361d8d0362048647075 (patch)
tree871f3577e54f77aa51368f1e34012c1f4f31e44d /examples
parentc4eccc81d9bfa472197a4f302df1c967081a0be5 (diff)
docs: improve the factory example
Diffstat (limited to 'examples')
-rw-r--r--examples/factory/bootstrap.rs7
-rw-r--r--examples/factory/interfaces/mod.rs1
-rw-r--r--examples/factory/interfaces/user_manager.rs6
-rw-r--r--examples/factory/main.rs32
-rw-r--r--examples/factory/user.rs6
-rw-r--r--examples/factory/user_manager.rs50
6 files changed, 73 insertions, 29 deletions
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,15 +3,22 @@ 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::<dyn IUserManager>()
+ .to::<UserManager>()
+ .unwrap();
+
+ di_container
.bind::<IUserFactory>()
.to_factory(&|name, date_of_birth, password| {
let user: TransientPtr<dyn IUser> =
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<TransientPtr<dyn IUser>>,
- user_factory: &FactoryPtr<IUserFactory>,
-)
-{
- 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::<IUserFactory>().unwrap();
-
- let mut users = Vec::<TransientPtr<dyn IUser>>::new();
+ let mut user_manager = di_container.get::<dyn IUserManager>().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<TransientPtr<dyn IUser>>,
+ user_factory: FactoryPtr<IUserFactory>,
+}
+
+#[injectable(IUserManager)]
+impl UserManager
+{
+ pub fn new(user_factory: FactoryPtr<IUserFactory>) -> 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()
+ );
+ }
+ }
+}