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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::{fmt::{Formatter, Display}, borrow::Cow};

use bc_components::{tags, DigestProvider, Digest};
use dcbor::prelude::*;

use crate::{EnvelopeEncodable, Envelope};

#[derive(Debug, Clone)]
enum KnownValueName {
    Static(&'static str),
    Dynamic(String),
}

/// A value in a namespace of unsigned integers, frequently used as predicates.
///
/// Known values are a specific case of envelope that defines a namespace consisting
/// of single unsigned integers. The expectation is that the most common and widely
/// useful predicates will be assigned in this namespace, but known values may be
/// used in any position in an envelope.
#[derive(Clone, Debug)]
pub struct KnownValue {
    /// The known value as coded into CBOR.
    value: u64,
    /// A name assigned to the known value used for debugging and formatted output.
    assigned_name: Option<KnownValueName>,
}

impl KnownValue {
    /// Create a known value with the given value and no name.
    pub fn new(value: u64) -> Self {
        Self { value, assigned_name: None }
    }

    /// Create a known value with the given value and associated name.
    pub fn new_with_name<T: Into<u64>>(value: T, assigned_name: String) -> Self {
        Self { value: value.into(), assigned_name: Some(KnownValueName::Dynamic(assigned_name)) }
    }

    /// Creates a known value at compile time with the given value and associated name.
    pub const fn new_with_static_name(value: u64, name: &'static str) -> Self {
        Self { value, assigned_name: Some(KnownValueName::Static(name)) }
    }

    /// The known value as coded into CBOR.
    pub fn value(&self) -> u64 {
        self.value
    }

    /// The name assigned to the known value.
    pub fn assigned_name(&self) -> Option<&str> {
        match &self.assigned_name {
            Some(KnownValueName::Static(name)) => Some(name),
            Some(KnownValueName::Dynamic(name)) => Some(name),
            None => None,
        }
    }

    /// The human readable name.
    ///
    /// Defaults to the numerical value if no name has been assigned.
    pub fn name(&self) -> String {
        match &self.assigned_name {
            Some(KnownValueName::Static(name)) => name.to_string(),
            Some(KnownValueName::Dynamic(name)) => name.clone(),
            None => self.value.to_string(),
        }
    }
}

impl PartialEq for KnownValue {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl Eq for KnownValue { }

impl std::hash::Hash for KnownValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value.hash(state);
    }
}

impl Display for KnownValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.assigned_name {
            Some(KnownValueName::Static(name)) => write!(f, "{}", name),
            Some(KnownValueName::Dynamic(name)) => write!(f, "{}", name),
            None => write!(f, "{}", self.value),
        }
    }
}

impl EnvelopeEncodable for KnownValue {
    fn envelope(self) -> Envelope {
        Envelope::new_with_known_value(self)
    }
}

impl From<KnownValue> for Envelope {
    fn from(known_value: KnownValue) -> Self {
        known_value.envelope()
    }
}

impl DigestProvider for KnownValue {
    fn digest(&self) -> Cow<'_, Digest> {
        Cow::Owned(Digest::from_image(self.tagged_cbor().cbor_data()))
    }
}

impl CBORTagged for KnownValue {
    fn cbor_tags() -> Vec<Tag> {
        vec![tags::KNOWN_VALUE]
    }
}

impl CBOREncodable for KnownValue {
    fn cbor(&self) -> CBOR {
        self.tagged_cbor()
    }
}

impl From<KnownValue> for CBOR {
    fn from(value: KnownValue) -> Self {
        value.cbor()
    }
}

impl CBORDecodable for KnownValue {
    fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
        Self::from_tagged_cbor(cbor)
    }
}

impl TryFrom<CBOR> for KnownValue {
    type Error = anyhow::Error;

    fn try_from(cbor: CBOR) -> Result<Self, Self::Error> {
        Self::from_cbor(&cbor)
    }
}

impl CBORCodable for KnownValue { }

impl CBORTaggedEncodable for KnownValue {
    fn untagged_cbor(&self) -> CBOR {
        self.value.cbor()
    }
}

impl CBORTaggedDecodable for KnownValue {
    fn from_untagged_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
        let value = u64::from_cbor(cbor)?;
        Ok(Self::new(value))
    }
}

impl CBORTaggedCodable for KnownValue { }

impl From<u64> for KnownValue {
    fn from(value: u64) -> Self {
        KnownValue::new(value)
    }
}

impl From<i32> for KnownValue {
    fn from(value: i32) -> Self {
        KnownValue::new(value as u64)
    }
}

impl From<usize> for KnownValue {
    fn from(value: usize) -> Self {
        KnownValue::new(value as u64)
    }
}