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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Provides [Atomic]-[Mentionable] interface.

use std::{marker::PhantomData, ops::Deref};

use crate::atomic::*;

use super::*;

/// Generic implementation of a [Mentionable] for [Atomic]s.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AtomicObject<A> {
    atomic: A,
}

impl<A: AtomicBase> From<A> for AtomicObject<A> {
    fn from(value: A) -> Self {
        Self { atomic: value }
    }
}

impl<A: AtomicBase> AsRef<A> for AtomicObject<A> {
    fn as_ref(&self) -> &A {
        &self.atomic
    }
}

impl<A: AtomicBase> Deref for AtomicObject<A> {
    type Target = A;

    fn deref(&self) -> &Self::Target {
        &self.atomic
    }
}

impl<A: AtomicBase> Serializable for AtomicObject<A> {
    fn serialize(&self, serializer: &mut dyn Serializer) {
        self.atomic.serialize(serializer)
    }
}

impl<A: AtomicBase> MentionableBase<'_> for AtomicObject<A> {
    type Fctr = AtomicFactory<A>;

    fn factory(&self) -> Self::Fctr {
        AtomicFactory::new()
    }
}

impl<'a, Ctx: Context<'a>, A: AtomicBase> MentionableTop<'a, Ctx> for AtomicObject<A> {
    fn topology_hash(&self) -> Hash {
        Ctx::hash(b"")
    }

    fn points_typed(&self, _points: &mut impl PointsVisitor<'a, Ctx>) {}
}

/// Generic implementation of a [Factory] for [Atomic]s.
pub struct AtomicFactory<A> {
    _pd: PhantomData<A>,
}

impl<A> AtomicFactory<A> {
    fn new() -> Self {
        AtomicFactory { _pd: PhantomData }
    }
}

impl<A: AtomicBase> Clone for AtomicFactory<A> {
    fn clone(&self) -> Self {
        Self::new()
    }
}

impl<A: AtomicBase> FactoryBase<'_> for AtomicFactory<A> {
    type Mtbl = AtomicObject<A>;

    type ParseError = A::AParseError;
}

impl<A: ParseMode> ParseMode for AtomicFactory<A> {
    type Mode = A::Mode;
}

impl<'a, Ctx: Context<'a>, A: AtomicModeParse> FactoryModeParse<'a, Ctx> for AtomicFactory<A> {
    fn mdeserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> ModeResultM<'a, Self, I> {
        A::ma_deserialize(inctx).map(|a| Self::map(a, From::from))
    }

    fn mextend(
        &self,
        mentionable: ExtensionSourceM<'a, Self>,
        tail: &[u8],
    ) -> ExtensionResultM<'a, Self> {
        Self::xbind(
            A::ma_extend(Self::smap(mentionable, |a| a.atomic), tail),
            |a| Ok(a.into()),
        )
    }
}

/// Extension trait to provide method-like utilities associated with [AtomicObject]s.
pub trait AtomicObjectExt: AtomicBase {
    /// Shorthand for getting specific [`AtomicFactory`].
    fn f() -> AtomicFactory<Self> {
        AtomicFactory::new()
    }
    /// Shorthand for getting specific [`AtomicObject`].
    fn m(self) -> AtomicObject<Self> {
        self.into()
    }
}

impl<A: AtomicBase> AtomicObjectExt for A {}