Skip to main content

surql_parser/upstream/sql/
algorithm.rs

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