Context

Struct Context 

Source
pub struct Context<T>
where T: Serialize + for<'de> Deserialize<'de> + Debug,
{ /* 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 implement Serialize, Deserialize, and Debug.

§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>
where T: Serialize + for<'de> Deserialize<'de> + Debug,

Source

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());
Source

pub fn clone_data(&self) -> Self
where 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
Source

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 successful
  • Err(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(_))));
Source

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 update
  • value - The new value
§Returns
  • Ok(()) - If the update was successful
  • Err(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(_))));
Source

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 exists
  • None - 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);
Source

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 removed
  • None - 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);
Source

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);
}
Source

pub fn into_data(self) -> HashMap<String, T>

Consumes the context and returns the underlying data HashMap.

§Returns

The HashMap containing all context data

Source

pub fn from_data(data: HashMap<String, T>) -> Self

Creates a Context from a HashMap.

§Arguments
  • data - The HashMap to use as context data
§Returns

A new Context with the provided data

Source

pub fn to_json(&self) -> Result<String, ContextError>

Serializes the context to a JSON string.

§Returns
  • Ok(String) - The JSON representation of the context
  • Err(ContextError) - If serialization fails
Source

pub fn from_json(json: String) -> Result<Self, ContextError>

Deserializes a context from a JSON string.

§Arguments
  • json - The JSON string to deserialize
§Returns
  • Ok(Context<T>) - The deserialized context
  • Err(ContextError) - If deserialization fails

Trait Implementations§

Source§

impl<T> Debug for Context<T>
where T: Serialize + for<'de> Deserialize<'de> + Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Context<T>
where T: Serialize + for<'de> Deserialize<'de> + Debug,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Context<T>

§

impl<T> RefUnwindSafe for Context<T>
where T: RefUnwindSafe,

§

impl<T> Send for Context<T>
where T: Send,

§

impl<T> Sync for Context<T>
where T: Sync,

§

impl<T> Unpin for Context<T>
where T: Unpin,

§

impl<T> UnwindSafe for Context<T>
where T: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more