sonic_channel/
misc.rs

1/// Search data destination. Contains collection, bucket and object.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub struct ObjDest(Dest, String);
4
5impl ObjDest {
6    /// Creates a new object destination from base destination (`Dest`) and object id.
7    ///
8    /// ```rust
9    /// # use sonic_channel::{Dest, ObjDest};
10    /// let base_dest = Dest::col_buc("wiki", "user:1");
11    /// let dest = ObjDest::new(base_dest, "article:1");
12    /// assert_eq!(dest.collection(), "wiki");
13    /// assert_eq!(dest.bucket_opt(), Some(&String::from("user:1")));
14    /// assert_eq!(dest.object(), "article:1");
15    /// ```
16    pub fn new(cb: Dest, o: impl ToString) -> Self {
17        Self(cb, o.to_string())
18    }
19
20    /// Returns the collection.
21    #[inline]
22    pub fn collection(&self) -> &String {
23        self.0.collection()
24    }
25
26    /// Returns the optional bucket.
27    #[inline]
28    pub fn bucket_opt(&self) -> Option<&String> {
29        self.0.bucket_opt()
30    }
31
32    /// Returns the object id.
33    #[inline]
34    pub fn object(&self) -> &String {
35        &self.1
36    }
37}
38
39/// Search objects destination. Contains collection and bucket.
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Dest {
42    collection: String,
43    bucket: Option<String>,
44}
45
46impl Dest {
47    /// Creates a new destination with collection and bucket.
48    ///
49    /// ```rust
50    /// # use sonic_channel::Dest;
51    /// let dest = Dest::col_buc("wiki", "user:1");
52    /// assert_eq!(dest.collection(), "wiki");
53    /// assert_eq!(dest.bucket_opt(), Some(&String::from("user:1")));
54    /// ```
55    pub fn col_buc(c: impl ToString, b: impl ToString) -> Self {
56        Self::col(c).buc(b)
57    }
58
59    /// Creates a new destination with collection.
60    ///
61    /// ```rust
62    /// # use sonic_channel::Dest;
63    /// let dest = Dest::col("wiki");
64    /// assert_eq!(dest.collection(), "wiki");
65    /// ```
66    pub fn col(c: impl ToString) -> Self {
67        Self {
68            collection: c.to_string(),
69            bucket: None,
70        }
71    }
72
73    /// Set bucket for the destination.
74    ///
75    /// ```rust
76    /// # use sonic_channel::Dest;
77    /// let dest = Dest::col("wiki").buc("user:1");
78    /// assert_eq!(dest.collection(), "wiki");
79    /// assert_eq!(dest.bucket_opt(), Some(&String::from("user:1")));
80    /// ```
81    pub fn buc(mut self, b: impl ToString) -> Self {
82        self.bucket = Some(b.to_string());
83        self
84    }
85
86    /// Set object id to the destination and transform to object destination (`ObjDest`).
87    ///
88    /// Short for `ObjDest::new(dest, object_id)`
89    ///
90    /// ```rust
91    /// # use sonic_channel::Dest;
92    /// let dest = Dest::col_buc("wiki", "user:1").obj("article:1");
93    /// assert_eq!(dest.collection(), "wiki");
94    /// assert_eq!(dest.bucket_opt(), Some(&String::from("user:1")));
95    /// assert_eq!(dest.object(), "article:1");
96    /// ```
97    pub fn obj(self, o: impl ToString) -> ObjDest {
98        ObjDest::new(self, o)
99    }
100
101    /// Returns the collection.
102    #[inline]
103    pub fn collection(&self) -> &String {
104        &self.collection
105    }
106
107    /// Returns the optional bucket.
108    #[inline]
109    pub fn bucket_opt(&self) -> Option<&String> {
110        self.bucket.as_ref()
111    }
112}
113
114#[cfg(feature = "ingest")]
115#[derive(Debug)]
116pub(crate) struct OptDest {
117    pub(crate) collection: String,
118    pub(crate) bucket: Option<String>,
119    pub(crate) object: Option<String>,
120}
121
122#[cfg(feature = "ingest")]
123impl OptDest {
124    pub(crate) fn col(c: impl ToString) -> Self {
125        Self {
126            collection: c.to_string(),
127            bucket: None,
128            object: None,
129        }
130    }
131
132    pub(crate) fn col_buc(c: impl ToString, b: impl ToString) -> Self {
133        Self {
134            collection: c.to_string(),
135            bucket: Some(b.to_string()),
136            object: None,
137        }
138    }
139
140    pub(crate) fn col_buc_obj(c: impl ToString, b: impl ToString, o: impl ToString) -> Self {
141        Self {
142            collection: c.to_string(),
143            bucket: Some(b.to_string()),
144            object: Some(o.to_string()),
145        }
146    }
147}
148
149#[cfg(feature = "ingest")]
150impl From<Dest> for OptDest {
151    fn from(d: Dest) -> Self {
152        Self {
153            collection: d.collection,
154            bucket: d.bucket,
155            object: None,
156        }
157    }
158}
159
160#[cfg(feature = "ingest")]
161impl From<ObjDest> for OptDest {
162    fn from(ObjDest(dest, obj): ObjDest) -> Self {
163        Self {
164            collection: dest.collection,
165            bucket: dest.bucket,
166            object: Some(obj),
167        }
168    }
169}