fluvio_stream_model/
core.rs

1pub use core_model::*;
2pub use context::*;
3
4mod context {
5
6    use std::fmt;
7    use std::fmt::Display;
8    use std::collections::HashMap;
9
10    pub type DefaultMetadataContext = MetadataContext<String>;
11
12    pub trait MetadataItem:
13        Clone + Default + fmt::Debug + PartialEq + Send + Sync + 'static
14    {
15        type UId: PartialEq;
16
17        fn uid(&self) -> &Self::UId;
18
19        /// checkif item is newer
20        fn is_newer(&self, another: &Self) -> bool;
21
22        /// if object is process of being deleted
23        fn is_being_deleted(&self) -> bool {
24            false
25        }
26
27        /// set string labels
28        fn set_labels<T: Into<String>>(self, _labels: Vec<(T, T)>) -> Self {
29            self
30        }
31
32        /// get string labels
33        fn get_labels(&self) -> HashMap<String, String> {
34            HashMap::new()
35        }
36
37        fn owner(&self) -> Option<&Self> {
38            Default::default()
39        }
40
41        fn set_owner(&mut self, _owner: Self) {}
42
43        fn children(&self) -> Option<&HashMap<String, Vec<Self>>> {
44            Default::default()
45        }
46
47        fn set_children(&mut self, _children: HashMap<String, Vec<Self>>) {}
48    }
49
50    pub trait MetadataRevExtension: MetadataItem {
51        // return next revision
52        fn next_rev(&self) -> Self;
53    }
54
55    impl MetadataItem for u32 {
56        type UId = u32;
57
58        fn uid(&self) -> &Self::UId {
59            self
60        }
61
62        fn is_newer(&self, another: &Self) -> bool {
63            self > another
64        }
65    }
66
67    impl MetadataItem for u64 {
68        type UId = u64;
69
70        fn uid(&self) -> &Self::UId {
71            self
72        }
73
74        fn is_newer(&self, another: &Self) -> bool {
75            self > another
76        }
77    }
78
79    #[derive(Default, Debug, Clone, Eq, PartialEq)]
80    pub struct MetadataContext<C> {
81        item: C,
82    }
83
84    impl<C> From<C> for MetadataContext<C> {
85        fn from(item: C) -> Self {
86            Self { item }
87        }
88    }
89
90    impl<C> MetadataContext<C> {
91        pub fn new(item: C) -> Self {
92            Self::from(item)
93        }
94
95        pub fn item(&self) -> &C {
96            &self.item
97        }
98
99        pub fn item_mut(&mut self) -> &mut C {
100            &mut self.item
101        }
102
103        pub fn set_item(&mut self, item: C) {
104            self.item = item;
105        }
106
107        pub fn item_owned(self) -> C {
108            self.item
109        }
110
111        pub fn into_inner(self) -> C {
112            self.item
113        }
114    }
115
116    impl<C> MetadataContext<C>
117    where
118        C: MetadataItem,
119    {
120        pub fn create_child(&self) -> Self {
121            let mut item = C::default();
122            item.set_owner(self.item().clone());
123            Self { item }
124        }
125
126        pub fn set_labels<T: Into<String>>(mut self, labels: Vec<(T, T)>) -> Self {
127            let item = self.item.set_labels(labels);
128            self.item = item;
129            self
130        }
131    }
132
133    impl<C> MetadataContext<C>
134    where
135        C: MetadataRevExtension,
136    {
137        pub fn next_rev(&self) -> Self {
138            Self::new(self.item.next_rev())
139        }
140    }
141
142    impl<C> Display for MetadataContext<C>
143    where
144        C: MetadataItem,
145    {
146        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147            write!(f, "{:#?}", self.item)
148        }
149    }
150}
151
152mod core_model {
153
154    use std::fmt::{Debug, Display};
155    use std::hash::Hash;
156    use std::str::FromStr;
157
158    /// metadata driver
159    pub trait MetadataStoreDriver {
160        type Metadata;
161    }
162
163    #[cfg(not(feature = "use_serde"))]
164    pub trait Spec: Default + Debug + Clone + PartialEq + Send + Sync + 'static {
165        const LABEL: &'static str;
166        type Status: Status;
167        type Owner: Spec;
168        type IndexKey: Debug + Eq + Hash + Clone + ToString + FromStr + Display + Send + Sync;
169    }
170
171    #[cfg(feature = "use_serde")]
172    pub trait Spec:
173        Default
174        + Debug
175        + Clone
176        + PartialEq
177        + serde::Serialize
178        + serde::de::DeserializeOwned
179        + Send
180        + Sync
181        + 'static
182    {
183        const LABEL: &'static str;
184        type Status: Status + serde::Serialize + serde::de::DeserializeOwned;
185        type Owner: Spec;
186        type IndexKey: Debug + Eq + Hash + Clone + ToString + FromStr + Display + Send + Sync;
187    }
188
189    pub trait Status:
190        Default + Debug + Clone + ToString + Display + PartialEq + Send + Sync
191    {
192    }
193
194    /// for deleting objects
195    pub trait Removable {
196        type DeleteKey;
197    }
198
199    /// marker trait for creating
200    pub trait Creatable {}
201
202    /// Represents some metadata object
203    pub struct MetadataObj<S, P>
204    where
205        P: MetadataStoreDriver,
206        S: Spec,
207    {
208        pub name: String,
209        pub metadata: P::Metadata,
210        pub spec: S,
211        pub status: S::Status,
212    }
213}