use crate::base32;
#[cfg(feature = "key")]
mod blob;
#[cfg(feature = "key")]
mod node;
#[cfg(feature = "key")]
pub use self::{blob::BlobTicket, node::NodeTicket};
pub trait Ticket: Sized {
const KIND: &'static str;
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(bytes: &[u8]) -> Result<Self, Error>;
fn serialize(&self) -> String {
let mut out = Self::KIND.to_string();
base32::fmt_append(self.to_bytes(), &mut out);
out
}
fn deserialize(str: &str) -> Result<Self, Error> {
let expected = Self::KIND;
let Some(rest) = str.strip_prefix(expected) else {
return Err(Error::Kind { expected });
};
let bytes = base32::parse_vec(rest)?;
let ticket = Self::from_bytes(&bytes)?;
Ok(ticket)
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("wrong prefix, expected {expected}")]
Kind { expected: &'static str },
#[error("deserialization failed: {_0}")]
Postcard(#[from] postcard::Error),
#[error("decoding failed: {_0}")]
Encoding(#[from] base32::DecodeError),
#[error("verification failed: {_0}")]
Verify(&'static str),
}