Skip to main content

asimov_id/
public_key_encoding.rs

1// This is free and unencumbered software released into the public domain.
2
3use derive_more::{Display, FromStr};
4
5#[derive(Clone, Copy, Debug, Default, Display, Eq, FromStr, Hash, Ord, PartialEq, PartialOrd)]
6#[non_exhaustive]
7pub enum PublicKeyEncoding {
8    #[default]
9    Asimov,
10
11    Base58,
12
13    #[cfg(feature = "base64")]
14    Base64,
15
16    #[cfg(feature = "base64")]
17    Base64Url,
18
19    #[cfg(feature = "hex")]
20    Hex,
21
22    Jwk,
23
24    Near,
25
26    OpenSsh,
27
28    Pem,
29
30    #[cfg(feature = "z32")]
31    Z32,
32}
33
34#[cfg(any(feature = "base64", feature = "hex", feature = "z32"))]
35impl TryFrom<PublicKeyEncoding> for data_encoding::Encoding {
36    type Error = ();
37
38    fn try_from(input: PublicKeyEncoding) -> Result<Self, Self::Error> {
39        Ok(match input {
40            PublicKeyEncoding::Asimov => return Err(()),
41            PublicKeyEncoding::Base58 => return Err(()),
42            PublicKeyEncoding::Base64 => data_encoding::BASE64,
43            PublicKeyEncoding::Base64Url => data_encoding::BASE64URL_NOPAD,
44            PublicKeyEncoding::Hex => data_encoding::HEXLOWER,
45            PublicKeyEncoding::Jwk => return Err(()),
46            PublicKeyEncoding::Near => return Err(()),
47            PublicKeyEncoding::OpenSsh => return Err(()),
48            PublicKeyEncoding::Pem => return Err(()),
49            PublicKeyEncoding::Z32 => data_encoding::BASE32,
50        })
51    }
52}