summaryrefslogtreecommitdiff
path: root/src/serde.rs
diff options
context:
space:
mode:
authorpennae <github@quasiparticle.net>2022-07-17 11:43:52 +0200
committerpennae <github@quasiparticle.net>2022-07-17 17:25:27 +0200
commit55343df9c1f54113b5f2ed04cecfadf0670887c7 (patch)
tree506c7b548ec6c0536a012ef7e6fe6083b7bcf4bc /src/serde.rs
parentb0935f56b430223403629baa535d8b1f45ad9c6d (diff)
downloadminor-skulk-55343df9c1f54113b5f2ed04cecfadf0670887c7.tar.gz
minor-skulk-55343df9c1f54113b5f2ed04cecfadf0670887c7.tar.xz
minor-skulk-55343df9c1f54113b5f2ed04cecfadf0670887c7.zip
add dedicated types for all the tokens
using SecretBytes for all of them isn't that great.
Diffstat (limited to 'src/serde.rs')
-rw-r--r--src/serde.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/serde.rs b/src/serde.rs
new file mode 100644
index 0000000..76676d7
--- /dev/null
+++ b/src/serde.rs
@@ -0,0 +1,21 @@
+pub mod as_hex {
+ use serde::{de, Deserialize, Deserializer, Serializer};
+
+ pub fn serialize<const N: usize, S: Serializer>(
+ b: &[u8; N],
+ ser: S,
+ ) -> Result<S::Ok, S::Error> {
+ ser.serialize_str(&hex::encode(b))
+ }
+
+ pub fn deserialize<'de, const N: usize, D: Deserializer<'de>>(
+ des: D,
+ ) -> Result<[u8; N], D::Error> {
+ let raw = <String as Deserialize>::deserialize(des)?;
+ let mut result = [0; N];
+ hex::decode_to_slice(&raw, &mut result).map_err(|_| {
+ de::Error::invalid_value(de::Unexpected::Other("non-hex string"), &"a hex string")
+ })?;
+ Ok(result)
+ }
+}