@@ -5,6 +5,9 @@ Generic computation trace with visual tree rendering.
55` Tracer ` records what ran, what went in, what came out, and any
66sub-steps — then renders the whole tree as a color-coded inspection output.
77
8+ Extracted from the trace pattern in [ Babel] ( https://github.com/alexocode/babel ) ,
9+ adapted for general use.
10+
811## Installation
912
1013``` elixir
@@ -15,49 +18,97 @@ def deps do
1518end
1619```
1720
18- ## Usage
21+ ## Rendering
22+
23+ The ` Inspect ` implementation renders traces as a structured tree.
24+ Colors are applied when the terminal supports them — green for ` OK ` , red for ` ERROR ` .
25+
26+ ### Simple trace
1927
2028``` elixir
21- # Build a trace manually
22- trace = Tracer .new (:parse_int , " 42" , {:ok , 42 })
23- Tracer .ok? (trace) # => true
24- Tracer .result (trace) # => {:ok, 42}
29+ trace = Tracer .new (:double , 3 , {:ok , 6 })
30+ Tracer .inspect (trace)
31+ ```
32+
33+ ```
34+ Tracer<OK> {
35+ data = 3
2536
26- # Nested traces — full computation tree
37+ :double
38+ |=> 6
39+ }
40+ ```
41+
42+ ### Nested trace — full tree (` depth: :infinity ` )
43+
44+ ``` elixir
2745inner = Tracer .new (:validate_range , 42 , {:ok , 42 })
2846outer = Tracer .new (:parse_and_validate , " 42" , {:ok , 42 }, [inner])
29-
30- # Inspect with depth control
3147Tracer .inspect (outer, depth: :infinity )
32- # Tracer<OK> {
33- # data = "42"
34- # :parse_and_validate
35- # | :validate_range |=< 42 |=> 42
36- #
37- # |=> 42
38- # }
39-
40- # Only show error branches
41- Tracer .inspect (outer, depth: :error )
48+ ```
49+
50+ ```
51+ Tracer<OK> {
52+ data = "42"
53+
54+ :parse_and_validate
55+ |
56+ | :validate_range
57+ | |=< 42
58+ | |=> 42
59+ |
60+ |=> 42
61+ }
62+ ```
4263
43- # Find failing traces anywhere in the tree
44- Tracer .find (outer, & Tracer .error? / 1 )
64+ ### Error trace — only error branches (` depth: :error ` )
4565
46- # Find the root causes (leaf errors)
47- Tracer .root_causes (outer)
66+ ``` elixir
67+ bad = Tracer .new (:validate_range , - 1 , {:error , :out_of_range })
68+ good = Tracer .new (:parse_int , " -1" , {:ok , - 1 })
69+ outer = Tracer .new (:parse_and_validate , " -1" , {:error , [:out_of_range ]}, [good, bad])
70+ Tracer .inspect (outer, depth: :error )
4871```
4972
50- ## Inspect Options
73+ ```
74+ Tracer<ERROR> {
75+ data = "-1"
76+
77+ :parse_and_validate
78+ |
79+ | ... traces omitted (1) ...
80+ |
81+ | :validate_range
82+ | |=< -1
83+ | |=> {:error, :out_of_range}
84+ |
85+ |=> {:error, [:out_of_range]}
86+ }
87+ ```
88+
89+ ## API
5190
5291``` elixir
53- # depth: 0 — top level only (default)
54- # depth: N — show N levels deep
55- # depth: :error — only error branches
56- # depth: :infinity — the full tree
57- inspect (trace, custom_options: [depth: :infinity ])
58-
59- # Or use the shortcut
60- Tracer .inspect (trace, depth: :infinity )
92+ # Construct
93+ Tracer .new (step, input, output)
94+ Tracer .new (step, input, output, nested_traces)
95+
96+ # Status — ok? when output is not false/:error/{:error,_}
97+ Tracer .ok? (trace) # => boolean
98+ Tracer .error? (trace) # => boolean
99+ Tracer .result (trace) # => {:ok, value} | {:error, reason}
100+
101+ # Navigate the tree
102+ Tracer .find (trace, & Tracer .error? / 1 ) # => [Tracer.t()]
103+ Tracer .root_causes (trace) # => [Tracer.t()] — leaf errors only
104+ Tracer .reduce (trace, 0 , fn _ , n -> n + 1 end ) # => count of all traces
105+
106+ # Render
107+ Tracer .inspect (trace) # top level only (default)
108+ Tracer .inspect (trace, depth: :infinity ) # full tree
109+ Tracer .inspect (trace, depth: :error ) # error branches only
110+ Tracer .inspect (trace, depth: 2 ) # up to 2 levels deep
111+ inspect (trace, custom_options: [depth: 1 ]) # via standard Inspect opts
61112```
62113
63114## Building Nested Traces
@@ -73,6 +124,13 @@ Tracer.inspect(trace, depth: :infinity)
73124Tracer .new (operator, input, result, nested)
74125```
75126
127+ ## Used By
128+
129+ - [ Babel] ( https://github.com/alexocode/babel ) — data transformation pipelines
130+ (the trace pattern originated here)
131+ - [ Brex] ( https://github.com/alexocode/brex ) — composable business rules
132+ (planned: rule evaluation tree visualization)
133+
76134## License
77135
78136MIT — see [ LICENSE] ( LICENSE ) .
0 commit comments