biscuit_auth/token/builder/
algorithm.rs

1use core::fmt::Display;
2use std::{
3    convert::{TryFrom, TryInto},
4    str::FromStr,
5};
6
7use crate::error;
8
9#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
10pub enum Algorithm {
11    Ed25519,
12    Secp256r1,
13}
14
15impl Algorithm {
16    pub fn values() -> &'static [Self] {
17        &[Self::Ed25519, Self::Secp256r1]
18    }
19}
20
21impl Default for Algorithm {
22    fn default() -> Self {
23        Self::Ed25519
24    }
25}
26
27impl Display for Algorithm {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Algorithm::Ed25519 => write!(f, "ed25519"),
31            Algorithm::Secp256r1 => write!(f, "secp256r1"),
32        }
33    }
34}
35impl FromStr for Algorithm {
36    type Err = error::Format;
37
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        s.try_into()
40    }
41}
42
43impl TryFrom<&str> for Algorithm {
44    type Error = error::Format;
45    fn try_from(value: &str) -> Result<Self, Self::Error> {
46        match value {
47            "ed25519" => Ok(Algorithm::Ed25519),
48            "secp256r1" => Ok(Algorithm::Secp256r1),
49            _ => Err(error::Format::DeserializationError(format!(
50                "deserialization error: unexpected key algorithm {}",
51                value
52            ))),
53        }
54    }
55}
56
57impl From<biscuit_parser::builder::Algorithm> for Algorithm {
58    fn from(value: biscuit_parser::builder::Algorithm) -> Algorithm {
59        match value {
60            biscuit_parser::builder::Algorithm::Ed25519 => Algorithm::Ed25519,
61            biscuit_parser::builder::Algorithm::Secp256r1 => Algorithm::Secp256r1,
62        }
63    }
64}
65
66impl From<Algorithm> for biscuit_parser::builder::Algorithm {
67    fn from(value: Algorithm) -> biscuit_parser::builder::Algorithm {
68        match value {
69            Algorithm::Ed25519 => biscuit_parser::builder::Algorithm::Ed25519,
70            Algorithm::Secp256r1 => biscuit_parser::builder::Algorithm::Secp256r1,
71        }
72    }
73}
74
75impl From<crate::format::schema::public_key::Algorithm> for Algorithm {
76    fn from(value: crate::format::schema::public_key::Algorithm) -> Algorithm {
77        match value {
78            crate::format::schema::public_key::Algorithm::Ed25519 => Algorithm::Ed25519,
79            crate::format::schema::public_key::Algorithm::Secp256r1 => Algorithm::Secp256r1,
80        }
81    }
82}
83
84impl From<Algorithm> for crate::format::schema::public_key::Algorithm {
85    fn from(value: Algorithm) -> crate::format::schema::public_key::Algorithm {
86        match value {
87            Algorithm::Ed25519 => crate::format::schema::public_key::Algorithm::Ed25519,
88            Algorithm::Secp256r1 => crate::format::schema::public_key::Algorithm::Secp256r1,
89        }
90    }
91}