relay_event_normalization/eap/
attribute_like.rs1use relay_event_schema::protocol::{Attribute, Attributes, SpanData};
2use relay_protocol::{Annotated, Object, Value};
3
4pub trait AttributesLike {
12 type Value: AttributeLike;
14
15 fn as_object(&self) -> &Object<Self::Value>;
17 fn as_object_mut(&mut self) -> &mut Object<Self::Value>;
19
20 fn contains_key(&self, key: &str) -> bool {
22 self.as_object().contains_key(key)
23 }
24
25 fn insert(&mut self, key: String, value: Annotated<Self::Value>) {
27 self.as_object_mut().insert(key, value);
28 }
29
30 fn remove(&mut self, key: &str) -> Option<Annotated<Self::Value>> {
32 self.as_object_mut().remove(key)
33 }
34}
35
36impl AttributesLike for SpanData {
37 type Value = Value;
38
39 fn as_object(&self) -> &Object<Self::Value> {
40 &self.other
41 }
42
43 fn as_object_mut(&mut self) -> &mut Object<Self::Value> {
44 &mut self.other
45 }
46}
47
48impl AttributesLike for Attributes {
49 type Value = Attribute;
50
51 fn as_object(&self) -> &Object<Self::Value> {
52 &self.0
53 }
54
55 fn as_object_mut(&mut self) -> &mut Object<Self::Value> {
56 &mut self.0
57 }
58}
59
60pub trait AttributeLike: Clone + From<String> + From<i64> + From<f64> {
65 fn as_value(&self) -> Option<&Value>;
67
68 fn as_str(&self) -> Option<&str> {
72 self.as_value().and_then(Value::as_str)
73 }
74
75 fn as_f64(&self) -> Option<f64> {
79 self.as_value().and_then(Value::as_f64)
80 }
81}
82
83impl AttributeLike for Value {
84 fn as_value(&self) -> Option<&Value> {
85 Some(self)
86 }
87}
88
89impl AttributeLike for Attribute {
90 fn as_value(&self) -> Option<&Value> {
91 self.value.value.value()
92 }
93}