Skip to main content

bitwarden_api_api/models/
algorithm.rs

1/*
2 * Bitwarden Internal API
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: latest
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
12
13use crate::models;
14///
15#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
16pub enum Algorithm {
17    RS1,
18    RS512,
19    RS384,
20    RS256,
21    ES256K,
22    PS512,
23    PS384,
24    PS256,
25    ES512,
26    ES384,
27    EdDSA,
28    ES256,
29
30    /// Unknown value returned from the server. This is used to handle forward compatibility.
31    __Unknown(i64),
32}
33
34impl Algorithm {
35    pub fn as_i64(&self) -> i64 {
36        match self {
37            Self::RS1 => -65535,
38            Self::RS512 => -259,
39            Self::RS384 => -258,
40            Self::RS256 => -257,
41            Self::ES256K => -47,
42            Self::PS512 => -39,
43            Self::PS384 => -38,
44            Self::PS256 => -37,
45            Self::ES512 => -36,
46            Self::ES384 => -35,
47            Self::EdDSA => -8,
48            Self::ES256 => -7,
49            Self::__Unknown(v) => *v,
50        }
51    }
52
53    pub fn from_i64(value: i64) -> Self {
54        match value {
55            -65535 => Self::RS1,
56            -259 => Self::RS512,
57            -258 => Self::RS384,
58            -257 => Self::RS256,
59            -47 => Self::ES256K,
60            -39 => Self::PS512,
61            -38 => Self::PS384,
62            -37 => Self::PS256,
63            -36 => Self::ES512,
64            -35 => Self::ES384,
65            -8 => Self::EdDSA,
66            -7 => Self::ES256,
67            v => Self::__Unknown(v),
68        }
69    }
70}
71
72impl serde::Serialize for Algorithm {
73    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
74        serializer.serialize_i64(self.as_i64())
75    }
76}
77
78impl<'de> serde::Deserialize<'de> for Algorithm {
79    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
80        struct AlgorithmVisitor;
81
82        impl Visitor<'_> for AlgorithmVisitor {
83            type Value = Algorithm;
84
85            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
86                f.write_str("an integer")
87            }
88
89            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<Self::Value, E> {
90                Ok(Algorithm::from_i64(v))
91            }
92
93            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<Self::Value, E> {
94                Ok(Algorithm::from_i64(v as i64))
95            }
96        }
97
98        deserializer.deserialize_i64(AlgorithmVisitor)
99    }
100}
101
102impl std::fmt::Display for Algorithm {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        write!(f, "{}", self.as_i64())
105    }
106}
107impl Default for Algorithm {
108    fn default() -> Algorithm {
109        Self::RS1
110    }
111}