Skip to main content

basic_usage/
basic_usage.rs

1//! Examples of how to use the Cirious Codex diagnostic framework.
2//!
3//! This module demonstrates the usage of `CodexOk` with both dynamic metadata
4//! (`HashMap`) and strongly-typed diagnostic contexts.
5
6use cirious_codex_result::{CodexOk, CodexOkRaw, ExecutionContext};
7
8#[cfg(feature = "serde")]
9use cirious_codex_result::log_codex_ok;
10
11/// Demonstrates dynamic metadata usage via `HashMap`.
12pub fn example_dynamic_metadata() {
13  println!("--- Dynamic Metadata Example ---");
14
15  // Explicitly annotate type to satisfy inference for HashMap
16  let result = CodexOk::new("Operation completed").with_meta("service", "auth");
17
18  println!("{result}");
19
20  #[cfg(feature = "serde")]
21  log_codex_ok(&result);
22}
23
24/// Demonstrates strongly-typed context for high-performance, structured diagnostics.
25pub fn example_typed_metadata() {
26  println!("\n--- Typed Metadata Example ---");
27
28  let ctx = ExecutionContext::new()
29    .with_duration(150)
30    .with_affected_rows(12)
31    .with_process_id(9982);
32
33  // Provide explicit type here so the compiler knows C is ExecutionContext
34  let result: CodexOkRaw<String, ExecutionContext> = CodexOkRaw {
35    value: "Batch processing completed".to_string(),
36    location: std::panic::Location::caller(),
37    execution_meta: ctx,
38  };
39
40  println!("{result}");
41
42  #[cfg(feature = "serde")]
43  log_codex_ok(&result);
44}
45
46fn main() {
47  example_dynamic_metadata();
48  example_typed_metadata();
49}