willow_data_model/test_parameters/
namespace.rs

1use arbitrary::Arbitrary;
2
3use order_theory::*;
4
5use signature::{Keypair, Signer, Verifier};
6use ufotofu::codec_prelude::*;
7
8/// A namespace id for testing.
9///
10/// Serves as a "public key" corresopnding to a [`TestNamespaceSecret`].
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Arbitrary)]
12#[allow(missing_docs)]
13pub enum TestNamespace {
14    Family,
15    Wiki,
16    Project,
17}
18
19pub use TestNamespace::*;
20
21impl Encodable for TestNamespace {
22    async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
23    where
24        C: BulkConsumer<Item = u8> + ?Sized,
25    {
26        match self {
27            Family => consumer.consume_item(0).await,
28            Wiki => consumer.consume_item(1).await,
29            Project => consumer.consume_item(2).await,
30        }
31    }
32}
33
34impl EncodableKnownLength for TestNamespace {
35    fn len_of_encoding(&self) -> usize {
36        1
37    }
38}
39
40impl Decodable for TestNamespace {
41    type ErrorReason = Blame;
42
43    async fn decode<P>(
44        producer: &mut P,
45    ) -> Result<Self, DecodeError<P::Final, P::Error, Self::ErrorReason>>
46    where
47        P: BulkProducer<Item = u8> + ?Sized,
48        Self: Sized,
49    {
50        let byte = producer.produce_item().await?;
51
52        match byte {
53            0 => Ok(Family),
54            1 => Ok(Wiki),
55            2 => Ok(Project),
56            _ => Err(DecodeError::Other(Blame::TheirFault)),
57        }
58    }
59}
60
61impl DecodableCanonic for TestNamespace {
62    type ErrorCanonic = Blame;
63
64    async fn decode_canonic<P>(
65        producer: &mut P,
66    ) -> Result<Self, DecodeError<P::Final, P::Error, Self::ErrorCanonic>>
67    where
68        P: BulkProducer<Item = u8> + ?Sized,
69        Self: Sized,
70    {
71        Self::decode(producer).await
72    }
73}
74
75impl Verifier<TestNamespaceSignature> for TestNamespace {
76    fn verify(
77        &self,
78        _msg: &[u8],
79        signature: &TestNamespaceSignature,
80    ) -> Result<(), signature::Error> {
81        match (self, signature) {
82            (Family, FamilySignature) => Ok(()),
83            (Wiki, WikiSignature) => Ok(()),
84            (Project, ProjectSignature) => Ok(()),
85            _ => Err(signature::Error::new()),
86        }
87    }
88}
89
90impl GreatestElement for TestNamespace {
91    fn greatest() -> Self {
92        Project
93    }
94}
95
96impl TrySuccessor for TestNamespace {
97    fn try_successor(&self) -> Option<Self> {
98        match self {
99            Family => Some(Wiki),
100            Wiki => Some(Project),
101            Project => None,
102        }
103    }
104}
105
106impl SuccessorExceptForGreatest for TestNamespace {}
107
108impl LeastElement for TestNamespace {
109    fn least() -> Self {
110        Family
111    }
112}
113
114impl TryPredecessor for TestNamespace {
115    fn try_predecessor(&self) -> Option<Self> {
116        match self {
117            Family => None,
118            Wiki => Some(Family),
119            Project => Some(Wiki),
120        }
121    }
122}
123
124impl PredecessorExceptForLeast for TestNamespace {}
125
126/// A "private key" corresponding to a namespace, used for issuing [`TestNamespaceSignature`]s.
127#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Arbitrary)]
128#[allow(missing_docs)]
129pub enum TestNamespaceSecret {
130    FamilySecret,
131    WikiSecret,
132    ProjectSecret,
133}
134
135pub use TestNamespaceSecret::*;
136
137impl Signer<TestNamespaceSignature> for TestNamespaceSecret {
138    fn try_sign(&self, _msg: &[u8]) -> Result<TestNamespaceSignature, signature::Error> {
139        Ok(match self {
140            FamilySecret => FamilySignature,
141            WikiSecret => WikiSignature,
142            ProjectSecret => ProjectSignature,
143        })
144    }
145}
146
147impl Keypair for TestNamespaceSecret {
148    type VerifyingKey = TestNamespace;
149
150    fn verifying_key(&self) -> Self::VerifyingKey {
151        match self {
152            FamilySecret => Family,
153            WikiSecret => Wiki,
154            ProjectSecret => Project,
155        }
156    }
157}
158
159/// A "signature" issued by a [`TestNamespaceSecret`].
160///
161/// The signing process ignores the actual details of the payload.
162#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Arbitrary)]
163#[allow(missing_docs)]
164pub enum TestNamespaceSignature {
165    FamilySignature,
166    WikiSignature,
167    ProjectSignature,
168}
169
170pub use TestNamespaceSignature::*;
171
172impl Encodable for TestNamespaceSignature {
173    async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
174    where
175        C: BulkConsumer<Item = u8> + ?Sized,
176    {
177        match self {
178            FamilySignature => consumer.consume_item(0).await,
179            WikiSignature => consumer.consume_item(1).await,
180            ProjectSignature => consumer.consume_item(2).await,
181        }
182    }
183}
184
185impl EncodableKnownLength for TestNamespaceSignature {
186    fn len_of_encoding(&self) -> usize {
187        1
188    }
189}
190
191impl Decodable for TestNamespaceSignature {
192    type ErrorReason = Blame;
193
194    async fn decode<P>(
195        producer: &mut P,
196    ) -> Result<Self, DecodeError<P::Final, P::Error, Self::ErrorReason>>
197    where
198        P: BulkProducer<Item = u8> + ?Sized,
199        Self: Sized,
200    {
201        let byte = producer.produce_item().await?;
202
203        match byte {
204            0 => Ok(FamilySignature),
205            1 => Ok(WikiSignature),
206            2 => Ok(ProjectSignature),
207            _ => Err(DecodeError::Other(Blame::TheirFault)),
208        }
209    }
210}
211
212impl DecodableCanonic for TestNamespaceSignature {
213    type ErrorCanonic = Blame;
214
215    async fn decode_canonic<P>(
216        producer: &mut P,
217    ) -> Result<Self, DecodeError<P::Final, P::Error, Self::ErrorCanonic>>
218    where
219        P: BulkProducer<Item = u8> + ?Sized,
220        Self: Sized,
221    {
222        Self::decode(producer).await
223    }
224}