policy-rs 1.7.0

Policy library for working with protobuf-defined policy objects
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Reference adapter: generic record types with correct Matchable/Transformable impls.
//!
//! Integrating `policy-rs` requires implementing [`Matchable`] (and optionally
//! [`Transformable`]) for your telemetry types. Getting the field-access
//! semantics exactly right is non-trivial — see the [`Matchable`] contract docs
//! for the full list of rules.
//!
//! This module provides three ready-to-use record types that satisfy all
//! conformance requirements out of the box:
//!
//! | Type | Signal | Traits |
//! |------|--------|--------|
//! | [`LogRecord`] | [`LogSignal`] | `Matchable` + `Transformable` |
//! | [`MetricRecord`] | [`MetricSignal`] | `Matchable` |
//! | [`SpanRecord`] | [`TraceSignal`] | `Matchable` + `Transformable` |
//!
//! Each type wraps a simple attribute model that mirrors OTel's `AnyValue`
//! discrimination:
//! - [`Value::String`] values are matchable (returned by `get_field`).
//! - All other [`Value`] variants (int, float, bool, bytes) are present but
//!   non-matchable (`get_field` returns `None`, `field_exists` returns `true`).
//!
//! # Usage
//!
//! Wrap your OTel proto structs by extracting their fields into these types,
//! or use them as-is in tests and the conformance runner.
//!
//! ```
//! use policy_rs::adapter::{LogRecord, Value};
//! use policy_rs::{Matchable, LogSignal};
//! use policy_rs::field::LogFieldSelector;
//! use policy_rs::proto::tero::policy::v1::LogField;
//!
//! let mut log = LogRecord::new();
//! log.body = Some("error connecting to db".to_string());
//! log.set_log_attr("service", Value::String("api".to_string()));
//! log.set_resource_attr("env", Value::String("production".to_string()));
//! ```
//!
//! [`Matchable`]: crate::Matchable
//! [`Transformable`]: crate::Transformable
//! [`LogSignal`]: crate::LogSignal
//! [`MetricSignal`]: crate::MetricSignal
//! [`TraceSignal`]: crate::TraceSignal

use std::borrow::Cow;
use std::collections::HashMap;

use crate::canonical;
use crate::engine::{LogSignal, Matchable, MetricSignal, TraceSignal, Transformable};
use crate::field::{LogFieldSelector, MetricFieldSelector, TraceFieldSelector};
use crate::proto::tero::policy::v1::{
    AggregationTemporality, LogField, MetricField, MetricType, SpanKind, SpanStatusCode, TraceField,
};

// =============================================================================
// Value type
// =============================================================================

/// An attribute value that may or may not be matchable as a string.
///
/// The engine can only pattern-match against [`Value::String`]. Other variants
/// are "present but non-matchable" — `field_exists` returns `true` for them,
/// but `get_field` returns `None` so string matchers won't fire.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    /// A UTF-8 string — the only variant returned by `get_field`.
    String(String),
    /// A 64-bit integer. Present but not matchable.
    Int(i64),
    /// A 64-bit float. Present but not matchable.
    Float(f64),
    /// A boolean. Present but not matchable.
    Bool(bool),
    /// Raw bytes. Present but not matchable.
    Bytes(Vec<u8>),
}

impl Value {
    fn as_str(&self) -> Option<&str> {
        match self {
            Value::String(s) => Some(s.as_str()),
            _ => None,
        }
    }
}

// =============================================================================
// Attribute map helper
// =============================================================================

/// A flat map from attribute key to [`Value`].
pub type Attrs = HashMap<String, Value>;

fn get_attr<'a>(attrs: &'a Attrs, path: &[String]) -> Option<Cow<'a, str>> {
    let key = path.first()?;
    attrs.get(key)?.as_str().map(Cow::Borrowed)
}

fn attr_exists(attrs: &Attrs, path: &[String]) -> bool {
    path.first().map(|k| attrs.contains_key(k)).unwrap_or(false)
}

fn set_attr(attrs: &mut Attrs, path: &[String], value: &str) {
    if let Some(key) = path.first() {
        attrs.insert(key.clone(), Value::String(value.to_string()));
    }
}

fn delete_attr(attrs: &mut Attrs, path: &[String]) -> bool {
    path.first().and_then(|k| attrs.remove(k)).is_some()
}

// =============================================================================
// LogRecord
// =============================================================================

/// A generic log record that satisfies the full `Matchable` + `Transformable`
/// contract for [`LogSignal`].
#[derive(Debug, Default, Clone)]
pub struct LogRecord {
    pub body: Option<String>,
    pub severity_text: Option<String>,
    pub trace_id: Option<String>,
    pub span_id: Option<String>,
    pub event_name: Option<String>,
    pub resource_schema_url: Option<String>,
    pub scope_schema_url: Option<String>,
    pub log_attrs: Attrs,
    pub resource_attrs: Attrs,
    pub scope_attrs: Attrs,
}

impl LogRecord {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn set_log_attr(&mut self, key: impl Into<String>, value: Value) {
        self.log_attrs.insert(key.into(), value);
    }

    pub fn set_resource_attr(&mut self, key: impl Into<String>, value: Value) {
        self.resource_attrs.insert(key.into(), value);
    }

    pub fn set_scope_attr(&mut self, key: impl Into<String>, value: Value) {
        self.scope_attrs.insert(key.into(), value);
    }
}

impl Matchable for LogRecord {
    type Signal = LogSignal;

    fn get_field(&self, field: &LogFieldSelector) -> Option<Cow<'_, str>> {
        match field {
            LogFieldSelector::Simple(f) => match f {
                LogField::Body => self.body.as_deref().map(Cow::Borrowed),
                LogField::SeverityText => self.severity_text.as_deref().map(Cow::Borrowed),
                LogField::TraceId => self.trace_id.as_deref().map(Cow::Borrowed),
                LogField::SpanId => self.span_id.as_deref().map(Cow::Borrowed),
                LogField::EventName => self.event_name.as_deref().map(Cow::Borrowed),
                LogField::ResourceSchemaUrl => {
                    self.resource_schema_url.as_deref().map(Cow::Borrowed)
                }
                LogField::ScopeSchemaUrl => self.scope_schema_url.as_deref().map(Cow::Borrowed),
                _ => None,
            },
            LogFieldSelector::LogAttribute(path) => get_attr(&self.log_attrs, path),
            LogFieldSelector::ResourceAttribute(path) => get_attr(&self.resource_attrs, path),
            LogFieldSelector::ScopeAttribute(path) => get_attr(&self.scope_attrs, path),
        }
    }

    fn field_exists(&self, field: &LogFieldSelector) -> bool {
        match field {
            LogFieldSelector::Simple(_) => self.get_field(field).is_some(),
            LogFieldSelector::LogAttribute(path) => attr_exists(&self.log_attrs, path),
            LogFieldSelector::ResourceAttribute(path) => attr_exists(&self.resource_attrs, path),
            LogFieldSelector::ScopeAttribute(path) => attr_exists(&self.scope_attrs, path),
        }
    }
}

impl Transformable for LogRecord {
    fn set_field(&mut self, field: &LogFieldSelector, value: &str) {
        match field {
            LogFieldSelector::Simple(f) => match f {
                LogField::Body => self.body = Some(value.to_string()),
                LogField::SeverityText => self.severity_text = Some(value.to_string()),
                LogField::TraceId => self.trace_id = Some(value.to_string()),
                LogField::SpanId => self.span_id = Some(value.to_string()),
                LogField::EventName => self.event_name = Some(value.to_string()),
                LogField::ResourceSchemaUrl => self.resource_schema_url = Some(value.to_string()),
                LogField::ScopeSchemaUrl => self.scope_schema_url = Some(value.to_string()),
                _ => {}
            },
            LogFieldSelector::LogAttribute(path) => set_attr(&mut self.log_attrs, path, value),
            LogFieldSelector::ResourceAttribute(path) => {
                set_attr(&mut self.resource_attrs, path, value)
            }
            LogFieldSelector::ScopeAttribute(path) => set_attr(&mut self.scope_attrs, path, value),
        }
    }

    fn delete_field(&mut self, field: &LogFieldSelector) -> bool {
        match field {
            LogFieldSelector::Simple(f) => match f {
                LogField::Body => self.body.take().is_some(),
                LogField::SeverityText => self.severity_text.take().is_some(),
                LogField::TraceId => self.trace_id.take().is_some(),
                LogField::SpanId => self.span_id.take().is_some(),
                LogField::EventName => self.event_name.take().is_some(),
                LogField::ResourceSchemaUrl => self.resource_schema_url.take().is_some(),
                LogField::ScopeSchemaUrl => self.scope_schema_url.take().is_some(),
                _ => false,
            },
            LogFieldSelector::LogAttribute(path) => delete_attr(&mut self.log_attrs, path),
            LogFieldSelector::ResourceAttribute(path) => {
                delete_attr(&mut self.resource_attrs, path)
            }
            LogFieldSelector::ScopeAttribute(path) => delete_attr(&mut self.scope_attrs, path),
        }
    }

    fn move_field(&mut self, from: &LogFieldSelector, to: &LogFieldSelector) {
        let value = match from {
            LogFieldSelector::Simple(f) => match f {
                LogField::Body => self.body.take().map(Value::String),
                LogField::SeverityText => self.severity_text.take().map(Value::String),
                LogField::TraceId => self.trace_id.take().map(Value::String),
                LogField::SpanId => self.span_id.take().map(Value::String),
                LogField::EventName => self.event_name.take().map(Value::String),
                LogField::ResourceSchemaUrl => self.resource_schema_url.take().map(Value::String),
                LogField::ScopeSchemaUrl => self.scope_schema_url.take().map(Value::String),
                _ => None,
            },
            LogFieldSelector::LogAttribute(path) => {
                path.first().and_then(|k| self.log_attrs.remove(k))
            }
            LogFieldSelector::ResourceAttribute(path) => {
                path.first().and_then(|k| self.resource_attrs.remove(k))
            }
            LogFieldSelector::ScopeAttribute(path) => {
                path.first().and_then(|k| self.scope_attrs.remove(k))
            }
        };
        let Some(v) = value else { return };
        match to {
            LogFieldSelector::LogAttribute(path) => {
                if let Some(k) = path.first() {
                    self.log_attrs.insert(k.clone(), v);
                }
            }
            LogFieldSelector::ResourceAttribute(path) => {
                if let Some(k) = path.first() {
                    self.resource_attrs.insert(k.clone(), v);
                }
            }
            LogFieldSelector::ScopeAttribute(path) => {
                if let Some(k) = path.first() {
                    self.scope_attrs.insert(k.clone(), v);
                }
            }
            _ => {}
        }
    }
}

// =============================================================================
// MetricRecord
// =============================================================================

/// A generic metric record that satisfies the `Matchable` contract for
/// [`MetricSignal`].
///
/// Metric policies only filter (keep/drop) — there are no transforms.
/// `MetricRecord` therefore only implements `Matchable`, not `Transformable`.
#[derive(Debug, Default, Clone)]
pub struct MetricRecord {
    pub name: Option<String>,
    pub description: Option<String>,
    pub unit: Option<String>,
    pub resource_schema_url: Option<String>,
    pub scope_schema_url: Option<String>,
    pub scope_name: Option<String>,
    pub scope_version: Option<String>,
    /// Use [`canonical::metric_type_str`] to produce the right value.
    pub metric_type: Option<MetricType>,
    /// Use [`canonical::aggregation_temporality_str`] to produce the right value.
    pub temporality: Option<AggregationTemporality>,
    pub datapoint_attrs: Attrs,
    pub resource_attrs: Attrs,
    pub scope_attrs: Attrs,
}

impl MetricRecord {
    pub fn new() -> Self {
        Self::default()
    }
}

impl Matchable for MetricRecord {
    type Signal = MetricSignal;

    fn get_field(&self, field: &MetricFieldSelector) -> Option<Cow<'_, str>> {
        match field {
            MetricFieldSelector::Simple(f) => match f {
                MetricField::Name => self.name.as_deref().map(Cow::Borrowed),
                MetricField::Description => self.description.as_deref().map(Cow::Borrowed),
                MetricField::Unit => self.unit.as_deref().map(Cow::Borrowed),
                MetricField::ResourceSchemaUrl => {
                    self.resource_schema_url.as_deref().map(Cow::Borrowed)
                }
                MetricField::ScopeSchemaUrl => self.scope_schema_url.as_deref().map(Cow::Borrowed),
                MetricField::ScopeName => self.scope_name.as_deref().map(Cow::Borrowed),
                MetricField::ScopeVersion => self.scope_version.as_deref().map(Cow::Borrowed),
                _ => None,
            },
            MetricFieldSelector::Type => self
                .metric_type
                .map(|mt| Cow::Borrowed(canonical::metric_type_str(mt))),
            MetricFieldSelector::Temporality => self
                .temporality
                .map(|at| Cow::Borrowed(canonical::aggregation_temporality_str(at))),
            MetricFieldSelector::DatapointAttribute(path) => get_attr(&self.datapoint_attrs, path),
            MetricFieldSelector::ResourceAttribute(path) => get_attr(&self.resource_attrs, path),
            MetricFieldSelector::ScopeAttribute(path) => get_attr(&self.scope_attrs, path),
        }
    }

    fn field_exists(&self, field: &MetricFieldSelector) -> bool {
        match field {
            MetricFieldSelector::Type => self.metric_type.is_some(),
            MetricFieldSelector::Temporality => self.temporality.is_some(),
            MetricFieldSelector::DatapointAttribute(path) => {
                attr_exists(&self.datapoint_attrs, path)
            }
            MetricFieldSelector::ResourceAttribute(path) => attr_exists(&self.resource_attrs, path),
            MetricFieldSelector::ScopeAttribute(path) => attr_exists(&self.scope_attrs, path),
            _ => self.get_field(field).is_some(),
        }
    }
}

// =============================================================================
// SpanRecord
// =============================================================================

/// A generic trace span that satisfies the `Matchable` + `Transformable`
/// contract for [`TraceSignal`].
///
/// After calling [`PolicyEngine::evaluate_trace`], check
/// [`SpanRecord::sampling_threshold`]: if `Some`, write its value into the
/// span's tracestate under the `th` sub-key of the `ot` entry.
///
/// # ID fields
///
/// `trace_id`, `span_id`, and `parent_span_id` are bytes fields per the OTel
/// proto. Store them as raw bytes in the `_bytes` fields. The engine's sampler
/// reads raw bytes directly via `get_typed_value` — no hex conversion at
/// evaluation time.
///
/// For string matchers (`exact`, `regex`, etc.) the hex-encoded string fields
/// are used. Populate both if you need both string matching and sampling;
/// populate only `_bytes` if you only need sampling.
///
/// [`PolicyEngine::evaluate_trace`]: crate::PolicyEngine::evaluate_trace
#[derive(Debug, Default, Clone)]
pub struct SpanRecord {
    pub name: Option<String>,
    /// Hex-encoded trace ID for string matchers. See also `trace_id_bytes`.
    pub trace_id: Option<String>,
    /// Raw bytes for the trace ID — used by the sampler directly.
    pub trace_id_bytes: Option<Vec<u8>>,
    /// Hex-encoded span ID for string matchers. See also `span_id_bytes`.
    pub span_id: Option<String>,
    /// Raw bytes for the span ID.
    pub span_id_bytes: Option<Vec<u8>>,
    /// Hex-encoded parent span ID for string matchers. See also `parent_span_id_bytes`.
    pub parent_span_id: Option<String>,
    /// Raw bytes for the parent span ID.
    pub parent_span_id_bytes: Option<Vec<u8>>,
    pub trace_state: Option<String>,
    pub resource_schema_url: Option<String>,
    pub scope_schema_url: Option<String>,
    pub scope_name: Option<String>,
    pub scope_version: Option<String>,
    /// Use [`canonical::span_kind_str`] for the canonical string.
    pub span_kind: Option<SpanKind>,
    /// Use [`canonical::span_status_code_str`] for the canonical string.
    /// `SpanStatusCode::Unspecified` represents OTel's "Unset" status.
    pub span_status: Option<SpanStatusCode>,
    pub span_attrs: Attrs,
    pub resource_attrs: Attrs,
    pub scope_attrs: Attrs,
    /// Written by [`PolicyEngine::evaluate_trace`] when the span is sampled.
    /// The caller must propagate this into the tracestate `ot=th:<value>`.
    pub sampling_threshold: Option<String>,
}

impl SpanRecord {
    pub fn new() -> Self {
        Self::default()
    }
}

impl Matchable for SpanRecord {
    type Signal = TraceSignal;

    fn get_field(&self, field: &TraceFieldSelector) -> Option<Cow<'_, str>> {
        match field {
            TraceFieldSelector::Simple(f) => match f {
                TraceField::Name => self.name.as_deref().map(Cow::Borrowed),
                TraceField::TraceId => self.trace_id.as_deref().map(Cow::Borrowed),
                TraceField::SpanId => self.span_id.as_deref().map(Cow::Borrowed),
                TraceField::ParentSpanId => self.parent_span_id.as_deref().map(Cow::Borrowed),
                TraceField::TraceState => self.trace_state.as_deref().map(Cow::Borrowed),
                TraceField::ResourceSchemaUrl => {
                    self.resource_schema_url.as_deref().map(Cow::Borrowed)
                }
                TraceField::ScopeSchemaUrl => self.scope_schema_url.as_deref().map(Cow::Borrowed),
                TraceField::ScopeName => self.scope_name.as_deref().map(Cow::Borrowed),
                TraceField::ScopeVersion => self.scope_version.as_deref().map(Cow::Borrowed),
                _ => None,
            },
            // SpanKind is a unit variant: return the span's current kind as its
            // canonical string so Vectorscan can match it.
            TraceFieldSelector::SpanKind => self
                .span_kind
                .map(|k| Cow::Borrowed(canonical::span_kind_str(k))),
            // SpanStatus is a unit variant: return the canonical string.
            // A proto3 default (Unspecified == Unset) is still "present".
            TraceFieldSelector::SpanStatus => {
                let sc = self.span_status.unwrap_or(SpanStatusCode::Unspecified);
                Some(Cow::Borrowed(canonical::span_status_code_str(sc)))
            }
            TraceFieldSelector::SpanAttribute(path) => get_attr(&self.span_attrs, path),
            TraceFieldSelector::ResourceAttribute(path) => get_attr(&self.resource_attrs, path),
            TraceFieldSelector::ScopeAttribute(path) => get_attr(&self.scope_attrs, path),
            // Event/link fields require iterating over event/link lists; not
            // supported in this flat record model.
            TraceFieldSelector::EventName | TraceFieldSelector::EventAttribute(_) => None,
            TraceFieldSelector::LinkTraceId => None,
            TraceFieldSelector::SamplingThreshold => {
                self.sampling_threshold.as_deref().map(Cow::Borrowed)
            }
        }
    }

    fn field_exists(&self, field: &TraceFieldSelector) -> bool {
        match field {
            TraceFieldSelector::SpanKind => self.span_kind.is_some(),
            // Status is always present — absent proto3 default ≡ Unspecified/Unset.
            TraceFieldSelector::SpanStatus => true,
            TraceFieldSelector::SpanAttribute(path) => attr_exists(&self.span_attrs, path),
            TraceFieldSelector::ResourceAttribute(path) => attr_exists(&self.resource_attrs, path),
            TraceFieldSelector::ScopeAttribute(path) => attr_exists(&self.scope_attrs, path),
            _ => self.get_field(field).is_some(),
        }
    }

    /// Returns raw bytes for `trace_id`, `span_id`, and `parent_span_id` when
    /// the `_bytes` fields are populated, bypassing hex-string conversion in the
    /// sampler. Falls back to the string value (wrapped as `TypedValue::String`)
    /// for all other fields and when no bytes are set.
    fn get_typed_value(&self, field: &TraceFieldSelector) -> Option<crate::engine::TypedValue<'_>> {
        use crate::engine::TypedValue;
        match field {
            TraceFieldSelector::Simple(TraceField::TraceId) => self
                .trace_id_bytes
                .as_deref()
                .map(TypedValue::Bytes)
                .or_else(|| {
                    self.trace_id
                        .as_deref()
                        .map(|s| TypedValue::String(Cow::Borrowed(s)))
                }),
            TraceFieldSelector::Simple(TraceField::SpanId) => self
                .span_id_bytes
                .as_deref()
                .map(TypedValue::Bytes)
                .or_else(|| {
                    self.span_id
                        .as_deref()
                        .map(|s| TypedValue::String(Cow::Borrowed(s)))
                }),
            TraceFieldSelector::Simple(TraceField::ParentSpanId) => self
                .parent_span_id_bytes
                .as_deref()
                .map(TypedValue::Bytes)
                .or_else(|| {
                    self.parent_span_id
                        .as_deref()
                        .map(|s| TypedValue::String(Cow::Borrowed(s)))
                }),
            _ => self.get_field(field).map(TypedValue::String),
        }
    }
}

impl Transformable for SpanRecord {
    fn set_field(&mut self, field: &TraceFieldSelector, value: &str) {
        match field {
            TraceFieldSelector::Simple(f) => match f {
                TraceField::Name => self.name = Some(value.to_string()),
                TraceField::TraceId => self.trace_id = Some(value.to_string()),
                TraceField::SpanId => self.span_id = Some(value.to_string()),
                TraceField::ParentSpanId => self.parent_span_id = Some(value.to_string()),
                TraceField::TraceState => self.trace_state = Some(value.to_string()),
                TraceField::ResourceSchemaUrl => self.resource_schema_url = Some(value.to_string()),
                TraceField::ScopeSchemaUrl => self.scope_schema_url = Some(value.to_string()),
                TraceField::ScopeName => self.scope_name = Some(value.to_string()),
                TraceField::ScopeVersion => self.scope_version = Some(value.to_string()),
                _ => {}
            },
            TraceFieldSelector::SpanAttribute(path) => set_attr(&mut self.span_attrs, path, value),
            TraceFieldSelector::ResourceAttribute(path) => {
                set_attr(&mut self.resource_attrs, path, value)
            }
            TraceFieldSelector::ScopeAttribute(path) => {
                set_attr(&mut self.scope_attrs, path, value)
            }
            TraceFieldSelector::SamplingThreshold => {
                self.sampling_threshold = Some(value.to_string());
            }
            _ => {}
        }
    }

    fn delete_field(&mut self, field: &TraceFieldSelector) -> bool {
        match field {
            TraceFieldSelector::Simple(f) => match f {
                TraceField::Name => self.name.take().is_some(),
                TraceField::TraceId => self.trace_id.take().is_some(),
                TraceField::SpanId => self.span_id.take().is_some(),
                TraceField::ParentSpanId => self.parent_span_id.take().is_some(),
                TraceField::TraceState => self.trace_state.take().is_some(),
                TraceField::ResourceSchemaUrl => self.resource_schema_url.take().is_some(),
                TraceField::ScopeSchemaUrl => self.scope_schema_url.take().is_some(),
                TraceField::ScopeName => self.scope_name.take().is_some(),
                TraceField::ScopeVersion => self.scope_version.take().is_some(),
                _ => false,
            },
            TraceFieldSelector::SpanAttribute(path) => delete_attr(&mut self.span_attrs, path),
            TraceFieldSelector::ResourceAttribute(path) => {
                delete_attr(&mut self.resource_attrs, path)
            }
            TraceFieldSelector::ScopeAttribute(path) => delete_attr(&mut self.scope_attrs, path),
            _ => false,
        }
    }

    fn move_field(&mut self, from: &TraceFieldSelector, to: &TraceFieldSelector) {
        let value = match from {
            TraceFieldSelector::SpanAttribute(path) => {
                path.first().and_then(|k| self.span_attrs.remove(k))
            }
            TraceFieldSelector::ResourceAttribute(path) => {
                path.first().and_then(|k| self.resource_attrs.remove(k))
            }
            TraceFieldSelector::ScopeAttribute(path) => {
                path.first().and_then(|k| self.scope_attrs.remove(k))
            }
            _ => None,
        };
        let Some(v) = value else { return };
        match to {
            TraceFieldSelector::SpanAttribute(path) => {
                if let Some(k) = path.first() {
                    self.span_attrs.insert(k.clone(), v);
                }
            }
            TraceFieldSelector::ResourceAttribute(path) => {
                if let Some(k) = path.first() {
                    self.resource_attrs.insert(k.clone(), v);
                }
            }
            TraceFieldSelector::ScopeAttribute(path) => {
                if let Some(k) = path.first() {
                    self.scope_attrs.insert(k.clone(), v);
                }
            }
            _ => {}
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::field::LogFieldSelector;
    use crate::proto::tero::policy::v1::LogField;

    #[test]
    fn log_record_string_value_is_matchable() {
        let mut rec = LogRecord::new();
        rec.body = Some("hello".to_string());
        rec.set_log_attr("k", Value::String("v".to_string()));

        assert_eq!(
            rec.get_field(&LogFieldSelector::Simple(LogField::Body)),
            Some(Cow::Borrowed("hello"))
        );
        assert_eq!(
            rec.get_field(&LogFieldSelector::LogAttribute(vec!["k".to_string()])),
            Some(Cow::Borrowed("v"))
        );
    }

    #[test]
    fn log_record_non_string_attr_is_present_but_not_matchable() {
        let mut rec = LogRecord::new();
        rec.set_log_attr("count", Value::Int(42));

        // get_field returns None (can't match a string pattern against an int)
        assert_eq!(
            rec.get_field(&LogFieldSelector::LogAttribute(vec!["count".to_string()])),
            None
        );
        // field_exists returns true (exists: true matcher should fire)
        assert!(rec.field_exists(&LogFieldSelector::LogAttribute(vec!["count".to_string()])));
    }

    #[test]
    fn metric_record_type_and_temporality_use_canonical_strings() {
        use crate::field::MetricFieldSelector;
        let mut rec = MetricRecord::new();
        rec.metric_type = Some(MetricType::Sum);
        rec.temporality = Some(AggregationTemporality::Delta);

        assert_eq!(
            rec.get_field(&MetricFieldSelector::Type),
            Some(Cow::Borrowed("METRIC_TYPE_SUM"))
        );
        assert_eq!(
            rec.get_field(&MetricFieldSelector::Temporality),
            Some(Cow::Borrowed("AGGREGATION_TEMPORALITY_DELTA"))
        );
    }

    #[test]
    fn span_record_sampling_threshold_set_by_set_field() {
        use crate::field::TraceFieldSelector;
        let mut span = SpanRecord::new();
        span.set_field(&TraceFieldSelector::SamplingThreshold, "8");
        assert_eq!(span.sampling_threshold, Some("8".to_string()));
    }

    #[test]
    fn span_record_span_status_is_always_present() {
        use crate::field::TraceFieldSelector;

        // Status is always present (proto3 default = Unspecified = Unset).
        let span = SpanRecord::new(); // no span_status set
        assert!(span.field_exists(&TraceFieldSelector::SpanStatus));
        assert_eq!(
            span.get_field(&TraceFieldSelector::SpanStatus),
            Some(Cow::Borrowed("SPAN_STATUS_CODE_UNSPECIFIED"))
        );

        // Explicit status is returned.
        let mut span2 = SpanRecord::new();
        span2.span_status = Some(SpanStatusCode::Error);
        assert_eq!(
            span2.get_field(&TraceFieldSelector::SpanStatus),
            Some(Cow::Borrowed("SPAN_STATUS_CODE_ERROR"))
        );
    }
}