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
114
//! Identifiers module
//!

pub mod derive;
pub(crate) mod digest_identifier;
pub(crate) mod error;
pub(crate) mod key_identifier;
pub(crate) mod signature_identifier;

pub use digest_identifier::DigestIdentifier;
pub use key_identifier::KeyIdentifier;
pub use signature_identifier::SignatureIdentifier;

use base64::encode_config;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;

use self::error::Error;

/// Enumeration of Identifier types
#[derive(PartialEq, Debug, Clone, Eq, Hash)]
pub(crate) enum Identifier {
    Key(KeyIdentifier),
    Digest(DigestIdentifier),
    Sign(SignatureIdentifier),
}

impl Identifier {
    /*pub fn payload(&self) -> Vec<u8> {
        match self {
            Identifier::Key(value) => value.public_key.clone(),
            Identifier::Digest(value) => value.digest.clone(),
            Identifier::Sign(value) => value.signature.clone(),
        }
    }*/
}

/// Default implementation for Identifiers
impl Default for Identifier {
    fn default() -> Self {
        Self::Digest(DigestIdentifier::default())
    }
}

/// Derivable Identifiers
pub trait Derivable: FromStr<Err = Error> {
    fn derivative(&self) -> Vec<u8>;

    fn derivation_code(&self) -> String;

    fn to_str(&self) -> String {
        match self.derivative().len() {
            0 => "".to_string(),
            _ => [
                self.derivation_code(),
                encode_config(self.derivative(), base64::URL_SAFE_NO_PAD),
            ]
            .join(""),
        }
    }
}

impl FromStr for Identifier {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(id) = DigestIdentifier::from_str(s) {
            Ok(Identifier::Digest(id))
        } else if let Ok(id) = KeyIdentifier::from_str(s) {
            Ok(Identifier::Key(id))
        } else if let Ok(id) = SignatureIdentifier::from_str(s) {
            Ok(Identifier::Sign(id))
        } else {
            Err(Error::SemanticError(format!("Incorrect Identifier: {}", s)))
        }
    }
}

impl Derivable for Identifier {
    fn derivative(&self) -> Vec<u8> {
        match self {
            Identifier::Key(d) => d.derivative(),
            Identifier::Digest(d) => d.derivative(),
            Identifier::Sign(d) => d.derivative(),
        }
    }

    fn derivation_code(&self) -> String {
        match self {
            Identifier::Key(d) => d.derivation_code(),
            Identifier::Digest(d) => d.derivation_code(),
            Identifier::Sign(d) => d.derivation_code(),
        }
    }
}

impl Serialize for Identifier {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_str())
    }
}

impl<'de> Deserialize<'de> for Identifier {
    fn deserialize<D>(deserializer: D) -> Result<Identifier, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Identifier::from_str(&s).map_err(serde::de::Error::custom)
    }
}