mddd_traits/
traits.rs

1pub trait IUseCaseRequest: Sized {
2    fn build(src: &[&str]) -> Result<Self, String>;
3    fn help() -> String;
4}
5
6pub trait IConfig: Sized {
7    fn build() -> Result<Self, String>;
8    fn help() -> String;
9}
10
11pub trait IUseCase {
12    type Request;
13    type Error;
14    fn short_description() -> String;
15    fn description(&self) -> String {
16        Self::short_description()
17    }
18    fn execute(&mut self, request: Self::Request) -> Result<(), Self::Error>;
19}
20
21pub trait IParser<T> {
22    fn parse(src: &str) -> Result<T, String>;
23}
24
25pub trait IRunner {
26    fn short_description(&self) -> String;
27    fn execute(&mut self, prefix: &str, args: &[&str]);
28}
29
30impl IUseCaseRequest for () {
31    fn build(src: &[&str]) -> Result<Self, String> {
32        if src.is_empty() {
33            Ok(())
34        } else {
35            Err("No args expected".to_string())
36        }
37    }
38
39    fn help() -> String {
40        "<Nothing>".to_string()
41    }
42}
43
44pub trait CacheParam {
45    type CacheType: Eq;
46    fn to_cache_type(&self) -> Self::CacheType;
47}
48
49macro_rules! impl_clone {
50    ($tp:ty) => {
51        impl CacheParam for $tp {
52            type CacheType = $tp;
53            fn to_cache_type(&self) -> $tp {
54                self.clone()
55            }
56        }
57    };
58}
59
60impl_clone!(usize);
61impl_clone!(isize);
62impl_clone!(u64);
63impl_clone!(i64);
64impl_clone!(u32);
65impl_clone!(i32);
66impl_clone!(u16);
67impl_clone!(i16);
68impl_clone!(u8);
69impl_clone!(i8);
70impl_clone!(char);
71impl_clone!(String);
72
73impl<T: CacheParam> CacheParam for &T {
74    type CacheType = T::CacheType;
75
76    fn to_cache_type(&self) -> T::CacheType {
77        (**self).to_cache_type()
78    }
79}
80
81impl CacheParam for &str {
82    type CacheType = String;
83
84    fn to_cache_type(&self) -> Self::CacheType {
85        self.to_string()
86    }
87}
88
89impl<T: CacheParam> CacheParam for &[T] {
90    type CacheType = Vec<T::CacheType>;
91
92    fn to_cache_type(&self) -> Self::CacheType {
93        self.iter().map(|i| i.to_cache_type()).collect()
94    }
95}
96
97impl<T: CacheParam> CacheParam for Vec<T> {
98    type CacheType = Vec<T::CacheType>;
99
100    fn to_cache_type(&self) -> Self::CacheType {
101        self.iter().map(|i| i.to_cache_type()).collect()
102    }
103}
104
105impl<A: CacheParam, B: CacheParam> CacheParam for (A, B) {
106    type CacheType = (A::CacheType, B::CacheType);
107
108    fn to_cache_type(&self) -> Self::CacheType {
109        (self.0.to_cache_type(), self.1.to_cache_type())
110    }
111}
112
113impl<A: CacheParam, B: CacheParam, C: CacheParam> CacheParam for (A, B, C) {
114    type CacheType = (A::CacheType, B::CacheType, C::CacheType);
115
116    fn to_cache_type(&self) -> Self::CacheType {
117        (
118            self.0.to_cache_type(),
119            self.1.to_cache_type(),
120            self.2.to_cache_type(),
121        )
122    }
123}
124
125impl<A: CacheParam, B: CacheParam, C: CacheParam, D: CacheParam> CacheParam
126for (A, B, C, D)
127{
128    type CacheType = (A::CacheType, B::CacheType, C::CacheType, D::CacheType);
129
130    fn to_cache_type(&self) -> Self::CacheType {
131        (
132            self.0.to_cache_type(),
133            self.1.to_cache_type(),
134            self.2.to_cache_type(),
135            self.3.to_cache_type(),
136        )
137    }
138}
139
140impl<
141    A: CacheParam,
142    B: CacheParam,
143    C: CacheParam,
144    D: CacheParam,
145    E: CacheParam,
146> CacheParam for (A, B, C, D, E)
147{
148    type CacheType = (
149        A::CacheType,
150        B::CacheType,
151        C::CacheType,
152        D::CacheType,
153        E::CacheType,
154    );
155
156    fn to_cache_type(&self) -> Self::CacheType {
157        (
158            self.0.to_cache_type(),
159            self.1.to_cache_type(),
160            self.2.to_cache_type(),
161            self.3.to_cache_type(),
162            self.4.to_cache_type(),
163        )
164    }
165}