Skip to main content

surrealdb_expr/expr/
algorithm.rs

1use std::fmt;
2
3use crate::expr::Value;
4use crate::expr::statements::info::InfoStructure;
5
6#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
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
31impl From<Algorithm> for jsonwebtoken::Algorithm {
32	fn from(val: Algorithm) -> Self {
33		match val {
34			Algorithm::Hs256 => jsonwebtoken::Algorithm::HS256,
35			Algorithm::Hs384 => jsonwebtoken::Algorithm::HS384,
36			Algorithm::Hs512 => jsonwebtoken::Algorithm::HS512,
37			Algorithm::EdDSA => jsonwebtoken::Algorithm::EdDSA,
38			Algorithm::Es256 => jsonwebtoken::Algorithm::ES256,
39			Algorithm::Es384 => jsonwebtoken::Algorithm::ES384,
40			Algorithm::Es512 => jsonwebtoken::Algorithm::ES384,
41			Algorithm::Ps256 => jsonwebtoken::Algorithm::PS256,
42			Algorithm::Ps384 => jsonwebtoken::Algorithm::PS384,
43			Algorithm::Ps512 => jsonwebtoken::Algorithm::PS512,
44			Algorithm::Rs256 => jsonwebtoken::Algorithm::RS256,
45			Algorithm::Rs384 => jsonwebtoken::Algorithm::RS384,
46			Algorithm::Rs512 => jsonwebtoken::Algorithm::RS512,
47		}
48	}
49}
50
51impl fmt::Display for Algorithm {
52	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53		f.write_str(match self {
54			Self::EdDSA => "EDDSA",
55			Self::Es256 => "ES256",
56			Self::Es384 => "ES384",
57			Self::Es512 => "ES512",
58			Self::Hs256 => "HS256",
59			Self::Hs384 => "HS384",
60			Self::Hs512 => "HS512",
61			Self::Ps256 => "PS256",
62			Self::Ps384 => "PS384",
63			Self::Ps512 => "PS512",
64			Self::Rs256 => "RS256",
65			Self::Rs384 => "RS384",
66			Self::Rs512 => "RS512",
67		})
68	}
69}
70
71impl InfoStructure for Algorithm {
72	fn structure(self) -> Value {
73		self.to_string().into()
74	}
75}