parent
7b71b4fdcb
commit
3a2e30486a
@ -0,0 +1,64 @@
|
||||
use super::*;
|
||||
use khash::ctx::{self, Context};
|
||||
use khash::salt::Salt;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Tripcode(String);
|
||||
|
||||
lazy_static!{
|
||||
static ref CONTEXT: Context = Context::new(ctx::Algorithm::Sha256Truncated, Salt::internal());
|
||||
}
|
||||
|
||||
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 from_string(string: String) -> Self
|
||||
{
|
||||
Self(string)
|
||||
}
|
||||
/// As a string
|
||||
#[inline] pub fn as_str(&self) -> &str
|
||||
{
|
||||
&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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue