Skip to main content

synap_sdk/
set.rs

1//! Set data structure operations
2
3use crate::client::SynapClient;
4use crate::error::Result;
5use serde_json::json;
6
7/// Set data structure interface (Redis-compatible)
8///
9/// Set is a collection of unique strings with set algebra operations.
10#[derive(Clone)]
11pub struct SetManager {
12    client: SynapClient,
13}
14
15impl SetManager {
16    /// Create a new Set manager interface
17    pub(crate) fn new(client: SynapClient) -> Self {
18        Self { client }
19    }
20
21    /// Add members to set
22    pub async fn add<K>(&self, key: K, members: Vec<String>) -> Result<usize>
23    where
24        K: AsRef<str>,
25    {
26        let payload = json!({
27            "key": key.as_ref(),
28            "members": members,
29        });
30
31        let response = self.client.send_command("set.add", payload).await?;
32        Ok(response.get("added").and_then(|v| v.as_u64()).unwrap_or(0) as usize)
33    }
34
35    /// Remove members from set
36    pub async fn rem<K>(&self, key: K, members: Vec<String>) -> Result<usize>
37    where
38        K: AsRef<str>,
39    {
40        let payload = json!({
41            "key": key.as_ref(),
42            "members": members,
43        });
44
45        let response = self.client.send_command("set.rem", payload).await?;
46        Ok(response
47            .get("removed")
48            .and_then(|v| v.as_u64())
49            .unwrap_or(0) as usize)
50    }
51
52    /// Check if member exists in set
53    pub async fn is_member<K>(&self, key: K, member: String) -> Result<bool>
54    where
55        K: AsRef<str>,
56    {
57        let payload = json!({
58            "key": key.as_ref(),
59            "member": member,
60        });
61
62        let response = self.client.send_command("set.ismember", payload).await?;
63        Ok(response
64            .get("is_member")
65            .and_then(|v| v.as_bool())
66            .unwrap_or(false))
67    }
68
69    /// Get all members of set
70    pub async fn members<K>(&self, key: K) -> Result<Vec<String>>
71    where
72        K: AsRef<str>,
73    {
74        let payload = json!({"key": key.as_ref()});
75        let response = self.client.send_command("set.members", payload).await?;
76
77        let members = response
78            .get("members")
79            .and_then(|v| serde_json::from_value(v.clone()).ok())
80            .unwrap_or_default();
81
82        Ok(members)
83    }
84
85    /// Get set cardinality (size)
86    pub async fn card<K>(&self, key: K) -> Result<usize>
87    where
88        K: AsRef<str>,
89    {
90        let payload = json!({"key": key.as_ref()});
91        let response = self.client.send_command("set.card", payload).await?;
92        Ok(response
93            .get("cardinality")
94            .and_then(|v| v.as_u64())
95            .unwrap_or(0) as usize)
96    }
97
98    /// Remove and return random members
99    pub async fn pop<K>(&self, key: K, count: usize) -> Result<Vec<String>>
100    where
101        K: AsRef<str>,
102    {
103        let payload = json!({
104            "key": key.as_ref(),
105            "count": count,
106        });
107
108        let response = self.client.send_command("set.pop", payload).await?;
109        let members = response
110            .get("members")
111            .and_then(|v| serde_json::from_value(v.clone()).ok())
112            .unwrap_or_default();
113
114        Ok(members)
115    }
116
117    /// Get random members without removing
118    pub async fn rand_member<K>(&self, key: K, count: usize) -> Result<Vec<String>>
119    where
120        K: AsRef<str>,
121    {
122        let payload = json!({
123            "key": key.as_ref(),
124            "count": count,
125        });
126
127        let response = self.client.send_command("set.randmember", payload).await?;
128        let members = response
129            .get("members")
130            .and_then(|v| serde_json::from_value(v.clone()).ok())
131            .unwrap_or_default();
132
133        Ok(members)
134    }
135
136    /// Move member from source to destination set
137    pub async fn r#move<S, D>(&self, source: S, destination: D, member: String) -> Result<bool>
138    where
139        S: AsRef<str>,
140        D: AsRef<str>,
141    {
142        let payload = json!({
143            "source": source.as_ref(),
144            "destination": destination.as_ref(),
145            "member": member,
146        });
147
148        let response = self.client.send_command("set.move", payload).await?;
149        Ok(response
150            .get("moved")
151            .and_then(|v| v.as_bool())
152            .unwrap_or(false))
153    }
154
155    /// Get intersection of sets
156    pub async fn inter(&self, keys: Vec<String>) -> Result<Vec<String>> {
157        let payload = json!({"keys": keys});
158        let response = self.client.send_command("set.inter", payload).await?;
159
160        let members = response
161            .get("members")
162            .and_then(|v| serde_json::from_value(v.clone()).ok())
163            .unwrap_or_default();
164
165        Ok(members)
166    }
167
168    /// Get union of sets
169    pub async fn union(&self, keys: Vec<String>) -> Result<Vec<String>> {
170        let payload = json!({"keys": keys});
171        let response = self.client.send_command("set.union", payload).await?;
172
173        let members = response
174            .get("members")
175            .and_then(|v| serde_json::from_value(v.clone()).ok())
176            .unwrap_or_default();
177
178        Ok(members)
179    }
180
181    /// Get difference of sets (first minus others)
182    pub async fn diff(&self, keys: Vec<String>) -> Result<Vec<String>> {
183        let payload = json!({"keys": keys});
184        let response = self.client.send_command("set.diff", payload).await?;
185
186        let members = response
187            .get("members")
188            .and_then(|v| serde_json::from_value(v.clone()).ok())
189            .unwrap_or_default();
190
191        Ok(members)
192    }
193
194    /// Store intersection result in destination
195    pub async fn inter_store<D>(&self, destination: D, keys: Vec<String>) -> Result<usize>
196    where
197        D: AsRef<str>,
198    {
199        let payload = json!({
200            "destination": destination.as_ref(),
201            "keys": keys,
202        });
203
204        let response = self.client.send_command("set.interstore", payload).await?;
205        Ok(response
206            .get("cardinality")
207            .and_then(|v| v.as_u64())
208            .unwrap_or(0) as usize)
209    }
210
211    /// Store union result in destination
212    pub async fn union_store<D>(&self, destination: D, keys: Vec<String>) -> Result<usize>
213    where
214        D: AsRef<str>,
215    {
216        let payload = json!({
217            "destination": destination.as_ref(),
218            "keys": keys,
219        });
220
221        let response = self.client.send_command("set.unionstore", payload).await?;
222        Ok(response
223            .get("cardinality")
224            .and_then(|v| v.as_u64())
225            .unwrap_or(0) as usize)
226    }
227
228    /// Store difference result in destination
229    pub async fn diff_store<D>(&self, destination: D, keys: Vec<String>) -> Result<usize>
230    where
231        D: AsRef<str>,
232    {
233        let payload = json!({
234            "destination": destination.as_ref(),
235            "keys": keys,
236        });
237
238        let response = self.client.send_command("set.diffstore", payload).await?;
239        Ok(response
240            .get("cardinality")
241            .and_then(|v| v.as_u64())
242            .unwrap_or(0) as usize)
243    }
244}