1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Fama provides a series of functionalities that makes it easy to layout the steps
//! require to accomplish a task.
//! Each step in the process is refer to as a pipe. The data or content is passed through
//! the "pipes". At any stage the flow can be stopped.
//!
//! This pattern is usually refer to as the "pipeline" pattern. It is very similar to the
//! middleware pattern.
//!
//! This implementation remove the responsibility of the current "pipe" calling the next pipe like
//! in the middleware patten. A "pipe" can apply it's changes/logic or stop the flow. It is the "pipeline"
//! that initiates the next call.
//!
//! The following example is illustrating a "New User" flow through the pipeline.
//!
//! ```rust
//!#  #![allow(dead_code)]
//!
//! #[tokio::main]
//! async fn main() {
//!   // A new user instance is being passed through the pipeline
//!   let new_user = fama::Pipeline::pass(NewUser::default()) // The input/content
//!       .through(ValidateUserName).await  // pipe "ValidateUserName" will stop the flow if the user does not have a "username"
//!       .through(GenerateUserId).await    // pipe "GenerateUserId" generates and set the user ID.  
//!       .through(ApplyDefaultRole).await  // pipe "ApplyDefaultRole" will give the user the "Basic" role if the list of roles is empty
//!       .through(SaveNewUserData).await   // pipe "SaveNewUserData"  saves the data to the database. At this stage, we know all is well
//!       .through_fn(|_pipe: fama::PipeContent| async {
//!           println!("yep, you can pass a closure or function too");
//!       }).await
//!       .deliver();                       // starts the process or use
//!       // .confirm()                     // Return true when the content passes throug all the pipes
//!
//!   // Fails because "new user" does not have a "username"
//!   println!("fails validation: {:#?}", &new_user);
//!
//!   println!("----------------------------------------");
//!
//!   let new_user2 = fama::Pipeline::pass(NewUser {
//!         username: Some("user1".into()),  // "new user" has a username
//!         ..NewUser::default()
//!     })
//!     .through(ValidateUserName).await
//!     .through(GenerateUserId).await
//!     .through(ApplyDefaultRole).await
//!     .through(SaveNewUserData).await
//!     .deliver();
//!
//!   println!(
//!         "passes validation and all fields are set : {:#?}",
//!         &new_user2
//!    );
//! }
//!
//! // The content or the pipeline input. Could be any type
//! #[derive(Debug, Clone)]
//! struct NewUser {
//!   internal_id: i32,
//!   id: Option<String>,
//!   username: Option<String>,
//!   role: Option<Vec<UserRole>>,
//! }
//!
//! impl Default for NewUser {
//!   fn default() -> Self {
//!       Self {
//!           internal_id: 0,
//!           id: None,
//!           username: None,
//!           role: None,
//!       }
//!   }
//! }
//!
//! // making the pipe input type injectable
//! #[fama::async_trait]
//! impl busybody::Injectable for NewUser {
//!  async fn inject(c: &busybody::ServiceContainer) -> Self {
//!     // get the instance of the type in the current scope or
//!     // create a new instance
//!     c.proxy_value().unwrap_or_else(|| Self::default())
//!  }
//! }
//!
//! // The various roles a user can have
//! #[derive(Debug, Clone)]
//! enum UserRole {
//!   Admin,
//!   ContentCreator,
//!   Moderator,
//!   Basic,
//! }
//!
//! struct ValidateUserName;
//!
//! // A struct becomes a pipe when it implements "fama::FamaPipe"
//! #[fama::async_trait]
//! impl fama::FamaPipe<(NewUser, fama::PipeContent), ()> for ValidateUserName {
//!
//!   // The only requirement is to implement "receive_pipe_content" method
//!    async fn receive_pipe_content(&self, (new_user, mut content): (NewUser, fama::PipeContent)) {
//!  
//1        // When the username is "none", stop the flow
//!        if new_user.username.is_none() {
//!            println!("User name cannot be empty");
//!            content.stop_the_flow(); // Stop the pipeline flow. Pipes below this pipe will not get call
//!        }
//!
//!   }
//! }
//!
//! struct GenerateUserId;
//!
//! #[fama::async_trait]
//! impl fama::FamaPipe<(NewUser, fama::PipeContent), Option<fama::PipeContent>> for GenerateUserId {
//!     async fn receive_pipe_content(&self, (mut new_user, content): (NewUser,fama::PipeContent)) -> Option<fama::PipeContent> {
//!
//!         if new_user.id.is_none() {
//!             new_user.id = Some(uuid::Uuid::new_v4().to_string()); // Generate and set the ID
//!             content.store(new_user);
//!         }
//!
//!       None
//!     }
//! }
//!
//! struct ApplyDefaultRole;
//!
//! #[fama::async_trait]
//! impl fama::FamaPipe<(NewUser, fama::PipeContent), Option<fama::PipeContent>> for ApplyDefaultRole {
//!     async fn receive_pipe_content(&self, (mut new_user, content): (NewUser,fama::PipeContent)) -> Option<fama::PipeContent> {
//!
//!         if new_user.role.is_none() {
//!             new_user.role = Some(vec![UserRole::Basic]); // Apply default role
//!             content.store(new_user);
//!        }
//!
//!         Some(content)
//!     }
//! }
//!
//! struct SaveNewUserData;
//! #[fama::async_trait]
//! impl fama::FamaPipe<(NewUser, fama::PipeContent), Option<fama::PipeContent>> for SaveNewUserData {
//!     async fn receive_pipe_content(&self, (mut new_user, content): (NewUser,fama::PipeContent)) -> Option<fama::PipeContent> {
//!
//!         println!(">> saving new user: {:?}", &new_user);
//!
//!         new_user.internal_id = 1; // pretend we persisted the data to a database
//!         content.store(new_user);
//!
//!         Some(content)
//!     }
//! }
//! ```
//!
mod content;
mod pipeline;

pub use content::PipeContent;
pub use pipeline::FamaPipe;
pub use pipeline::Pipeline;

pub use async_trait::async_trait;