You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yuurei/src/tripcode.rs

71 lines
1.3 KiB

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<Self, khash::error::Error>
{
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<Tripcode> 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<str> for Tripcode
{
fn as_ref(&self) -> &str
{
&self.0[..]
}
}
impl std::borrow::Borrow<String> for Tripcode
{
fn borrow(&self) -> &String
{
&self.0
}
}