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