1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct GraphConfig {
8 pub thread_id: Option<String>,
10 pub trace_id: Option<String>,
21 #[serde(skip_serializing_if = "Option::is_none", default)]
26 pub trace_ctx: Option<stack_ids::TraceCtx>,
27 pub recursion_limit: usize,
29 pub max_parallelism: usize,
32 pub tags: Vec<String>,
34 pub metadata: HashMap<String, Value>,
36 pub configurable: HashMap<String, Value>,
38}
39
40impl Default for GraphConfig {
41 fn default() -> Self {
42 Self {
43 thread_id: None,
44 trace_id: None,
45 trace_ctx: None,
46 recursion_limit: 100,
47 max_parallelism: 8,
48 tags: Vec::new(),
49 metadata: HashMap::new(),
50 configurable: HashMap::new(),
51 }
52 }
53}
54
55impl GraphConfig {
56 pub fn new() -> Self {
57 Self::default()
58 }
59
60 pub fn with_thread_id(mut self, id: impl Into<String>) -> Self {
61 self.thread_id = Some(id.into());
62 self
63 }
64
65 pub fn with_recursion_limit(mut self, limit: usize) -> Self {
66 self.recursion_limit = limit;
67 self
68 }
69
70 pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
71 self.tags.push(tag.into());
72 self
73 }
74
75 pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
76 self.metadata.insert(key.into(), value);
77 self
78 }
79
80 pub fn with_configurable(mut self, key: impl Into<String>, value: Value) -> Self {
81 self.configurable.insert(key.into(), value);
82 self
83 }
84
85 pub fn with_trace_id(mut self, id: impl Into<String>) -> Self {
86 self.trace_id = Some(id.into());
87 self
88 }
89
90 pub fn with_max_parallelism(mut self, n: usize) -> Self {
91 self.max_parallelism = n.clamp(1, 32);
92 self
93 }
94
95 pub fn resolve_trace_ctx(&self) -> stack_ids::TraceCtx {
102 if let Some(ref ctx) = self.trace_ctx {
103 return ctx.clone();
104 }
105 match &self.trace_id {
106 Some(id) => stack_ids::TraceCtx::from_legacy_trace_id(id),
107 None => stack_ids::TraceCtx::generate(),
108 }
109 }
110
111 pub fn with_trace_ctx(mut self, ctx: stack_ids::TraceCtx) -> Self {
115 self.trace_id = Some(ctx.to_legacy_trace_id().to_string());
116 self.trace_ctx = Some(ctx);
117 self
118 }
119}