bin/src/paste_id.rs
Gunwant Jain e77c1a57e4 cleaned up code, using &str instead of RawStr
Signed-off-by: Gunwant Jain <mail@wantguns.dev>
2021-07-08 18:48:19 +05:30

42 lines
926 B
Rust

use std::borrow::Cow;
use std::fmt;
use rocket::request::FromParam;
use rand::{self, distributions::Alphanumeric, Rng};
pub struct PasteId<'a>(Cow<'a, str>);
fn valid_id(id: &str) -> bool {
id.chars().all(char::is_alphanumeric)
}
impl<'a> PasteId<'a> {
pub fn new(size: usize) -> PasteId<'static> {
let id: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(size)
.map(char::from)
.collect();
PasteId(Cow::Owned(id))
}
}
impl<'a> FromParam<'a> for PasteId<'a> {
type Error = &'a str;
fn from_param(param: &'a str) -> Result<Self, Self::Error> {
match valid_id(param) {
true => Ok(PasteId(Cow::Borrowed(param))),
false => Err(param),
}
}
}
impl<'a> fmt::Display for PasteId<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}