pub mod inode_base32_serde {
use serde::*;
use super::*;
pub struct InodeDe{}
impl<'de> serde::de::Visitor<'de> for InodeDe {
type Value = Inode;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a base32-encoded string")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
let mut b = [0; 8];
if data_encoding::BASE32_NOPAD.decode_mut(s.as_bytes(), &mut b).is_ok() {
let b: u64 = BigEndian::read_u64(&b);
Ok(Inode(b.into()))
} else {
Err(de::Error::invalid_value(serde::de::Unexpected::Str(s), &self))
}
}
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Inode, D::Error> {
d.deserialize_str(InodeDe{})
}
pub fn serialize<S: Serializer>(inode: &Inode, s: S) -> Result<S::Ok, S::Error> {
let inode: u64 = inode.0.into();
let mut b = [0; 8];
BigEndian::write_u64(&mut b, inode);
let mut bb = [0; 13];
data_encoding::BASE32_NOPAD.encode_mut(&b, &mut bb);
let b = std::str::from_utf8(&bb).unwrap();
s.serialize_str(b)
}
}