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
use super::*;

/// Represents a potentially resolvable [`Mentionable`].
pub trait Origin<'a, Ctx: Context<'a>>: 'a + Send + Sync {
    /// Type of the associated object.
    type Mtbl: MentionableBase<'a>;
    /// Clone the associated factory.
    fn factory(&self) -> OFctr<'a, Ctx, Self>;
    /// Try resolving the value.
    fn resolve(self: Arc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>
    where
        OFctr<'a, Ctx, Self>: FactoryParse<'a, Ctx>;
    /// Try resolving the bytes. Should avoid parsing the value.
    fn resolve_bytes(self: Arc<Self>) -> HashResolution<'a, Ctx>;
}

/// Type of the [`Factory`] associated with the [`Origin`].
pub type OFctr<'a, Ctx, O> = Fctr<'a, <O as Origin<'a, Ctx>>::Mtbl>;

/// [`OriginMap::resolve_map`].
pub trait OriginMap<'a, Ctx: Context<'a>>: Origin<'a, Ctx> {
    fn ref_resolve(self: &Arc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>
    where
        OFctr<'a, Ctx, Self>: FactoryParse<'a, Ctx>,
    {
        self.clone().resolve()
    }

    fn ref_resolve_bytes(self: &Arc<Self>) -> HashResolution<'a, Ctx> {
        self.clone().resolve_bytes()
    }

    fn resolve_map<T: 'a + Send>(
        self: &Arc<Self>,
        f: impl 'a + Send + FnOnce(ResolutionResult<'a, Ctx, Self::Mtbl>) -> T,
    ) -> Wrapped<'a, Ctx, T>
    where
        OFctr<'a, Ctx, Self>: FactoryParse<'a, Ctx>,
    {
        Ctx::fmap(self.ref_resolve(), f)
    }

    fn resolve_bytes_map<T: 'a + Send>(
        self: &Arc<Self>,
        f: impl 'a + Send + FnOnce(HashResolutionResult<'a, Ctx>) -> T,
    ) -> Wrapped<'a, Ctx, T> {
        Ctx::fmap(self.ref_resolve_bytes(), f)
    }
}

impl<'a, Ctx: Context<'a>, R: ?Sized + Origin<'a, Ctx>> OriginMap<'a, Ctx> for R {}