initialise
routes:
GET /<id>
POST /
Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
commit
805e4ce7d2
6 changed files with 951 additions and 0 deletions
42
src/paste_id.rs
Normal file
42
src/paste_id.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
|
||||
use rocket::http::RawStr;
|
||||
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 RawStr;
|
||||
|
||||
fn from_param(param: &'a RawStr) -> 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue