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]
11pub enum Algorithm {
12	EdDSA,
13	Es256,
14	Es384,
15	Es512,
16	Hs256,
17	Hs384,
18	Hs512,
19	Ps256,
20	Ps384,
21	Ps512,
22	Rs256,
23	Rs384,
24	Rs512,
25}
26
27impl Algorithm {
28	// Does the algorithm use the same key for signing and verification?
29	pub(crate) fn is_symmetric(self) -> bool {
30		matches!(self, Algorithm::Hs256 | Algorithm::Hs384 | Algorithm::Hs512)
31	}
32}
33
34impl From<Algorithm> for jsonwebtoken::Algorithm {
35	fn from(val: Algorithm) -> Self {
36		match val {
37			Algorithm::Hs256 => jsonwebtoken::Algorithm::HS256,
38			Algorithm::Hs384 => jsonwebtoken::Algorithm::HS384,
39			Algorithm::Hs512 => jsonwebtoken::Algorithm::HS512,
40			Algorithm::EdDSA => jsonwebtoken::Algorithm::EdDSA,
41			Algorithm::Es256 => jsonwebtoken::Algorithm::ES256,
42			Algorithm::Es384 => jsonwebtoken::Algorithm::ES384,
43			Algorithm::Es512 => jsonwebtoken::Algorithm::ES384,
44			Algorithm::Ps256 => jsonwebtoken::Algorithm::PS256,
45			Algorithm::Ps384 => jsonwebtoken::Algorithm::PS384,
46			Algorithm::Ps512 => jsonwebtoken::Algorithm::PS512,
47			Algorithm::Rs256 => jsonwebtoken::Algorithm::RS256,
48			Algorithm::Rs384 => jsonwebtoken::Algorithm::RS384,
49			Algorithm::Rs512 => jsonwebtoken::Algorithm::RS512,
50		}
51	}
52}
53
54impl Default for Algorithm {
55	fn default() -> Self {
56		Self::Hs512
57	}
58}
59
60impl fmt::Display for Algorithm {
61	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62		f.write_str(match self {
63			Self::EdDSA => "EDDSA",
64			Self::Es256 => "ES256",
65			Self::Es384 => "ES384",
66			Self::Es512 => "ES512",
67			Self::Hs256 => "HS256",
68			Self::Hs384 => "HS384",
69			Self::Hs512 => "HS512",
70			Self::Ps256 => "PS256",
71			Self::Ps384 => "PS384",
72			Self::Ps512 => "PS512",
73			Self::Rs256 => "RS256",
74			Self::Rs384 => "RS384",
75			Self::Rs512 => "RS512",
76		})
77	}
78}
79
80impl InfoStructure for Algorithm {
81	fn structure(self) -> Value {
82		self.to_string().into()
83	}
84}