bc-envelope 0.43.0

Gordian Envelope for Rust.
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use bc_components::tags;
use dcbor::prelude::*;

use super::FunctionsStore;
use crate::{Envelope, EnvelopeEncodable, string_utils::StringUtils};

/// Internal representation of a function name, which can be either static or
/// dynamic.
///
/// Static names are &'static str references, typically used for compile-time
/// constants. Dynamic names are owned String instances, used for
/// runtime-created functions.
#[derive(Clone, Debug, Eq)]
pub enum FunctionName {
    /// A function name represented as a static string reference.
    Static(&'static str),
    /// A function name represented as a dynamic (owned) string.
    Dynamic(String),
}

impl FunctionName {
    /// Returns the string value of this function name.
    fn value(&self) -> &str {
        match self {
            Self::Static(name) => name,
            Self::Dynamic(name) => name,
        }
    }
}

/// Implementation of equality for FunctionName based on the string value.
impl PartialEq for FunctionName {
    fn eq(&self, other: &Self) -> bool { self.value() == other.value() }
}

/// Implementation of hashing for FunctionName based on the string value.
impl std::hash::Hash for FunctionName {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value().hash(state)
    }
}

/// A function identifier used in Gordian Envelope expressions.
///
/// In Gordian Envelope, a function appears as the subject of an expression
/// envelope, with its parameters as assertions on that envelope.
///
/// Functions can be identified in two ways:
/// 1. By a numeric ID (for well-known functions)
/// 2. By a string name (for application-specific or less common functions)
///
/// When encoded in CBOR, functions are tagged with #6.40006.
///
/// # Examples
///
/// A numeric function:
///
/// ```
/// use bc_envelope::prelude::*;
///
/// // Define a function with a numeric ID
/// let add_function = Function::new_known(1, Some("add".to_string()));
/// ```
///
/// A named function:
///
/// ```
/// use bc_envelope::prelude::*;
///
/// // Define a function with a string name
/// let verify_function = Function::new_named("verifySignature");
/// ```
///
/// A function with static name:
///
/// ```
/// use bc_envelope::prelude::*;
///
/// // Define a function with a numeric ID and static name
/// const ADD: Function = Function::new_with_static_name(1, "add");
/// ```
#[derive(Debug, Clone, Eq)]
pub enum Function {
    /// A well-known function identified by a numeric ID, with an optional name.
    Known(u64, Option<FunctionName>),
    /// A function identified by a name.
    Named(FunctionName),
}

impl Function {
    /// Creates a new function with a numeric ID and an optional string name.
    ///
    /// This creates a "known" function, which is identified primarily by its
    /// numeric ID. The optional name is used for display purposes.
    ///
    /// # Parameters
    ///
    /// * `value` - The numeric ID of the function
    /// * `name` - An optional name for the function
    ///
    /// # Returns
    ///
    /// A new `Function` instance
    ///
    /// # Examples
    ///
    /// ```
    /// use bc_envelope::prelude::*;
    ///
    /// // Create a function with ID 1 and name "add"
    /// let add_function = Function::new_known(1, Some("add".to_string()));
    /// ```
    pub fn new_known(value: u64, name: Option<String>) -> Self {
        Self::Known(value, name.map(FunctionName::Dynamic))
    }

    /// Creates a new function identified by a string name.
    ///
    /// This creates a "named" function, which is identified by its string name.
    /// This method cannot be used to declare a function at compile-time, as it
    /// creates a dynamic string.
    ///
    /// # Parameters
    ///
    /// * `name` - The string name of the function
    ///
    /// # Returns
    ///
    /// A new `Function` instance
    ///
    /// # Examples
    ///
    /// ```
    /// use bc_envelope::prelude::*;
    ///
    /// // Create a named function
    /// let verify_function = Function::new_named("verifySignature");
    /// ```
    pub fn new_named(name: &str) -> Self {
        Self::Named(FunctionName::Dynamic(name.into()))
    }

    /// Creates a new function with a numeric ID and a static string name.
    ///
    /// This creates a "known" function, which is identified primarily by its
    /// numeric ID. The static name is used for display purposes. This
    /// method can be used to declare a function at compile-time, as it uses
    /// a static string reference.
    ///
    /// # Parameters
    ///
    /// * `value` - The numeric ID of the function
    /// * `name` - A static string name for the function
    ///
    /// # Returns
    ///
    /// A new `Function` instance
    ///
    /// # Examples
    ///
    /// ```
    /// use bc_envelope::prelude::*;
    ///
    /// // Define a function constant
    /// const ADD: Function = Function::new_with_static_name(1, "add");
    /// ```
    pub const fn new_with_static_name(value: u64, name: &'static str) -> Self {
        Self::Known(value, Some(FunctionName::Static(name)))
    }

    /// Creates a new function identified by a static string name.
    ///
    /// This creates a "named" function, which is identified by its string name.
    /// This method can be used to declare a function at compile-time, as it
    /// uses a static string reference.
    ///
    /// # Parameters
    ///
    /// * `name` - A static string name for the function
    ///
    /// # Returns
    ///
    /// A new `Function` instance
    ///
    /// # Examples
    ///
    /// ```
    /// use bc_envelope::prelude::*;
    ///
    /// // Define a named function constant
    /// const VERIFY: Function = Function::new_static_named("verifySignature");
    /// ```
    pub const fn new_static_named(name: &'static str) -> Self {
        Self::Named(FunctionName::Static(name))
    }

    /// Returns the display name of the function.
    ///
    /// For known functions with a name, returns the name.
    /// For known functions without a name, returns the numeric ID as a string.
    /// For named functions, returns the name enclosed in quotes.
    ///
    /// # Returns
    ///
    /// A string representation of the function name
    ///
    /// # Examples
    ///
    /// ```
    /// use bc_envelope::prelude::*;
    ///
    /// let add = Function::new_known(1, Some("add".to_string()));
    /// assert_eq!(add.name(), "add");
    ///
    /// let unknown = Function::new_known(42, None);
    /// assert_eq!(unknown.name(), "42");
    ///
    /// let verify = Function::new_named("verifySignature");
    /// assert_eq!(verify.name(), "\"verifySignature\"");
    /// ```
    pub fn name(&self) -> String {
        match self {
            Self::Known(value, name) => {
                if let Some(name) = name {
                    name.value().to_string()
                } else {
                    value.to_string()
                }
            }
            Self::Named(name) => {
                name.value().to_string().flanked_by("\"", "\"")
            }
        }
    }

    /// Returns the name of a named function, if available.
    ///
    /// This method returns the raw string name for named functions, without
    /// quotes. For known functions (numeric IDs), it returns None.
    ///
    /// # Returns
    ///
    /// An Option containing the function name string if this is a named
    /// function, or None if it's a known (numeric) function.
    ///
    /// # Examples
    ///
    /// ```
    /// use bc_envelope::prelude::*;
    ///
    /// let add = Function::new_known(1, Some("add".to_string()));
    /// assert_eq!(add.named_name(), None);
    ///
    /// let verify = Function::new_named("verifySignature");
    /// assert_eq!(verify.named_name(), Some("verifySignature".to_string()));
    /// ```
    pub fn named_name(&self) -> Option<String> {
        match self {
            Self::Known(_, _) => None,
            Self::Named(name) => Some(name.value().to_string()),
        }
    }
}

/// Implementation of equality for Function.
///
/// Known functions are equal if they have the same numeric ID (names are
/// ignored). Named functions are equal if they have the same name.
/// Known and named functions are never equal to each other.
impl PartialEq for Function {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Function::Known(l, _), Function::Known(r, _)) => l == r,
            (Function::Named(l), Function::Named(r)) => l == r,
            _ => false,
        }
    }
}

/// Implementation of hash for Function.
///
/// Known functions are hashed by their numeric ID.
/// Named functions are hashed by their name.
impl std::hash::Hash for Function {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            Function::Known(value, _) => value.hash(state),
            Function::Named(name) => name.hash(state),
        }
    }
}

/// Allows creating a Function from a u64.
///
/// This creates a known function with the given numeric ID and no name.
impl From<u64> for Function {
    fn from(value: u64) -> Self { Self::new_known(value, None) }
}

/// Allows creating a Function from a string reference.
///
/// This creates a named function with the given name.
impl From<&str> for Function {
    fn from(name: &str) -> Self { Self::new_named(name) }
}

/// Allows creating a Function from a reference to a Function.
///
/// This clones the function.
impl From<&Function> for Function {
    fn from(function: &Function) -> Self { function.clone() }
}

/// Implementation of the CBORTagged trait for Function.
///
/// Functions are tagged with #6.40006 (TAG_FUNCTION).
impl CBORTagged for Function {
    fn cbor_tags() -> Vec<Tag> { tags_for_values(&[tags::TAG_FUNCTION]) }
}

/// Allows creating a CBOR value from a Function.
impl From<Function> for CBOR {
    fn from(value: Function) -> Self { value.tagged_cbor() }
}

/// Implementation of the CBORTaggedEncodable trait for Function.
///
/// Known functions are encoded as unsigned integers.
/// Named functions are encoded as text strings.
impl CBORTaggedEncodable for Function {
    fn untagged_cbor(&self) -> CBOR {
        match self {
            Function::Known(value, _) => (*value).into(),
            Function::Named(name) => name.value().into(),
        }
    }
}

/// Allows creating a Function from a CBOR value.
impl TryFrom<CBOR> for Function {
    type Error = dcbor::Error;

    fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
        Self::from_tagged_cbor(cbor)
    }
}

/// Implementation of the CBORTaggedDecodable trait for Function.
///
/// Unsigned integers are decoded as known functions.
/// Text strings are decoded as named functions.
impl CBORTaggedDecodable for Function {
    fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
        match untagged_cbor.as_case() {
            CBORCase::Unsigned(value) => Ok(Self::new_known(*value, None)),
            CBORCase::Text(name) => Ok(Self::new_named(name)),
            _ => Err("invalid function".into()),
        }
    }
}

impl Function {
    /// Returns a description of this function for display.
    ///
    /// For known functions, attempts to look up the name in the provided
    /// functions store, if available.
    /// For named functions, returns the name in quotes.
    fn description(&self, functions: Option<&FunctionsStore>) -> String {
        match self {
            Function::Known(_, _) => {
                FunctionsStore::name_for_function(self, functions)
            }
            Function::Named(name) => {
                format!("\"{}\"", name.value())
            }
        }
    }
}

/// Implements display for Function.
///
/// Uses the description method with no functions store.
impl std::fmt::Display for Function {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description(None))
    }
}

/// Implements the EnvelopeEncodable trait for Function.
///
/// This allows a Function to be directly converted to an Envelope.
impl EnvelopeEncodable for Function {
    fn into_envelope(self) -> Envelope { Envelope::new_leaf(self) }
}

/// Implements conversion from Envelope to Function.
///
/// This attempts to extract a Function from an Envelope leaf.
impl TryFrom<Envelope> for Function {
    type Error = dcbor::Error;

    fn try_from(envelope: Envelope) -> dcbor::Result<Self> {
        Function::try_from(envelope.try_leaf().map_err(|e| e.to_string())?)
    }
}