pub struct Context<T>{ /* private fields */ }Expand description
A context that holds data for pipeline execution.
The context is a type-safe, serializable container that flows through your pipeline, allowing tasks to share data. It supports JSON serialization and provides key-value access patterns with comprehensive error handling.
§Type Parameter
T: The type of values stored in the context. Must implementSerialize,Deserialize, andDebug.
§Examples
use cloacina_workflow::Context;
use serde_json::Value;
// Create a context for JSON values
let mut context = Context::<Value>::new();
// Insert and retrieve data
context.insert("user_id", serde_json::json!(123)).unwrap();
let user_id = context.get("user_id").unwrap();Implementations§
Source§impl<T> Context<T>
impl<T> Context<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty context.
§Examples
use cloacina_workflow::Context;
let context = Context::<i32>::new();
assert!(context.get("any_key").is_none());Sourcepub fn clone_data(&self) -> Selfwhere
T: Clone,
pub fn clone_data(&self) -> Selfwhere
T: Clone,
Creates a clone of this context’s data.
§Performance
- Time complexity: O(n) where n is the number of key-value pairs
- Space complexity: O(n) for the cloned data
Sourcepub fn insert(
&mut self,
key: impl Into<String>,
value: T,
) -> Result<(), ContextError>
pub fn insert( &mut self, key: impl Into<String>, value: T, ) -> Result<(), ContextError>
Inserts a value into the context.
§Arguments
key- The key to insert (can be any type that converts to String)value- The value to store
§Returns
Ok(())- If the insertion was successfulErr(ContextError::KeyExists)- If the key already exists
§Examples
use cloacina_workflow::{Context, ContextError};
let mut context = Context::<i32>::new();
// First insertion succeeds
assert!(context.insert("count", 42).is_ok());
// Duplicate insertion fails
assert!(matches!(context.insert("count", 43), Err(ContextError::KeyExists(_))));Sourcepub fn update(
&mut self,
key: impl Into<String>,
value: T,
) -> Result<(), ContextError>
pub fn update( &mut self, key: impl Into<String>, value: T, ) -> Result<(), ContextError>
Updates an existing value in the context.
§Arguments
key- The key to updatevalue- The new value
§Returns
Ok(())- If the update was successfulErr(ContextError::KeyNotFound)- If the key doesn’t exist
§Examples
use cloacina_workflow::{Context, ContextError};
let mut context = Context::<i32>::new();
context.insert("count", 42).unwrap();
// Update existing key
assert!(context.update("count", 100).is_ok());
assert_eq!(context.get("count"), Some(&100));
// Update non-existent key fails
assert!(matches!(context.update("missing", 1), Err(ContextError::KeyNotFound(_))));Sourcepub fn get(&self, key: &str) -> Option<&T>
pub fn get(&self, key: &str) -> Option<&T>
Gets a reference to a value from the context.
§Arguments
key- The key to look up
§Returns
Some(&T)- If the key existsNone- If the key doesn’t exist
§Examples
use cloacina_workflow::Context;
let mut context = Context::<String>::new();
context.insert("message", "Hello".to_string()).unwrap();
assert_eq!(context.get("message"), Some(&"Hello".to_string()));
assert_eq!(context.get("missing"), None);Sourcepub fn remove(&mut self, key: &str) -> Option<T>
pub fn remove(&mut self, key: &str) -> Option<T>
Removes and returns a value from the context.
§Arguments
key- The key to remove
§Returns
Some(T)- If the key existed and was removedNone- If the key didn’t exist
§Examples
use cloacina_workflow::Context;
let mut context = Context::<i32>::new();
context.insert("temp", 42).unwrap();
assert_eq!(context.remove("temp"), Some(42));
assert_eq!(context.get("temp"), None);
assert_eq!(context.remove("missing"), None);Sourcepub fn data(&self) -> &HashMap<String, T>
pub fn data(&self) -> &HashMap<String, T>
Gets a reference to the underlying data HashMap.
This method provides direct access to the internal data structure for advanced use cases that need to iterate over all key-value pairs.
§Returns
A reference to the HashMap containing all context data
§Examples
use cloacina_workflow::Context;
let mut context = Context::<i32>::new();
context.insert("a", 1).unwrap();
context.insert("b", 2).unwrap();
for (key, value) in context.data() {
println!("{}: {}", key, value);
}Sourcepub fn into_data(self) -> HashMap<String, T>
pub fn into_data(self) -> HashMap<String, T>
Consumes the context and returns the underlying data HashMap.
§Returns
The HashMap containing all context data