Skip to main content

surrealdb_sql/
algorithm.rs

1use std::fmt;
2
3use surrealdb_types::{SqlFormat, ToSql};
4
5#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Hash)]
6#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
7pub enum Algorithm {
8	EdDSA,
9	Es256,
10	Es384,
11	Es512,
12	Hs256,
13	Hs384,
14	#[default]
15	Hs512,
16	Ps256,
17	Ps384,
18	Ps512,
19	Rs256,
20	Rs384,
21	Rs512,
22}
23
24impl Algorithm {
25	// Does the algorithm use the same key for signing and verification?
26	pub fn is_symmetric(self) -> bool {
27		matches!(self, Algorithm::Hs256 | Algorithm::Hs384 | Algorithm::Hs512)
28	}
29
30	pub fn as_str(self) -> &'static str {
31		match self {
32			Self::EdDSA => "EDDSA",
33			Self::Es256 => "ES256",
34			Self::Es384 => "ES384",
35			Self::Es512 => "ES512",
36			Self::Hs256 => "HS256",
37			Self::Hs384 => "HS384",
38			Self::Hs512 => "HS512",
39			Self::Ps256 => "PS256",
40			Self::Ps384 => "PS384",
41			Self::Ps512 => "PS512",
42			Self::Rs256 => "RS256",
43			Self::Rs384 => "RS384",
44			Self::Rs512 => "RS512",
45		}
46	}
47}
48
49impl fmt::Display for Algorithm {
50	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51		f.write_str(self.as_str())
52	}
53}
54
55impl ToSql for Algorithm {
56	fn fmt_sql(&self, f: &mut String, _fmt: SqlFormat) {
57		f.push_str(&self.to_string())
58	}
59}