relay_event_schema/processor/
funcs.rs

1use relay_protocol::{Annotated, IntoValue, Meta, Remark, RemarkType};
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) => delete_hard(v),
23        Err(ProcessingAction::DeleteValueWithRemark(rule_id)) => delete_with_remark(v, rule_id),
24        Err(ProcessingAction::DeleteValueSoft) => delete_soft(v),
25
26        x @ Err(ProcessingAction::InvalidTransaction(_)) => return x,
27    }
28
29    Ok(())
30}
31
32/// Deletes an `Annotated`'s value.
33pub fn delete_hard<T>(v: &mut Annotated<T>) {
34    v.0 = None;
35}
36
37/// Deletes an `Annotated`'s value and adds a remark to the metadata.
38///
39/// The passed `rule_id` is used in the remark.
40pub fn delete_with_remark<T>(v: &mut Annotated<T>, rule_id: &str) {
41    v.0 = None;
42    v.1.add_remark(Remark {
43        ty: RemarkType::Removed,
44        rule_id: rule_id.to_owned(),
45        range: None,
46    });
47}
48
49/// Deletes this `Annotated`'s value, but retains it as the original
50/// value in the metadata.
51pub fn delete_soft<T: IntoValue>(v: &mut Annotated<T>) {
52    v.1.set_original_value(v.0.take());
53}
54
55/// Processes the value using the given processor.
56#[inline]
57pub fn process_value<T, P>(
58    annotated: &mut Annotated<T>,
59    processor: &mut P,
60    state: &ProcessingState<'_>,
61) -> ProcessingResult
62where
63    T: ProcessValue,
64    P: Processor,
65{
66    let action = processor.before_process(annotated.0.as_ref(), &mut annotated.1, state);
67    apply(annotated, |_, _| action)?;
68
69    apply(annotated, |value, meta| {
70        ProcessValue::process_value(value, meta, processor, state)
71    })?;
72
73    let action = processor.after_process(annotated.0.as_ref(), &mut annotated.1, state);
74    apply(annotated, |_, _| action)?;
75
76    Ok(())
77}