use crate::types::{ActivityOutput, OutputType, UsageEntry};
use rust_decimal::Decimal;
use serde_json::Value;
#[derive(Debug, Clone, Default)]
pub struct ActivityResult {
pub outputs: Vec<ActivityOutput>,
pub cost_usd: Option<Decimal>,
pub usage: Vec<UsageEntry>,
pub metadata: Option<Value>,
}
impl ActivityResult {
pub fn value(name: impl Into<String>, value: Value) -> Self {
Self {
outputs: vec![ActivityOutput::value(name, value)],
..Default::default()
}
}
pub fn values(outputs: Vec<ActivityOutput>) -> Self {
Self {
outputs,
..Default::default()
}
}
pub fn with_cost(mut self, cost_usd: Decimal) -> Self {
self.cost_usd = Some(cost_usd);
self
}
pub fn with_usage(mut self, usage: Vec<UsageEntry>) -> Self {
self.usage = usage;
self
}
pub fn push_usage(mut self, entry: UsageEntry) -> Self {
self.usage.push(entry);
self
}
pub fn with_metadata(mut self, metadata: Value) -> Self {
self.metadata = Some(metadata);
self
}
pub fn to_json_value(&self) -> Value {
let mut map = serde_json::Map::new();
for output in &self.outputs {
if output.output_type == OutputType::Value {
map.insert(output.name.clone(), output.value.clone());
}
}
Value::Object(map)
}
pub fn output_count(&self) -> usize {
self.outputs.len()
}
pub fn has_output(&self, name: &str) -> bool {
self.outputs.iter().any(|o| o.name == name)
}
pub fn get_output(&self, name: &str) -> Option<&ActivityOutput> {
self.outputs.iter().find(|o| o.name == name)
}
pub fn value_outputs(&self) -> Vec<&ActivityOutput> {
self.outputs
.iter()
.filter(|o| o.output_type == OutputType::Value)
.collect()
}
pub fn file_outputs(&self) -> Vec<&ActivityOutput> {
self.outputs
.iter()
.filter(|o| o.output_type == OutputType::File)
.collect()
}
pub fn folder_outputs(&self) -> Vec<&ActivityOutput> {
self.outputs
.iter()
.filter(|o| o.output_type == OutputType::Folder)
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use rust_decimal_macros::dec;
use serde_json::json;
#[test]
fn value_output() {
let result = ActivityResult::value("result", json!({"status": "success"}));
assert_eq!(result.outputs.len(), 1);
assert_eq!(result.outputs[0].name, "result");
assert_eq!(result.cost_usd, None);
assert!(result.usage.is_empty());
}
#[test]
fn to_json_value_filters_files() {
let result = ActivityResult::values(vec![
ActivityOutput::value("status", json!("success")),
ActivityOutput::file("document", "postgres://wf/act/file.pdf"),
ActivityOutput::value("count", json!(42)),
]);
assert_eq!(
result.to_json_value(),
json!({"status": "success", "count": 42})
);
}
#[test]
fn with_cost_and_usage() {
let result = ActivityResult::value("out", json!(1))
.with_cost(dec!(0.02))
.with_usage(vec![UsageEntry::new("anthropic", "claude-sonnet-5")])
.push_usage(UsageEntry::new("openai", "gpt-5"));
assert_eq!(result.cost_usd, Some(dec!(0.02)));
assert_eq!(result.usage.len(), 2);
assert_eq!(result.usage[1].provider, "openai");
}
#[test]
fn output_accessors() {
let result = ActivityResult::values(vec![
ActivityOutput::value("status", json!("success")),
ActivityOutput::file("document", "ref"),
ActivityOutput::folder("out_dir", "ref/"),
]);
assert_eq!(result.output_count(), 3);
assert!(result.has_output("status"));
assert!(!result.has_output("missing"));
assert_eq!(result.get_output("document").unwrap().name, "document");
assert_eq!(result.value_outputs().len(), 1);
assert_eq!(result.file_outputs().len(), 1);
assert_eq!(result.folder_outputs().len(), 1);
}
}