refactor: bring in the modules
Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
parent
8a3b29a0ef
commit
9c5a3af128
12 changed files with 145 additions and 118 deletions
3
src/models/mod.rs
Normal file
3
src/models/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod paste_id;
|
||||
pub mod pretty_syntax;
|
||||
pub mod pretty;
|
||||
41
src/models/paste_id.rs
Normal file
41
src/models/paste_id.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
18
src/models/pretty.rs
Normal file
18
src/models/pretty.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use std::fs;
|
||||
|
||||
use syntect::highlighting::ThemeSet;
|
||||
use syntect::html::highlighted_html_for_string;
|
||||
use syntect::parsing::SyntaxSet;
|
||||
|
||||
pub fn get_pretty_body(path: &String, ext: &String) -> String {
|
||||
let ss = SyntaxSet::load_defaults_newlines();
|
||||
let theme = ThemeSet::get_theme("themes/ayu_dark.tmTheme").unwrap();
|
||||
|
||||
let content = fs::read_to_string(path).unwrap();
|
||||
let syntax = ss
|
||||
.find_syntax_by_token(ext)
|
||||
.unwrap_or_else(|| ss.find_syntax_plain_text());
|
||||
let html = highlighted_html_for_string(&content, &ss, syntax, &theme);
|
||||
|
||||
html
|
||||
}
|
||||
43
src/models/pretty_syntax.rs
Normal file
43
src/models/pretty_syntax.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use rocket::request::FromParam;
|
||||
|
||||
pub struct PasteIdSyntax<'a> {
|
||||
syn_id: Cow<'a, str>,
|
||||
}
|
||||
|
||||
fn valid_syn(syn: &str) -> bool {
|
||||
let mut flag = false;
|
||||
let split: Vec<&str> = syn.split(".").collect();
|
||||
if split.len() == 2 {
|
||||
for s in split {
|
||||
if s.chars().all(char::is_alphanumeric) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flag
|
||||
}
|
||||
|
||||
impl<'a> PasteIdSyntax<'a> {
|
||||
pub fn get_fname(&self) -> &str {
|
||||
&self.syn_id.split(".").collect::<Vec<&str>>()[0]
|
||||
}
|
||||
pub fn get_ext(&self) -> &str {
|
||||
&self.syn_id.split(".").collect::<Vec<&str>>()[1]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromParam<'a> for PasteIdSyntax<'a> {
|
||||
type Error = &'a str;
|
||||
|
||||
fn from_param(param: &'a str) -> Result<Self, Self::Error> {
|
||||
match valid_syn(param) {
|
||||
true => Ok(PasteIdSyntax {
|
||||
syn_id: Cow::Borrowed(param),
|
||||
}),
|
||||
false => Err(param),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue