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/identity.rs

88 lines
1.7 KiB

//! Handles post and user identity.
use super::*;
use std::{
fmt,
};
use uuid::Uuid;
/// A globally unique post identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct PostID(Uuid);
impl bytes::IntoBytes for PostID
{
fn into_bytes(self) -> Box<[u8]>
{
Box::from(*self.0.as_bytes())
}
}
impl bytes::FromBytes for PostID
{
type Error = bytes::SizeError;
fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self,Self::Error> {
let bytes = bytes.as_ref();
if bytes.len() < 16 {
Err(bytes::SizeError)
} else {
let by = [0u8; 16];
assert_eq!(bytes::copy_slice(&mut by[..], bytes), 16);
Ok(Self(Uuid::from_bytes(by)))
}
}
}
impl PostID
{
/// Generate a new `PostID`.
pub fn new() -> Self
{
Self(Uuid::new_v4())
}
}
impl fmt::Display for PostID
{
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
self.0.fmt(f) //TODO: Change to use kana-hash.
}
}
/// A user's data, if set.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct User
{
pub name: Option<String>,
pub email: Option<String>,
pub tripcode: Option<tripcode::Tripcode>,
}
impl User
{
/// The name string of this instance, or the default name.
pub fn name_str(&self) -> &str
{
self.name.as_ref().map(|x| &x[..]).unwrap_or(&config::get().anon_name[..])
}
/// The email string of this instance, if it is set
pub fn email_str(&self) -> Option<&str>
{
self.email.as_ref().map(|x| &x[..])
}
// No str for tripcode because it's not just a string (yet)
/// Anon with no detials.
pub const fn anon() -> Self
{
Self {
name: None,
email: None,
tripcode: None,
}
}
}