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
//! Text encoding identifiers for oboron output.
use crate::error::Error;
/// Encoding identifier for text representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
B32,
C32,
B64,
Hex,
}
impl Encoding {
/// Convert encoding to string representation.
pub fn as_long_str(&self) -> &'static str {
match self {
Encoding::C32 => "base32crockford",
Encoding::B32 => "base32rfc",
Encoding::B64 => "base64",
Encoding::Hex => "hex",
}
}
/// Convert encoding to abbreviated string representation (for format strings).
pub fn as_str(&self) -> &'static str {
match self {
Encoding::C32 => "c32",
Encoding::B32 => "b32",
Encoding::B64 => "b64",
Encoding::Hex => "hex",
}
}
/// Parse encoding from string.
// Intentional inherent shortcut alongside the `FromStr` impl, so
// callers can write `Encoding::from_str(s)` without the trait import.
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Result<Self, Error> {
s.parse()
}
}
impl std::str::FromStr for Encoding {
type Err = Error;
/// Parse an encoding from its identifier.
///
/// Accepts the short tokens (`b32`, `c32`, `b64`, `hex`) and the
/// long aliases (`base32crockford`, `base32rfc`, `base64`),
/// case-insensitively — a deliberate ergonomics choice, more
/// lenient than the case-sensitive tokens of spec §1.1.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"b32" => Ok(Encoding::B32),
"c32" => Ok(Encoding::C32),
"b64" => Ok(Encoding::B64),
"hex" => Ok(Encoding::Hex),
// Long names
"base32crockford" => Ok(Encoding::C32),
"base32rfc" => Ok(Encoding::B32),
"base64" => Ok(Encoding::B64),
_ => Err(Error::UnknownEncoding),
}
}
}
impl std::fmt::Display for Encoding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}