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.
65 lines
1.1 KiB
65 lines
1.1 KiB
4 years ago
|
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
|
||
|
}
|
||
|
}
|
||
|
|