aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-20 17:10:08 +0200
committerHampusM <hampus@hampusmat.com>2022-08-21 18:17:50 +0200
commitade21185976ea2324d313a5c28a88cc0492f2934 (patch)
treef217d55ad324160d8a9a099c562c8bc6cbb60c1f
parentb31422d48a600ccccb682567f5eb11fc0bca547c (diff)
docs: add a example that uses a 3rd party library
-rw-r--r--Cargo.toml6
-rw-r--r--examples/with-3rd-party/bootstrap.rs30
-rw-r--r--examples/with-3rd-party/interfaces/mod.rs1
-rw-r--r--examples/with-3rd-party/interfaces/ninja.rs4
-rw-r--r--examples/with-3rd-party/main.rs42
-rw-r--r--examples/with-3rd-party/ninja.rs26
-rw-r--r--examples/with-3rd-party/third-party-lib/Cargo.toml6
-rw-r--r--examples/with-3rd-party/third-party-lib/src/lib.rs23
8 files changed, 138 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 3fd751a..d49ab8c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,6 +20,10 @@ prevent-circular = ["syrette_macros/prevent-circular"]
name = "factory"
required-features = ["factory"]
+[[example]]
+name = "with-3rd-party"
+required-features = ["factory"]
+
[dependencies]
syrette_macros = { path = "./macros", version = "0.2.1" }
linkme = "0.3.0"
@@ -29,9 +33,11 @@ ahash = "0.7.6"
[dev_dependencies]
mockall = "0.11.1"
+third-party-lib = { path = "./examples/with-3rd-party/third-party-lib" }
[workspace]
members = [
"macros",
+ "examples/with-3rd-party/third-party-lib"
]
diff --git a/examples/with-3rd-party/bootstrap.rs b/examples/with-3rd-party/bootstrap.rs
new file mode 100644
index 0000000..e51c104
--- /dev/null
+++ b/examples/with-3rd-party/bootstrap.rs
@@ -0,0 +1,30 @@
+use syrette::errors::di_container::BindingBuilderError;
+use syrette::ptr::TransientPtr;
+use syrette::{declare_default_factory, DIContainer};
+use third_party_lib::{IShuriken, Shuriken};
+
+// Interfaces
+use crate::interfaces::ninja::INinja;
+//
+// Concrete implementations
+use crate::ninja::Ninja;
+
+declare_default_factory!(IShuriken);
+
+pub fn bootstrap() -> error_stack::Result<DIContainer, BindingBuilderError>
+{
+ let mut di_container: DIContainer = DIContainer::new();
+
+ di_container.bind::<dyn INinja>().to::<Ninja>()?;
+
+ di_container
+ .bind::<dyn IShuriken>()
+ .to_default_factory(&|| {
+ let shuriken: TransientPtr<dyn IShuriken> =
+ TransientPtr::new(Shuriken::new());
+
+ shuriken
+ })?;
+
+ Ok(di_container)
+}
diff --git a/examples/with-3rd-party/interfaces/mod.rs b/examples/with-3rd-party/interfaces/mod.rs
new file mode 100644
index 0000000..c060c34
--- /dev/null
+++ b/examples/with-3rd-party/interfaces/mod.rs
@@ -0,0 +1 @@
+pub mod ninja;
diff --git a/examples/with-3rd-party/interfaces/ninja.rs b/examples/with-3rd-party/interfaces/ninja.rs
new file mode 100644
index 0000000..c4e9a59
--- /dev/null
+++ b/examples/with-3rd-party/interfaces/ninja.rs
@@ -0,0 +1,4 @@
+pub trait INinja
+{
+ fn throw_shuriken(&self);
+}
diff --git a/examples/with-3rd-party/main.rs b/examples/with-3rd-party/main.rs
new file mode 100644
index 0000000..f615ff5
--- /dev/null
+++ b/examples/with-3rd-party/main.rs
@@ -0,0 +1,42 @@
+#![deny(clippy::all)]
+#![deny(clippy::pedantic)]
+#![allow(clippy::module_name_repetitions)]
+
+use std::fmt::Display;
+
+mod bootstrap;
+mod interfaces;
+mod ninja;
+
+use error_stack::{Context, ResultExt};
+
+use crate::bootstrap::bootstrap;
+use crate::interfaces::ninja::INinja;
+
+#[derive(Debug)]
+struct ApplicationError;
+
+impl Display for ApplicationError
+{
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
+ {
+ f.write_str("An application error has occurred")
+ }
+}
+
+impl Context for ApplicationError {}
+
+fn main() -> error_stack::Result<(), ApplicationError>
+{
+ println!("Hello, world!");
+
+ let di_container = bootstrap().change_context(ApplicationError)?;
+
+ let ninja = di_container
+ .get::<dyn INinja>()
+ .change_context(ApplicationError)?;
+
+ ninja.throw_shuriken();
+
+ Ok(())
+}
diff --git a/examples/with-3rd-party/ninja.rs b/examples/with-3rd-party/ninja.rs
new file mode 100644
index 0000000..945adf0
--- /dev/null
+++ b/examples/with-3rd-party/ninja.rs
@@ -0,0 +1,26 @@
+use syrette::{injectable, ptr::TransientPtr};
+use third_party_lib::IShuriken;
+
+use crate::interfaces::ninja::INinja;
+
+pub struct Ninja
+{
+ shuriken: TransientPtr<dyn IShuriken>,
+}
+
+#[injectable(INinja)]
+impl Ninja
+{
+ pub fn new(shuriken: TransientPtr<dyn IShuriken>) -> Self
+ {
+ Self { shuriken }
+ }
+}
+
+impl INinja for Ninja
+{
+ fn throw_shuriken(&self)
+ {
+ self.shuriken.throw();
+ }
+}
diff --git a/examples/with-3rd-party/third-party-lib/Cargo.toml b/examples/with-3rd-party/third-party-lib/Cargo.toml
new file mode 100644
index 0000000..37668c4
--- /dev/null
+++ b/examples/with-3rd-party/third-party-lib/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "third-party-lib"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/examples/with-3rd-party/third-party-lib/src/lib.rs b/examples/with-3rd-party/third-party-lib/src/lib.rs
new file mode 100644
index 0000000..f3b3ed3
--- /dev/null
+++ b/examples/with-3rd-party/third-party-lib/src/lib.rs
@@ -0,0 +1,23 @@
+pub trait IShuriken
+{
+ fn throw(&self);
+}
+
+pub struct Shuriken {}
+
+impl Shuriken
+{
+ #[allow(clippy::new_without_default)]
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl IShuriken for Shuriken
+{
+ fn throw(&self)
+ {
+ println!("Threw shuriken!");
+ }
+}