1use async_trait::async_trait;
6use axum::response::IntoResponse;
7use serde::{Deserialize, Serialize};
8use std::any::Any;
9use std::collections::HashMap;
10use std::fmt::Debug;
11use crate::errors::Result;
12
13pub trait Service: Send + Sync + Any {
15 fn name(&self) -> &'static str;
17
18 fn metadata(&self) -> HashMap<String, String> {
20 HashMap::new()
21 }
22}
23
24#[async_trait]
26pub trait Controller: Send + Sync {
27 fn name(&self) -> &'static str;
29
30 async fn init(&mut self) -> Result<()> {
32 Ok(())
33 }
34
35 async fn cleanup(&mut self) -> Result<()> {
37 Ok(())
38 }
39}
40
41pub trait Model: Send + Sync + Clone + Debug {
43 type PrimaryKey: Send + Sync + Clone + Debug;
45
46 fn table_name() -> &'static str;
48
49 fn primary_key() -> &'static str {
51 "id"
52 }
53
54 fn get_key(&self) -> Option<Self::PrimaryKey>;
56
57 fn set_key(&mut self, key: Self::PrimaryKey);
59
60 fn fillable() -> Vec<&'static str> {
62 Vec::new()
63 }
64
65 fn guarded() -> Vec<&'static str> {
67 Vec::new()
68 }
69
70 fn hidden() -> Vec<&'static str> {
72 Vec::new()
73 }
74
75 fn visible() -> Vec<&'static str> {
77 Vec::new()
78 }
79
80 fn validate(&self) -> Result<()> {
82 Ok(())
83 }
84}
85
86#[async_trait]
88pub trait Repository<T: Model + 'static>: Send + Sync {
89 async fn find(&self, id: T::PrimaryKey) -> Result<Option<T>>;
91
92 async fn all(&self) -> Result<Vec<T>>;
94
95 async fn create(&self, model: &T) -> Result<T>;
97
98 async fn update(&self, id: T::PrimaryKey, model: &T) -> Result<T>;
100
101 async fn delete(&self, id: T::PrimaryKey) -> Result<bool>;
103
104 async fn find_by(&self, criteria: HashMap<String, serde_json::Value>) -> Result<Vec<T>>;
106
107 async fn count(&self) -> Result<u64>;
109
110 async fn exists(&self, id: T::PrimaryKey) -> Result<bool> {
112 Ok(self.find(id).await?.is_some())
113 }
114}
115
116#[async_trait]
118pub trait HttpMiddleware: Send + Sync {
119 async fn before(&self, request: &mut axum::extract::Request) -> Result<()> {
121 Ok(())
122 }
123
124 async fn after(&self, response: &mut axum::response::Response) -> Result<()> {
126 Ok(())
127 }
128
129 fn name(&self) -> &'static str;
131
132 fn priority(&self) -> i32 {
134 100
135 }
136}
137
138pub trait ValidationRule: Send + Sync {
140 fn validate(&self, value: &serde_json::Value) -> Result<()>;
142
143 fn message(&self) -> String;
145
146 fn name(&self) -> &'static str;
148}
149
150#[async_trait]
152pub trait CacheStore: Send + Sync {
153 async fn get(&self, key: &str) -> Result<Option<String>>;
155
156 async fn set(&self, key: &str, value: &str, ttl: Option<u64>) -> Result<()>;
158
159 async fn delete(&self, key: &str) -> Result<bool>;
161
162 async fn exists(&self, key: &str) -> Result<bool>;
164
165 async fn clear(&self) -> Result<()>;
167
168 async fn get_many(&self, keys: &[&str]) -> Result<HashMap<String, String>>;
170
171 async fn set_many(&self, items: HashMap<String, String>, ttl: Option<u64>) -> Result<()>;
173
174 async fn increment(&self, key: &str, value: i64) -> Result<i64>;
176
177 async fn decrement(&self, key: &str, value: i64) -> Result<i64>;
179}
180
181#[async_trait]
183pub trait SessionStore: Send + Sync {
184 async fn get(&self, session_id: &str) -> Result<Option<HashMap<String, serde_json::Value>>>;
186
187 async fn set(&self, session_id: &str, data: HashMap<String, serde_json::Value>) -> Result<()>;
189
190 async fn delete(&self, session_id: &str) -> Result<bool>;
192
193 async fn touch(&self, session_id: &str) -> Result<()>;
195
196 async fn gc(&self, max_lifetime: u64) -> Result<()>;
198}
199
200#[async_trait]
202pub trait QueueDriver: Send + Sync {
203 type Job: Send + Sync + Serialize + for<'de> Deserialize<'de>;
205
206 async fn push(&self, queue: &str, job: Self::Job) -> Result<String>;
208
209 async fn pop(&self, queue: &str) -> Result<Option<(String, Self::Job)>>;
211
212 async fn ack(&self, job_id: &str) -> Result<()>;
214
215 async fn reject(&self, job_id: &str, requeue: bool) -> Result<()>;
217
218 async fn size(&self, queue: &str) -> Result<u64>;
220
221 async fn purge(&self, queue: &str) -> Result<()>;
223}
224
225#[async_trait]
227pub trait EventListener: Send + Sync {
228 type Event: Send + Sync;
230
231 async fn handle(&self, event: Self::Event) -> Result<()>;
233
234 fn name(&self) -> &'static str;
236
237 fn is_sync(&self) -> bool {
239 false
240 }
241}
242
243pub trait Transformer<T>: Send + Sync {
245 type Output: Serialize;
247
248 fn transform(&self, resource: T) -> Self::Output;
250
251 fn name(&self) -> &'static str;
253}
254
255#[async_trait]
257pub trait FormRequest: Send + Sync {
258 type Data: for<'de> Deserialize<'de>;
260
261 async fn authorize(&self) -> Result<bool> {
263 Ok(true)
264 }
265
266 fn rules(&self) -> HashMap<String, Vec<Box<dyn ValidationRule>>>;
268
269 fn messages(&self) -> HashMap<String, String> {
271 HashMap::new()
272 }
273
274 async fn validate(&self, data: &Self::Data) -> Result<()>;
276}
277
278pub trait ResourceCollection<T>: Send + Sync {
280 type Item: Serialize;
282
283 fn collect(&self, items: Vec<T>) -> Vec<Self::Item>;
285
286 fn with(&self) -> HashMap<String, serde_json::Value> {
288 HashMap::new()
289 }
290
291 fn meta(&self) -> HashMap<String, serde_json::Value> {
293 HashMap::new()
294 }
295}
296
297#[async_trait]
299pub trait AuthGuard: Send + Sync {
300 type User: Send + Sync;
302
303 async fn authenticate(&self, request: &axum::extract::Request) -> Result<Option<Self::User>>;
305
306 fn name(&self) -> &'static str;
308}
309
310#[async_trait]
312pub trait Policy<T>: Send + Sync {
313 type User: Send + Sync;
315
316 async fn view(&self, user: &Self::User, resource: &T) -> Result<bool> {
318 Ok(false)
319 }
320
321 async fn create(&self, user: &Self::User) -> Result<bool> {
323 Ok(false)
324 }
325
326 async fn update(&self, user: &Self::User, resource: &T) -> Result<bool> {
328 Ok(false)
329 }
330
331 async fn delete(&self, user: &Self::User, resource: &T) -> Result<bool> {
333 Ok(false)
334 }
335
336 fn name(&self) -> &'static str;
338}
339
340#[async_trait]
342pub trait CommandHandler: Send + Sync {
343 type Args: Send + Sync;
345
346 async fn handle(&self, args: Self::Args) -> Result<()>;
348
349 fn name(&self) -> &'static str;
351
352 fn description(&self) -> &'static str {
354 ""
355 }
356
357 fn signature(&self) -> &'static str {
359 ""
360 }
361}
362
363pub trait ResponseFactory: Send + Sync {
365 fn success<T: Serialize>(&self, data: T) -> impl IntoResponse;
367
368 fn error(&self, message: &str, status: axum::http::StatusCode) -> impl IntoResponse;
370
371 fn paginated<T: Serialize>(&self, data: Vec<T>, total: u64, page: u64, per_page: u64) -> impl IntoResponse;
373}
374
375#[macro_export]
377macro_rules! impl_service {
378 ($type:ty, $name:expr) => {
379 impl $crate::traits::Service for $type {
380 fn name(&self) -> &'static str {
381 $name
382 }
383 }
384 };
385}
386
387#[macro_export]
389macro_rules! impl_model {
390 ($type:ty, $pk_type:ty, $table:expr) => {
391 impl $crate::traits::Model for $type {
392 type PrimaryKey = $pk_type;
393
394 fn table_name() -> &'static str {
395 $table
396 }
397
398 fn get_key(&self) -> Option<Self::PrimaryKey> {
399 self.id.clone()
400 }
401
402 fn set_key(&mut self, key: Self::PrimaryKey) {
403 self.id = Some(key);
404 }
405 }
406 };
407}