signatory/key/
name.rs

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
//! Key names: unambiguous identifier strings.

use crate::{Error, Result};
use alloc::string::String;
use core::{
    fmt::{self, Display},
    ops::Deref,
    str::FromStr,
};

/// Key names.
///
/// These are constrained to the following characters:
/// - Letters: `a-z`, A-Z`
/// - Numbers: `0-9`
/// - Delimiters: `-`, `_`
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct KeyName(String);

impl KeyName {
    /// Create a new key name from the given string.
    pub fn new(name: impl Into<String>) -> Result<Self> {
        let name = name.into();

        if !name
            .as_bytes()
            .iter()
            .all(|&byte| matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-' | b'_'))
        {
            return Err(Error::KeyNameInvalid);
        }

        Ok(Self(name))
    }
}

impl AsRef<str> for KeyName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

#[cfg(feature = "std")]
impl AsRef<std::path::Path> for KeyName {
    fn as_ref(&self) -> &std::path::Path {
        self.0.as_ref()
    }
}

impl Deref for KeyName {
    type Target = str;

    fn deref(&self) -> &str {
        &self.0
    }
}

impl Display for KeyName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for KeyName {
    type Err = Error;

    fn from_str(name: &str) -> Result<Self> {
        Self::new(name)
    }
}