From ade21185976ea2324d313a5c28a88cc0492f2934 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 20 Aug 2022 17:10:08 +0200 Subject: docs: add a example that uses a 3rd party library --- Cargo.toml | 6 ++++ examples/with-3rd-party/bootstrap.rs | 30 ++++++++++++++++ examples/with-3rd-party/interfaces/mod.rs | 1 + examples/with-3rd-party/interfaces/ninja.rs | 4 +++ examples/with-3rd-party/main.rs | 42 ++++++++++++++++++++++ examples/with-3rd-party/ninja.rs | 26 ++++++++++++++ examples/with-3rd-party/third-party-lib/Cargo.toml | 6 ++++ examples/with-3rd-party/third-party-lib/src/lib.rs | 23 ++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 examples/with-3rd-party/bootstrap.rs create mode 100644 examples/with-3rd-party/interfaces/mod.rs create mode 100644 examples/with-3rd-party/interfaces/ninja.rs create mode 100644 examples/with-3rd-party/main.rs create mode 100644 examples/with-3rd-party/ninja.rs create mode 100644 examples/with-3rd-party/third-party-lib/Cargo.toml create mode 100644 examples/with-3rd-party/third-party-lib/src/lib.rs 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 +{ + let mut di_container: DIContainer = DIContainer::new(); + + di_container.bind::().to::()?; + + di_container + .bind::() + .to_default_factory(&|| { + let shuriken: TransientPtr = + 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::() + .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, +} + +#[injectable(INinja)] +impl Ninja +{ + pub fn new(shuriken: TransientPtr) -> 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!"); + } +} -- cgit v1.2.3-18-g5258