xmtp_api_d14n/protocol/traits/
extractor.rs

1/// An Extractor indicates a specific direction to
2/// process an [`ProtocolEnvelope`].
3/// Extractor implementations are available in [`crate::protocol::extractors`]
4pub trait Extractor {
5    /// The output this extractor will produce
6    type Output;
7    /// Get the output of the extraction
8    fn get(self) -> Self::Output;
9}
10
11/// Represents an [`Extractor`] whose output is a [`Result`]
12/// Useful for deriving traits that should be aware of Result Ok and Error
13/// values.
14pub trait TryExtractor: Extractor<Output = Result<Self::Ok, Self::Error>> {
15    /// The [`Result::Ok`] value of an [`Extractor`]
16    type Ok;
17    /// The [`Result::Err`] value of an [`Extractor`]
18    type Error;
19    /// Try to get the extraction result
20    fn try_get(self) -> Result<Self::Ok, Self::Error>;
21}
22
23impl<T, O, Err> TryExtractor for T
24where
25    T: Extractor<Output = Result<O, Err>>,
26{
27    type Ok = O;
28
29    type Error = Err;
30
31    fn try_get(self) -> Result<Self::Ok, Self::Error> {
32        self.get()
33    }
34}
35
36pub trait MaybeExtractor: Extractor<Output = Option<Self::Value>> {
37    type Value;
38    fn maybe_get(self) -> Option<Self::Value>;
39}
40
41impl<T, V> MaybeExtractor for T
42where
43    T: Extractor<Output = Option<V>>,
44{
45    type Value = V;
46
47    fn maybe_get(self) -> Option<Self::Value> {
48        self.get()
49    }
50}