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
// This module only defines traits, every parameter is used by definition
#![allow(unused_variables)]

use std::fmt::Debug;

use relay_protocol::{FromValue, IntoValue, Meta};

use crate::processor::{process_value, ProcessingState, ValueType};

/// Used to indicate how to handle an annotated value in a callback.
#[must_use = "This `ProcessingAction` must be handled by `Annotated::apply`"]
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum ProcessingAction {
    /// Discards the value entirely.
    #[error("value should be hard-deleted (unreachable, should not surface as error!)")]
    DeleteValueHard,

    /// Discards the value and moves it into meta's `original_value`.
    #[error("value should be hard-deleted (unreachable, should not surface as error!)")]
    DeleteValueSoft,

    /// The event is invalid (needs to bubble up)
    #[error("invalid transaction event: {0}")]
    InvalidTransaction(&'static str),
}

/// The result of running a processor on a value implementing `ProcessValue`.
pub type ProcessingResult = Result<(), ProcessingAction>;

macro_rules! process_method {
    ($name: ident, $ty:ident $(::$path:ident)*) => {
        process_method!($name, $ty $(::$path)* <>);
    };

    ($name: ident, $ty:ident $(::$path:ident)* < $($param:ident),* > $(, $param_req_key:ident : $param_req_trait:path)*) => {
        #[inline]
        fn $name<$($param),*>(
            &mut self,
            value: &mut $ty $(::$path)* <$($param),*>,
            meta: &mut Meta,
            state: &ProcessingState<'_>,
        ) -> ProcessingResult
        where
            $($param: ProcessValue),*
            $(, $param_req_key : $param_req_trait)*
        {
            value.process_child_values(self, state)?;
            Ok(())
        }
    };
}

/// A trait for processing processable values.
pub trait Processor: Sized {
    #[inline]
    fn before_process<T: ProcessValue>(
        &mut self,
        value: Option<&T>,
        meta: &mut Meta,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult {
        Ok(())
    }

    #[inline]
    fn after_process<T: ProcessValue>(
        &mut self,
        value: Option<&T>,
        meta: &mut Meta,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult {
        Ok(())
    }

    process_method!(process_string, String);
    process_method!(process_u64, u64);
    process_method!(process_i64, i64);
    process_method!(process_f64, f64);
    process_method!(process_bool, bool);

    process_method!(process_value, relay_protocol::Value);
    process_method!(process_array, relay_protocol::Array<T>);
    process_method!(process_object, relay_protocol::Object<T>);

    process_method!(
        process_pairlist,
        crate::protocol::PairList<T>,
        T: crate::protocol::AsPair
    );
    process_method!(process_values, crate::protocol::Values<T>);
    process_method!(process_timestamp, crate::protocol::Timestamp);

    process_method!(process_event, crate::protocol::Event);
    process_method!(process_replay, crate::protocol::Replay);
    process_method!(process_exception, crate::protocol::Exception);
    process_method!(process_raw_stacktrace, crate::protocol::RawStacktrace);
    process_method!(process_stacktrace, crate::protocol::Stacktrace);
    process_method!(process_frame, crate::protocol::Frame);
    process_method!(process_request, crate::protocol::Request);
    process_method!(process_user, crate::protocol::User);
    process_method!(process_client_sdk_info, crate::protocol::ClientSdkInfo);
    process_method!(process_debug_meta, crate::protocol::DebugMeta);
    process_method!(process_debug_image, crate::protocol::DebugImage);
    process_method!(process_geo, crate::protocol::Geo);
    process_method!(process_logentry, crate::protocol::LogEntry);
    process_method!(process_thread, crate::protocol::Thread);
    process_method!(process_context, crate::protocol::Context);
    process_method!(process_breadcrumb, crate::protocol::Breadcrumb);
    process_method!(process_template_info, crate::protocol::TemplateInfo);
    process_method!(process_header_name, crate::protocol::HeaderName);
    process_method!(process_span, crate::protocol::Span);
    process_method!(process_trace_context, crate::protocol::TraceContext);
    process_method!(process_native_image_path, crate::protocol::NativeImagePath);
    process_method!(process_contexts, crate::protocol::Contexts);

    fn process_other(
        &mut self,
        other: &mut relay_protocol::Object<relay_protocol::Value>,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult {
        for (key, value) in other {
            process_value(
                value,
                self,
                &state.enter_borrowed(
                    key.as_str(),
                    state.inner_attrs(),
                    ValueType::for_field(value),
                ),
            )?;
        }

        Ok(())
    }
}

#[doc(inline)]
pub use enumset::{enum_set, EnumSet};

/// A recursively processable value.
pub trait ProcessValue: FromValue + IntoValue + Debug + Clone {
    /// Returns the type of the value.
    #[inline]
    fn value_type(&self) -> EnumSet<ValueType> {
        EnumSet::empty()
    }

    /// Executes a processor on this value.
    #[inline]
    fn process_value<P>(
        &mut self,
        meta: &mut Meta,
        processor: &mut P,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult
    where
        P: Processor,
    {
        self.process_child_values(processor, state)
    }

    /// Recurses into children of this value.
    #[inline]
    fn process_child_values<P>(
        &mut self,
        processor: &mut P,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult
    where
        P: Processor,
    {
        Ok(())
    }
}

pub use relay_event_derive::ProcessValue;