cloacina 0.4.0

A Rust library for resilient task execution and orchestration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *  Copyright 2025-2026 Colliery Software
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

use pyo3::prelude::*;
use pyo3::types::PyDict;
use pythonize::{depythonize, pythonize};
use serde_json;

/// PyContext - Python wrapper for Rust Context<serde_json::Value>
#[pyclass(name = "Context")]
#[derive(Debug)]
pub struct PyContext {
    pub inner: crate::Context<serde_json::Value>,
}

#[pymethods]
impl PyContext {
    /// Creates a new empty context
    #[new]
    #[pyo3(signature = (data = None))]
    pub fn new(data: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
        let mut context = crate::Context::new();

        if let Some(dict) = data {
            for (key, value) in dict.iter() {
                let key_str: String = key.extract()?;
                let json_value: serde_json::Value = depythonize(&value)?;
                context.insert(key_str, json_value).map_err(|e| {
                    PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                        "Failed to insert key: {}",
                        e
                    ))
                })?;
            }
        }

        Ok(PyContext { inner: context })
    }

    /// Gets a value from the context
    #[pyo3(signature = (key, default = None))]
    pub fn get(&self, key: &str, default: Option<&Bound<'_, PyAny>>) -> PyResult<PyObject> {
        match self.inner.get(key) {
            Some(value) => Python::with_gil(|py| Ok(pythonize(py, value)?.into())),
            None => match default {
                Some(default_value) => Ok(default_value.clone().into()),
                None => Python::with_gil(|py| Ok(py.None())),
            },
        }
    }

    /// Sets a value in the context (insert or update)
    pub fn set(&mut self, key: &str, value: &Bound<'_, PyAny>) -> PyResult<()> {
        let json_value: serde_json::Value = depythonize(value)?;

        if self.inner.get(key).is_some() {
            self.inner.update(key, json_value)
        } else {
            self.inner.insert(key, json_value)
        }
        .map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                "Failed to set key '{}': {}",
                key, e
            ))
        })
    }

    /// Updates an existing value in the context
    pub fn update(&mut self, key: &str, value: &Bound<'_, PyAny>) -> PyResult<()> {
        let json_value: serde_json::Value = depythonize(value)?;
        self.inner.update(key, json_value).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyKeyError, _>(format!("Key not found: {}", e))
        })
    }

    /// Inserts a new value into the context
    pub fn insert(&mut self, key: &str, value: &Bound<'_, PyAny>) -> PyResult<()> {
        let json_value: serde_json::Value = depythonize(value)?;
        self.inner.insert(key, json_value).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("Key already exists: {}", e))
        })
    }

    /// Removes and returns a value from the context
    pub fn remove(&mut self, key: &str) -> PyResult<Option<PyObject>> {
        match self.inner.remove(key) {
            Some(value) => Python::with_gil(|py| Ok(Some(pythonize(py, &value)?.into()))),
            None => Ok(None),
        }
    }

    /// Returns the context as a Python dictionary
    pub fn to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
        Ok(pythonize(py, self.inner.data())?.into())
    }

    /// Updates the context with values from a Python dictionary
    pub fn update_from_dict(&mut self, data: &Bound<'_, PyDict>) -> PyResult<()> {
        for (key, value) in data.iter() {
            let key_str: String = key.extract()?;
            let json_value: serde_json::Value = depythonize(&value)?;

            if self.inner.get(&key_str).is_some() {
                self.inner.update(key_str, json_value)
            } else {
                self.inner.insert(key_str, json_value)
            }
            .map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                    "Failed to update from dict: {}",
                    e
                ))
            })?;
        }
        Ok(())
    }

    /// Serializes the context to a JSON string
    pub fn to_json(&self) -> PyResult<String> {
        self.inner.to_json().map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                "Failed to serialize to JSON: {}",
                e
            ))
        })
    }

    /// Creates a context from a JSON string
    #[staticmethod]
    pub fn from_json(json_str: &str) -> PyResult<Self> {
        let context = crate::Context::from_json(json_str.to_string()).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                "Failed to deserialize from JSON: {}",
                e
            ))
        })?;
        Ok(PyContext { inner: context })
    }

    /// Returns the number of key-value pairs in the context
    pub fn __len__(&self) -> usize {
        self.inner.data().len()
    }

    /// Checks if a key exists in the context
    pub fn __contains__(&self, key: &str) -> bool {
        self.inner.get(key).is_some()
    }

    /// String representation of the context
    pub fn __repr__(&self) -> String {
        match self.inner.to_json() {
            Ok(json) => format!("Context({})", json),
            Err(_) => "Context(<serialization error>)".to_string(),
        }
    }

    /// Dictionary-style item access
    pub fn __getitem__(&self, key: &str) -> PyResult<PyObject> {
        let result = self.get(key, None)?;
        Python::with_gil(|py| {
            if result.is_none(py) {
                Err(PyErr::new::<pyo3::exceptions::PyKeyError, _>(format!(
                    "Key not found: '{}'",
                    key
                )))
            } else {
                Ok(result)
            }
        })
    }

    /// Dictionary-style item assignment
    pub fn __setitem__(&mut self, key: &str, value: &Bound<'_, PyAny>) -> PyResult<()> {
        self.set(key, value)
    }

    /// Dictionary-style item deletion
    pub fn __delitem__(&mut self, key: &str) -> PyResult<()> {
        match self.remove(key)? {
            Some(_) => Ok(()),
            None => Err(PyErr::new::<pyo3::exceptions::PyKeyError, _>(format!(
                "Key not found: '{}'",
                key
            ))),
        }
    }
}

impl PyContext {
    /// Create a PyContext from a Rust Context (for internal use)
    pub fn from_rust_context(context: crate::Context<serde_json::Value>) -> Self {
        PyContext { inner: context }
    }

    /// Extract the inner Rust Context (for internal use)
    pub fn into_inner(self) -> crate::Context<serde_json::Value> {
        self.inner
    }

    /// Clone the inner Rust Context (for internal use)
    pub fn clone_inner(&self) -> crate::Context<serde_json::Value> {
        self.inner.clone_data()
    }

    /// Get a clone of the context data as a HashMap (for internal use)
    pub fn get_data_clone(&self) -> std::collections::HashMap<String, serde_json::Value> {
        self.inner.data().clone()
    }
}

/// Manual implementation of Clone since Context<T> doesn't implement Clone
impl Clone for PyContext {
    fn clone(&self) -> Self {
        let data = self.inner.data();
        let mut new_context = crate::Context::new();
        for (key, value) in data.iter() {
            // Insert should never fail when cloning existing valid data,
            // but silently skip rather than panic if it does.
            let _ = new_context.insert(key.clone(), value.clone());
        }
        PyContext { inner: new_context }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pyo3::types::PyDict;

    #[test]
    fn test_new_empty() {
        pyo3::prepare_freethreaded_python();
        let ctx = PyContext::new(None).unwrap();
        assert_eq!(ctx.__len__(), 0);
    }

    #[test]
    fn test_new_from_dict() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let dict = PyDict::new(py);
            dict.set_item("key", "value").unwrap();
            let ctx = PyContext::new(Some(&dict.as_borrowed())).unwrap();
            assert_eq!(ctx.__len__(), 1);
            assert!(ctx.__contains__("key"));
        });
    }

    #[test]
    fn test_set_and_get() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = 42i64.into_pyobject(py).unwrap();
            ctx.set("answer", &val.as_borrowed()).unwrap();

            let result = ctx.get("answer", None).unwrap();
            let extracted: i64 = result.extract(py).unwrap();
            assert_eq!(extracted, 42);
        });
    }

    #[test]
    fn test_insert_new_key() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = "hello".into_pyobject(py).unwrap();
            ctx.insert("greeting", &val.as_borrowed()).unwrap();
            assert!(ctx.__contains__("greeting"));
        });
    }

    #[test]
    fn test_insert_duplicate_errors() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = "hello".into_pyobject(py).unwrap();
            ctx.insert("key", &val.as_borrowed()).unwrap();
            let val2 = "world".into_pyobject(py).unwrap();
            assert!(ctx.insert("key", &val2.as_borrowed()).is_err());
        });
    }

    #[test]
    fn test_update_existing_key() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = "hello".into_pyobject(py).unwrap();
            ctx.insert("key", &val.as_borrowed()).unwrap();
            let val2 = "world".into_pyobject(py).unwrap();
            ctx.update("key", &val2.as_borrowed()).unwrap();

            let result = ctx.get("key", None).unwrap();
            let extracted: String = result.extract(py).unwrap();
            assert_eq!(extracted, "world");
        });
    }

    #[test]
    fn test_update_missing_key_errors() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = "hello".into_pyobject(py).unwrap();
            assert!(ctx.update("missing", &val.as_borrowed()).is_err());
        });
    }

    #[test]
    fn test_remove_existing() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = 99i64.into_pyobject(py).unwrap();
            ctx.insert("num", &val.as_borrowed()).unwrap();

            let removed = ctx.remove("num").unwrap();
            assert!(removed.is_some());
            assert_eq!(ctx.__len__(), 0);
        });
    }

    #[test]
    fn test_remove_missing_returns_none() {
        pyo3::prepare_freethreaded_python();
        let mut ctx = PyContext::new(None).unwrap();
        let removed = ctx.remove("nope").unwrap();
        assert!(removed.is_none());
    }

    #[test]
    fn test_len_and_contains() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            assert_eq!(ctx.__len__(), 0);
            assert!(!ctx.__contains__("a"));

            let val = 1i64.into_pyobject(py).unwrap();
            ctx.insert("a", &val.as_borrowed()).unwrap();
            assert_eq!(ctx.__len__(), 1);
            assert!(ctx.__contains__("a"));
        });
    }

    #[test]
    fn test_to_json_and_from_json() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = "bar".into_pyobject(py).unwrap();
            ctx.insert("foo", &val.as_borrowed()).unwrap();

            let json = ctx.to_json().unwrap();
            let ctx2 = PyContext::from_json(&json).unwrap();
            assert_eq!(ctx2.__len__(), 1);
            assert!(ctx2.__contains__("foo"));

            let result = ctx2.get("foo", None).unwrap();
            let extracted: String = result.extract(py).unwrap();
            assert_eq!(extracted, "bar");
        });
    }

    #[test]
    fn test_to_dict() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = 42i64.into_pyobject(py).unwrap();
            ctx.insert("x", &val.as_borrowed()).unwrap();

            let dict_obj = ctx.to_dict(py).unwrap();
            let dict = dict_obj.downcast_bound::<PyDict>(py).unwrap();
            let x_val: i64 = dict.get_item("x").unwrap().unwrap().extract().unwrap();
            assert_eq!(x_val, 42);
        });
    }

    #[test]
    fn test_repr() {
        pyo3::prepare_freethreaded_python();
        let ctx = PyContext::new(None).unwrap();
        let repr = ctx.__repr__();
        assert!(repr.starts_with("Context("));
    }

    #[test]
    fn test_from_rust_context_and_clone_inner() {
        pyo3::prepare_freethreaded_python();
        let mut rust_ctx = crate::Context::new();
        rust_ctx
            .insert("k".to_string(), serde_json::json!("v"))
            .unwrap();
        let py_ctx = PyContext::from_rust_context(rust_ctx);
        assert!(py_ctx.__contains__("k"));

        let cloned = py_ctx.clone_inner();
        assert_eq!(cloned.get("k"), Some(&serde_json::json!("v")));
    }

    #[test]
    fn test_clone_preserves_data() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let mut ctx = PyContext::new(None).unwrap();
            let val = "hello".into_pyobject(py).unwrap();
            ctx.insert("key", &val.as_borrowed()).unwrap();

            let cloned = ctx.clone();
            assert_eq!(cloned.__len__(), 1);
            assert!(cloned.__contains__("key"));
        });
    }
}