1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use crate::pb::ActionTrace;

pub trait Action: Sized {
    const NAME: &'static str;

    fn decode(trace: &ActionTrace) -> Result<Self, crate::errors::Error>;

    // if action name and parameters match - return decoded action
    fn match_and_decode(trace: impl AsRef<ActionTrace>) -> Option<Self> {
        let trace = trace.as_ref();
        if trace.action.as_ref().unwrap().name != Self::NAME {
            return None;
        }
        Self::decode(trace).ok()
    }
}

impl AsRef<ActionTrace> for ActionTrace {
    fn as_ref(&self) -> &Self {
        self
    }
}