surrealdb_core/sql/
algorithm.rs

1use crate::sql::statements::info::InfoStructure;
2use crate::sql::Value;
3use revision::revisioned;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7#[revisioned(revision = 1)]
8#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
9#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
10#[non_exhaustive]
11#[derive(Default)]
12pub enum Algorithm {
13	EdDSA,
14	Es256,
15	Es384,
16	Es512,
17	Hs256,
18	Hs384,
19	#[default]
20	Hs512,
21	Ps256,
22	Ps384,
23	Ps512,
24	Rs256,
25	Rs384,
26	Rs512,
27}
28
29impl Algorithm {
30	// Does the algorithm use the same key for signing and verification?
31	pub(crate) fn is_symmetric(self) -> bool {
32		matches!(self, Algorithm::Hs256 | Algorithm::Hs384 | Algorithm::Hs512)
33	}
34}
35
36impl From<Algorithm> for jsonwebtoken::Algorithm {
37	fn from(val: Algorithm) -> Self {
38		match val {
39			Algorithm::Hs256 => jsonwebtoken::Algorithm::HS256,
40			Algorithm::Hs384 => jsonwebtoken::Algorithm::HS384,
41			Algorithm::Hs512 => jsonwebtoken::Algorithm::HS512,
42			Algorithm::EdDSA => jsonwebtoken::Algorithm::EdDSA,
43			Algorithm::Es256 => jsonwebtoken::Algorithm::ES256,
44			Algorithm::Es384 => jsonwebtoken::Algorithm::ES384,
45			Algorithm::Es512 => jsonwebtoken::Algorithm::ES384,
46			Algorithm::Ps256 => jsonwebtoken::Algorithm::PS256,
47			Algorithm::Ps384 => jsonwebtoken::Algorithm::PS384,
48			Algorithm::Ps512 => jsonwebtoken::Algorithm::PS512,
49			Algorithm::Rs256 => jsonwebtoken::Algorithm::RS256,
50			Algorithm::Rs384 => jsonwebtoken::Algorithm::RS384,
51			Algorithm::Rs512 => jsonwebtoken::Algorithm::RS512,
52		}
53	}
54}
55
56impl fmt::Display for Algorithm {
57	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58		f.write_str(match self {
59			Self::EdDSA => "EDDSA",
60			Self::Es256 => "ES256",
61			Self::Es384 => "ES384",
62			Self::Es512 => "ES512",
63			Self::Hs256 => "HS256",
64			Self::Hs384 => "HS384",
65			Self::Hs512 => "HS512",
66			Self::Ps256 => "PS256",
67			Self::Ps384 => "PS384",
68			Self::Ps512 => "PS512",
69			Self::Rs256 => "RS256",
70			Self::Rs384 => "RS384",
71			Self::Rs512 => "RS512",
72		})
73	}
74}
75
76impl InfoStructure for Algorithm {
77	fn structure(self) -> Value {
78		self.to_string().into()
79	}
80}