-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.ov
More file actions
54 lines (47 loc) · 1.79 KB
/
Copy pathtrace.ov
File metadata and controls
54 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module trace_example
record Order {
id: Int,
amount: Int,
status: OrderStatus,
}
enum OrderStatus { Pending, Shipped, Delivered }
@derive(Debug, Display)
enum ProcessError {
ApprovalRequired { order_id: Int },
AlreadyShipped { order_id: Int },
AlreadyDelivered { order_id: Int },
}
// A `trace` block captures structured events — function entry/exit, value
// bindings, branches taken, pattern-match arms — for any registered consumer.
// With no consumer subscribed, the block is a zero-cost pass-through: the body
// still runs, but no events are allocated or dispatched.
fn process_order(order: Order) -> Result<Order, ProcessError> {
trace {
match order.status {
OrderStatus.Pending => {
if order.amount > 10000 {
Err(ProcessError.ApprovalRequired { order_id = order.id })
} else {
Ok(order with { status = OrderStatus.Shipped })
}
}
OrderStatus.Shipped => Err(ProcessError.AlreadyShipped { order_id = order.id }),
OrderStatus.Delivered => Err(ProcessError.AlreadyDelivered { order_id = order.id }),
}
}
}
// Register a consumer at the start of main to observe the trace.
// In production, this is typically not called, so traces are inert.
fn main() !{io} -> Result<(), IoError> {
Trace.subscribe(print_event)
let order: Order = Order { id = 42, amount = 500, status = OrderStatus.Pending }
match process_order(order) {
Ok(updated) => println("shipped: ${updated.id}"),
Err(e) => println("failed: $e"),
}?
Ok(())
}
fn print_event(event: TraceEvent) !{io} -> () {
// Discard the Result explicitly (DESIGN.md §11: ignored Results are errors).
_ = println("[trace] $event")
}