From e77c1a57e42951511355bdb006bd071b83b611c5 Mon Sep 17 00:00:00 2001 From: Gunwant Jain Date: Thu, 8 Jul 2021 18:48:19 +0530 Subject: [PATCH] cleaned up code, using &str instead of RawStr Signed-off-by: Gunwant Jain --- src/paste_id.rs | 5 ++--- src/pretty.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/pretty.rs diff --git a/src/paste_id.rs b/src/paste_id.rs index a0fab41..9ed06fa 100644 --- a/src/paste_id.rs +++ b/src/paste_id.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; use std::fmt; -use rocket::http::RawStr; use rocket::request::FromParam; use rand::{self, distributions::Alphanumeric, Rng}; @@ -25,9 +24,9 @@ impl<'a> PasteId<'a> { } impl<'a> FromParam<'a> for PasteId<'a> { - type Error = &'a RawStr; + type Error = &'a str; - fn from_param(param: &'a RawStr) -> Result { + fn from_param(param: &'a str) -> Result { match valid_id(param) { true => Ok(PasteId(Cow::Borrowed(param))), false => Err(param), diff --git a/src/pretty.rs b/src/pretty.rs new file mode 100644 index 0000000..4fe2235 --- /dev/null +++ b/src/pretty.rs @@ -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 +}