challenge_bypass_ristretto/
macros.rs

1#[cfg(any(test, feature = "base64"))]
2#[macro_export]
3/// Implement the encode_base64 / decode_base64 functions for a struct which implements to_bytes / from_bytes
4macro_rules! impl_base64 {
5    ($t:ident) => {
6        impl $t {
7            #[cfg(all(feature = "alloc", not(feature = "std")))]
8            /// Encode to a base64 string
9            pub fn encode_base64(&self) -> ::alloc::string::String {
10                use base64::Engine;
11                base64::prelude::BASE64_STANDARD.encode(&self.to_bytes().to_vec())
12            }
13
14            #[cfg(all(feature = "std"))]
15            /// Encode to a base64 string
16            pub fn encode_base64(&self) -> ::std::string::String {
17                use base64::Engine;
18                base64::prelude::BASE64_STANDARD.encode(&self.to_bytes().to_vec())
19            }
20
21            /// Decode from a base64 string
22            pub fn decode_base64(s: &str) -> Result<Self, TokenError> {
23                use base64::Engine;
24                let bytes = base64::prelude::BASE64_STANDARD
25                    .decode(s)
26                    .or(Err(TokenError(InternalError::DecodingError)))?;
27                $t::from_bytes(&bytes)
28            }
29        }
30    };
31}
32
33#[cfg(all(feature = "serde", not(feature = "serde_base64")))]
34#[macro_export]
35/// Implement the Serialize / Deserialize traits for a struct which implements to_bytes / from_bytes
36macro_rules! impl_serde {
37    ($t:ident) => {
38        impl ::serde::Serialize for $t {
39            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
40            where
41                S: ::serde::Serializer,
42            {
43                serializer.serialize_bytes(&self.to_bytes())
44            }
45        }
46
47        impl<'d> ::serde::Deserialize<'d> for $t {
48            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
49            where
50                D: ::serde::Deserializer<'d>,
51            {
52                use core::fmt::Debug;
53
54                struct TVisitor;
55
56                impl<'d> ::serde::de::Visitor<'d> for TVisitor {
57                    type Value = $t;
58
59                    fn expecting(
60                        &self,
61                        formatter: &mut ::core::fmt::Formatter,
62                    ) -> ::core::fmt::Result {
63                        $t::bytes_length_error().fmt(formatter)
64                    }
65
66                    fn visit_bytes<E>(self, bytes: &[u8]) -> Result<$t, E>
67                    where
68                        E: ::serde::de::Error,
69                    {
70                        $t::from_bytes(bytes)
71                            .or(Err(::serde::de::Error::invalid_length(bytes.len(), &self)))
72                    }
73                }
74                deserializer.deserialize_bytes(TVisitor)
75            }
76        }
77    };
78}
79
80#[cfg(feature = "serde_base64")]
81#[macro_export]
82/// Implement the Serialize / Deserialize traits for a struct which implements to_bytes / from_bytes
83macro_rules! impl_serde {
84    ($t:ident) => {
85        impl ::serde::Serialize for $t {
86            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
87            where
88                S: ::serde::Serializer,
89            {
90                serializer.serialize_str(&self.encode_base64())
91            }
92        }
93
94        impl<'d> ::serde::Deserialize<'d> for $t {
95            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
96            where
97                D: ::serde::Deserializer<'d>,
98            {
99                use core::fmt::Debug;
100
101                struct TVisitor;
102
103                impl<'d> ::serde::de::Visitor<'d> for TVisitor {
104                    type Value = $t;
105
106                    fn expecting(
107                        &self,
108                        formatter: &mut ::core::fmt::Formatter,
109                    ) -> ::core::fmt::Result {
110                        write!(formatter, "a base64 encoded string: ")?;
111                        $t::bytes_length_error().fmt(formatter)
112                    }
113
114                    fn visit_str<E>(self, s: &str) -> Result<$t, E>
115                    where
116                        E: ::serde::de::Error,
117                    {
118                        $t::decode_base64(s).map_err(::serde::de::Error::custom)
119                    }
120                }
121                deserializer.deserialize_str(TVisitor)
122            }
123        }
124    };
125}