use super::*; use khash::ctx::Context; //use khash::salt::Salt; use std::fmt; /// A newtype tripcode string #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct Tripcode(String); lazy_static!{ static ref CONTEXT: Context = Context::new(defaults::TRIPCODE_ALGO, defaults::TRIPCODE_SALT); } impl Tripcode { /// Generate a tripcode from this string. pub fn generate(from: impl AsRef<[u8]>) -> Result { khash::generate(&CONTEXT, from).map(Self) } /// Create a tripcode that *is* this string #[inline] pub fn special(string: String) -> Self { Self(string) } /// As a string #[inline] pub fn as_str(&self) -> &str { &self.0 } /// Consume into regular string #[inline] pub fn into_inner(self) -> String { self.0 } } impl From for String { #[inline] fn from(from: Tripcode) -> Self { from.0 } } impl fmt::Display for Tripcode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } impl AsRef for Tripcode { fn as_ref(&self) -> &str { &self.0[..] } } impl std::borrow::Borrow for Tripcode { fn borrow(&self) -> &String { &self.0 } }