1pub mod codes;
2pub mod parsers;
3use chrono::{DateTime, FixedOffset};
4
5use crate::conversion::from_bytes_to_text;
6
7use self::codes::{
8 attached_signature_code::AttachedSignatureCode, basic::Basic, self_addressing::SelfAddressing,
9 self_signing::SelfSigning, PrimitiveCode,
10};
11
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub enum IdentifierCode {
14 Basic(Basic),
15 SelfAddressing(SelfAddressing),
16}
17
18pub type Identifier = (IdentifierCode, Vec<u8>);
19pub type PublicKey = (Basic, Vec<u8>);
20pub type Digest = (SelfAddressing, Vec<u8>);
21pub type Signature = (SelfSigning, Vec<u8>);
22pub type IndexedSignature = (AttachedSignatureCode, Vec<u8>);
23pub type Timestamp = DateTime<FixedOffset>;
24pub type TransferableQuadruple = (Identifier, u64, Digest, Vec<IndexedSignature>);
25pub type IdentifierSignaturesCouple = (Identifier, Vec<IndexedSignature>);
26
27pub trait CesrPrimitive {
28 fn derivative(&self) -> Vec<u8>;
29 fn derivation_code(&self) -> PrimitiveCode;
30 fn to_str(&self) -> String {
31 match self.derivative().len() {
32 0 => "".to_string(),
34 _ => {
35 let dc = self.derivation_code().to_str();
36 let lead_bytes = if dc.len() % 4 != 0 { dc.len() % 4 } else { 0 };
37 let derivative_text =
39 from_bytes_to_text(&self.derivative())[lead_bytes..].to_string();
40 [dc, derivative_text].join("")
41 }
42 }
43 }
44}
45
46impl CesrPrimitive for Digest {
47 fn derivative(&self) -> Vec<u8> {
48 self.1.clone()
49 }
50
51 fn derivation_code(&self) -> PrimitiveCode {
52 PrimitiveCode::SelfAddressing(self.0.clone())
53 }
54}
55
56impl CesrPrimitive for Signature {
57 fn derivative(&self) -> Vec<u8> {
58 self.1.clone()
59 }
60
61 fn derivation_code(&self) -> PrimitiveCode {
62 PrimitiveCode::SelfSigning(self.0)
63 }
64}
65
66impl CesrPrimitive for IndexedSignature {
67 fn derivative(&self) -> Vec<u8> {
68 self.1.clone()
69 }
70
71 fn derivation_code(&self) -> PrimitiveCode {
72 PrimitiveCode::IndexedSignature(self.0)
73 }
74}
75
76impl CesrPrimitive for PublicKey {
77 fn derivative(&self) -> Vec<u8> {
78 self.1.clone()
79 }
80
81 fn derivation_code(&self) -> PrimitiveCode {
82 PrimitiveCode::Basic(self.0)
83 }
84}
85
86impl CesrPrimitive for Identifier {
87 fn derivative(&self) -> Vec<u8> {
88 self.1.clone()
89 }
90
91 fn derivation_code(&self) -> PrimitiveCode {
92 match &self.0 {
93 IdentifierCode::Basic(b) => PrimitiveCode::Basic(*b),
94 IdentifierCode::SelfAddressing(sa) => PrimitiveCode::SelfAddressing(sa.clone()),
95 }
96 }
97}