-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_metadata.rs
More file actions
72 lines (66 loc) · 2.5 KB
/
Copy pathstructured_metadata.rs
File metadata and controls
72 lines (66 loc) · 2.5 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT
//! Structured metadata example showing typed fields vs string formatting.
//!
//! Run with:
//! ```sh
//! cargo run --example structured_metadata
//! ```
use std::{net::IpAddr, time::Duration};
use masterror::{AppError, AppResult, field};
fn database_query(
table: &'static str,
user_id: u64,
timeout: Duration,
retry_count: u64
) -> AppResult<String> {
Err(AppError::database_with_message("Query failed")
.with_field(field::str("table", table))
.with_field(field::u64("user_id", user_id))
.with_field(field::duration("timeout", timeout))
.with_field(field::u64("retry_count", retry_count)))
}
fn api_request(endpoint: &'static str, client_ip: IpAddr, latency_ms: f64) -> AppResult<()> {
Err(AppError::external_api("External API call failed")
.with_field(field::str("endpoint", endpoint))
.with_field(field::ip("client_ip", client_ip))
.with_field(field::f64("latency_ms", latency_ms)))
}
fn main() {
println!("=== Structured Metadata with Typed Fields ===\n");
match database_query("users", 12345, Duration::from_secs(30), 3) {
Ok(_) => println!("Query succeeded"),
Err(e) => {
println!("Error: {e}");
println!("\nMetadata:");
for (key, value) in e.metadata().iter() {
println!(" {key}: {value:?}");
}
}
}
println!("\n=== API Request with IP and Float ===\n");
let client_ip: IpAddr = "192.168.1.100".parse().unwrap();
match api_request("/api/users", client_ip, 123.45) {
Ok(()) => println!("API request succeeded"),
Err(e) => {
println!("Error: {e}");
println!("\nMetadata fields: {}", e.metadata().len());
for (key, value) in e.metadata().iter() {
println!(" {key}: {value:?}");
}
}
}
println!("\n=== Multiple Chained Metadata ===\n");
let err = AppError::internal("Processing failed")
.with_field(field::str("stage", "validation"))
.with_field(field::u64("record_id", 999))
.with_field(field::duration("elapsed", Duration::from_millis(456)))
.with_field(field::bool("retryable", true));
println!("Error: {err}");
println!("Total metadata fields: {}", err.metadata().len());
println!("\nAll fields:");
for (key, value) in err.metadata().iter() {
println!(" {key} = {value:?}");
}
}