relay_event_schema/processor/
funcs.rs

1use relay_protocol::{Annotated, IntoValue, Meta};
2
3use crate::processor::{
4    ProcessValue, ProcessingAction, ProcessingResult, ProcessingState, Processor,
5};
6
7/// Modifies this value based on the action returned by `f`.
8#[inline]
9pub fn apply<T, F, R>(v: &mut Annotated<T>, f: F) -> ProcessingResult
10where
11    T: IntoValue,
12    F: FnOnce(&mut T, &mut Meta) -> R,
13    R: Into<ProcessingResult>,
14{
15    let result = match (v.0.as_mut(), &mut v.1) {
16        (Some(value), meta) => f(value, meta).into(),
17        (None, _) => Ok(()),
18    };
19
20    match result {
21        Ok(()) => (),
22        Err(ProcessingAction::DeleteValueHard) => v.0 = None,
23        Err(ProcessingAction::DeleteValueSoft) => {
24            v.1.set_original_value(v.0.take());
25        }
26        x @ Err(ProcessingAction::InvalidTransaction(_)) => return x,
27    }
28
29    Ok(())
30}
31
32/// Processes the value using the given processor.
33#[inline]
34pub fn process_value<T, P>(
35    annotated: &mut Annotated<T>,
36    processor: &mut P,
37    state: &ProcessingState<'_>,
38) -> ProcessingResult
39where
40    T: ProcessValue,
41    P: Processor,
42{
43    let action = processor.before_process(annotated.0.as_ref(), &mut annotated.1, state);
44    apply(annotated, |_, _| action)?;
45
46    apply(annotated, |value, meta| {
47        ProcessValue::process_value(value, meta, processor, state)
48    })?;
49
50    let action = processor.after_process(annotated.0.as_ref(), &mut annotated.1, state);
51    apply(annotated, |_, _| action)?;
52
53    Ok(())
54}