Run cargo fmt
Diff
src/highlight.rs | 11 +++++++----
src/io.rs | 34 ++++++++++++++++++++++++++++++++--
src/main.rs | 32 +++++++++++++++++++++-----------
src/params.rs | 16 ++++++++++++----
4 files changed, 60 insertions(+), 33 deletions(-)
@@ -1,9 +1,9 @@
extern crate syntect;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::ThemeSet;
use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground};
use syntect::parsing::SyntaxSet;
@@ -19,5 +19,8 @@
let mut h = HighlightLines::new(syntax, &TS.themes["base16-ocean.dark"]);
let regions = h.highlight(content, &SS);
Some(styled_line_to_highlighted_html(®ions[..], IncludeBackground::No))
}
Some(styled_line_to_highlighted_html(
®ions[..],
IncludeBackground::No,
))
}
@@ -1,19 +1,23 @@
extern crate gpw;
extern crate linked_hash_map;
extern crate rand;
use rand::{Rng, thread_rng};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use linked_hash_map::LinkedHashMap;
use std::sync::RwLock;
use std::env;
use std::cell::RefCell;
use std::env;
use std::sync::RwLock;
lazy_static! {
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
static ref BUFFER_SIZE: usize = env::var("BIN_BUFFER_SIZE").map(|f| f.parse::<usize>().expect("Failed to parse value of BIN_BUFFER_SIZE")).unwrap_or(1000usize);
static ref BUFFER_SIZE: usize = env::var("BIN_BUFFER_SIZE")
.map(|f| f
.parse::<usize>()
.expect("Failed to parse value of BIN_BUFFER_SIZE"))
.unwrap_or(1000usize);
}
@@ -37,18 +41,32 @@
pub fn generate_id() -> String {
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
KEYGEN.with(|k| k.borrow_mut().next().unwrap_or_else(|| thread_rng().sample_iter(&Alphanumeric).take(6).collect::<String>()))
KEYGEN.with(|k| {
k.borrow_mut().next().unwrap_or_else(|| {
thread_rng()
.sample_iter(&Alphanumeric)
.take(6)
.collect::<String>()
})
})
}
pub fn store_paste(id: String, content: String) {
purge_old();
ENTRIES.write().unwrap_or_else(|p| p.into_inner()).insert(id, content);
ENTRIES
.write()
.unwrap_or_else(|p| p.into_inner())
.insert(id, content);
}
pub fn get_paste(id: &str) -> Option<String> {
ENTRIES.read().unwrap_or_else(|p| p.into_inner()).get(id).cloned()
}
ENTRIES
.read()
.unwrap_or_else(|p| p.into_inner())
.get(id)
.cloned()
}
@@ -1,29 +1,31 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(type_alias_enum_variants)]
#[macro_use] extern crate lazy_static;
#[macro_use]
extern crate lazy_static;
#[macro_use] extern crate rocket;
#[macro_use]
extern crate rocket;
extern crate askama;
extern crate askama_escape;
mod io;
mod highlight;
mod io;
mod params;
use io::{store_paste, get_paste, generate_id};
use highlight::highlight;
use params::{IsPlaintextRequest, HostHeader};
use io::{generate_id, get_paste, store_paste};
use params::{HostHeader, IsPlaintextRequest};
use askama::Template;
use askama_escape::{MarkupDisplay, Html};
use askama_escape::{Html, MarkupDisplay};
use rocket::Data;
use rocket::http::{ContentType, Status};
use rocket::request::Form;
use rocket::response::Redirect;
use rocket::response::content::Content;
use rocket::http::{ContentType, Status};
use rocket::response::Redirect;
use rocket::Data;
use std::io::Read;
@@ -40,14 +42,13 @@
Index {}
}
#[derive(FromForm)]
struct IndexForm {
val: String
val: String,
}
#[post("/", data = "<input>")]
@@ -67,11 +68,10 @@
match *host {
Some(host) => Ok(format!("https://{}/{}", host, id)),
None => Ok(id)
None => Ok(id),
}
}
@@ -100,16 +100,16 @@
None => MarkupDisplay::new_unsafe(entry, Html),
Some(extension) => highlight(&entry, extension)
.map(|h| MarkupDisplay::new_safe(h, Html))
.ok_or_else(|| Status::NotFound)?
.ok_or_else(|| Status::NotFound)?,
},
};
template.render()
template
.render()
.map(|html| Content(ContentType::HTML, html))
.map_err(|_| Status::InternalServerError)
}
}
fn main() {
rocket::ignite()
@@ -1,7 +1,7 @@
use std::ops::Deref;
use rocket::Request;
use rocket::request::{FromRequest, Outcome};
use rocket::Request;
@@ -27,9 +27,15 @@
}
}
match request.headers().get_one("User-Agent").and_then(|u| u.splitn(2, '/').next()) {
None | Some("Wget") | Some("curl") | Some("HTTPie") => Outcome::Success(IsPlaintextRequest(true)),
_ => Outcome::Success(IsPlaintextRequest(false))
match request
.headers()
.get_one("User-Agent")
.and_then(|u| u.splitn(2, '/').next())
{
None | Some("Wget") | Some("curl") | Some("HTTPie") => {
Outcome::Success(IsPlaintextRequest(true))
}
_ => Outcome::Success(IsPlaintextRequest(false)),
}
}
}
@@ -54,4 +60,4 @@
fn from_request(request: &'a Request<'r>) -> Outcome<HostHeader<'a>, ()> {
Outcome::Success(HostHeader(request.headers().get_one("Host")))
}
}
}