aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-22 13:25:45 +0200
committerHampusM <hampus@hampusmat.com>2022-07-22 13:25:45 +0200
commit4cb3884e24b3cba3347ff93475bbabd6fe18d2fa (patch)
tree2fa5e6d81de9dc39bd11d64797914e5d305d98e2 /examples
parent157f38bc2287dcb9a8b21ef3d5e33c569dc5136e (diff)
refactor: make factories an optional feature
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/animals/cow.rs24
-rw-r--r--examples/basic/animals/human.rs20
-rw-r--r--examples/basic/animals/mod.rs1
-rw-r--r--examples/basic/bootstrap.rs7
-rw-r--r--examples/basic/interfaces/cow.rs10
-rw-r--r--examples/basic/interfaces/mod.rs1
-rw-r--r--examples/factory/bootstrap.rs24
-rw-r--r--examples/factory/interfaces/mod.rs1
-rw-r--r--examples/factory/interfaces/user.rs13
-rw-r--r--examples/factory/main.rs47
-rw-r--r--examples/factory/user.rs42
11 files changed, 130 insertions, 60 deletions
diff --git a/examples/basic/animals/cow.rs b/examples/basic/animals/cow.rs
deleted file mode 100644
index a75d750..0000000
--- a/examples/basic/animals/cow.rs
+++ /dev/null
@@ -1,24 +0,0 @@
-use crate::interfaces::cow::ICow;
-
-pub struct Cow
-{
- moo_cnt: i32,
-}
-
-impl Cow
-{
- pub fn new(moo_cnt: i32) -> Self
- {
- Self { moo_cnt }
- }
-}
-
-impl ICow for Cow
-{
- fn moo(&self)
- {
- for _ in 0..self.moo_cnt {
- println!("Moo");
- }
- }
-}
diff --git a/examples/basic/animals/human.rs b/examples/basic/animals/human.rs
index 5bd2f8f..00574a3 100644
--- a/examples/basic/animals/human.rs
+++ b/examples/basic/animals/human.rs
@@ -1,8 +1,7 @@
use syrette::injectable;
-use syrette::ptr::{FactoryPtr, InterfacePtr};
+use syrette::ptr::InterfacePtr;
use crate::interfaces::cat::ICat;
-use crate::interfaces::cow::{CowFactory, ICow};
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
@@ -10,23 +9,14 @@ pub struct Human
{
dog: InterfacePtr<dyn IDog>,
cat: InterfacePtr<dyn ICat>,
- cow_factory: FactoryPtr<CowFactory>,
}
#[injectable(IHuman)]
impl Human
{
- pub fn new(
- dog: InterfacePtr<dyn IDog>,
- cat: InterfacePtr<dyn ICat>,
- cow_factory: FactoryPtr<CowFactory>,
- ) -> Self
+ pub fn new(dog: InterfacePtr<dyn IDog>, cat: InterfacePtr<dyn ICat>) -> Self
{
- Self {
- dog,
- cat,
- cow_factory,
- }
+ Self { dog, cat }
}
}
@@ -41,9 +31,5 @@ impl IHuman for Human
println!("Hi kitty!");
self.cat.meow();
-
- let cow: Box<dyn ICow> = (self.cow_factory)(3);
-
- cow.moo();
}
}
diff --git a/examples/basic/animals/mod.rs b/examples/basic/animals/mod.rs
index 6511d17..5444978 100644
--- a/examples/basic/animals/mod.rs
+++ b/examples/basic/animals/mod.rs
@@ -1,4 +1,3 @@
pub mod cat;
-pub mod cow;
pub mod dog;
pub mod human;
diff --git a/examples/basic/bootstrap.rs b/examples/basic/bootstrap.rs
index a1a7b05..71ef713 100644
--- a/examples/basic/bootstrap.rs
+++ b/examples/basic/bootstrap.rs
@@ -2,13 +2,11 @@ use syrette::DIContainer;
// Concrete implementations
use crate::animals::cat::Cat;
-use crate::animals::cow::Cow;
use crate::animals::dog::Dog;
use crate::animals::human::Human;
//
// Interfaces
use crate::interfaces::cat::ICat;
-use crate::interfaces::cow::{CowFactory, ICow};
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
@@ -20,10 +18,5 @@ pub fn bootstrap() -> DIContainer
di_container.bind::<dyn ICat>().to::<Cat>();
di_container.bind::<dyn IHuman>().to::<Human>();
- di_container.bind::<CowFactory>().to_factory(&|moo_cnt| {
- let cow: Box<dyn ICow> = Box::new(Cow::new(moo_cnt));
- cow
- });
-
di_container
}
diff --git a/examples/basic/interfaces/cow.rs b/examples/basic/interfaces/cow.rs
deleted file mode 100644
index 59ce7b1..0000000
--- a/examples/basic/interfaces/cow.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-use syrette::factory;
-use syrette::interfaces::factory::IFactory;
-
-pub trait ICow
-{
- fn moo(&self);
-}
-
-#[factory]
-pub type CowFactory = dyn IFactory<(i32,), dyn ICow>;
diff --git a/examples/basic/interfaces/mod.rs b/examples/basic/interfaces/mod.rs
index 6511d17..5444978 100644
--- a/examples/basic/interfaces/mod.rs
+++ b/examples/basic/interfaces/mod.rs
@@ -1,4 +1,3 @@
pub mod cat;
-pub mod cow;
pub mod dog;
pub mod human;
diff --git a/examples/factory/bootstrap.rs b/examples/factory/bootstrap.rs
new file mode 100644
index 0000000..5086b1a
--- /dev/null
+++ b/examples/factory/bootstrap.rs
@@ -0,0 +1,24 @@
+use syrette::ptr::InterfacePtr;
+use syrette::DIContainer;
+
+// Interfaces
+use crate::interfaces::user::{IUser, IUserFactory};
+//
+// Concrete implementations
+use crate::user::User;
+
+pub fn bootstrap() -> DIContainer
+{
+ let mut di_container: DIContainer = DIContainer::new();
+
+ di_container
+ .bind::<IUserFactory>()
+ .to_factory(&|name, date_of_birth, password| {
+ let user: InterfacePtr<dyn IUser> =
+ InterfacePtr::new(User::new(name, date_of_birth, password));
+
+ user
+ });
+
+ di_container
+}
diff --git a/examples/factory/interfaces/mod.rs b/examples/factory/interfaces/mod.rs
new file mode 100644
index 0000000..22d12a3
--- /dev/null
+++ b/examples/factory/interfaces/mod.rs
@@ -0,0 +1 @@
+pub mod user;
diff --git a/examples/factory/interfaces/user.rs b/examples/factory/interfaces/user.rs
new file mode 100644
index 0000000..70cd632
--- /dev/null
+++ b/examples/factory/interfaces/user.rs
@@ -0,0 +1,13 @@
+use syrette::factory;
+use syrette::interfaces::factory::IFactory;
+
+pub trait IUser
+{
+ fn get_name(&self) -> &'static str;
+ fn get_date_of_birth(&self) -> &'static str;
+ fn get_password(&self) -> &'static str;
+}
+
+#[factory]
+pub type IUserFactory =
+ dyn IFactory<(&'static str, &'static str, &'static str), dyn IUser>;
diff --git a/examples/factory/main.rs b/examples/factory/main.rs
new file mode 100644
index 0000000..c659f3e
--- /dev/null
+++ b/examples/factory/main.rs
@@ -0,0 +1,47 @@
+#![deny(clippy::all)]
+#![deny(clippy::pedantic)]
+#![allow(clippy::module_name_repetitions)]
+
+mod bootstrap;
+mod interfaces;
+mod user;
+
+use bootstrap::bootstrap;
+use interfaces::user::IUser;
+use interfaces::user::IUserFactory;
+use syrette::ptr::FactoryPtr;
+use syrette::ptr::InterfacePtr;
+
+fn add_users(
+ users: &mut Vec<InterfacePtr<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"));
+}
+
+fn main()
+{
+ println!("Hello, world!");
+
+ let di_container = bootstrap();
+
+ let user_factory = di_container.get_factory::<IUserFactory>().unwrap();
+
+ let mut users = Vec::<InterfacePtr<dyn IUser>>::new();
+
+ add_users(&mut users, &user_factory);
+
+ println!("Printing user information");
+
+ for user in users {
+ println!(
+ "{}, born {}, password is '{}'",
+ user.get_name(),
+ user.get_date_of_birth(),
+ user.get_password()
+ );
+ }
+}
diff --git a/examples/factory/user.rs b/examples/factory/user.rs
new file mode 100644
index 0000000..121ad25
--- /dev/null
+++ b/examples/factory/user.rs
@@ -0,0 +1,42 @@
+use crate::interfaces::user::IUser;
+
+pub struct User
+{
+ name: &'static str,
+ date_of_birth: &'static str,
+ password: &'static str,
+}
+
+impl User
+{
+ pub fn new(
+ name: &'static str,
+ date_of_birth: &'static str,
+ password: &'static str,
+ ) -> Self
+ {
+ Self {
+ name,
+ date_of_birth,
+ password,
+ }
+ }
+}
+
+impl IUser for User
+{
+ fn get_name(&self) -> &'static str
+ {
+ self.name.clone()
+ }
+
+ fn get_date_of_birth(&self) -> &'static str
+ {
+ self.date_of_birth.clone()
+ }
+
+ fn get_password(&self) -> &'static str
+ {
+ self.password.clone()
+ }
+}