123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- use diesel::result::Error;
- use rocket::http::Status;
- use rocket::response::status;
- use rocket_contrib::json::Json;
- use rocket_contrib::templates::Template;
- use std::collections::HashMap;
- use syntect::highlighting::ThemeSet;
- use syntect::html::highlighted_html_for_string;
- use syntect::parsing::SyntaxSet;
-
- use crate::connection::DbConn;
- use crate::gists;
- use crate::gists::{Gist, InsertableGist};
-
- fn error_response(error: Error) -> Status {
- match error {
- Error::NotFound => Status::NotFound,
- _ => Status::InternalServerError,
- }
- }
-
- fn format_gist(filetype: Option<String>, body: String) -> String {
- filetype.map_or(
- format!("<pre class='plaintext'>{}</pre>", body),
- |filetype| {
- let ps = SyntaxSet::load_defaults_newlines();
- let ts = ThemeSet::load_defaults();
- ps.find_syntax_by_extension(&filetype).map_or(
- format!("<pre class='plaintext'>{}</pre>", body),
- |syntax| {
- highlighted_html_for_string(&body, &ps, syntax, &ts.themes["Solarized (light)"])
- },
- )
- },
- )
- }
-
- #[get("/")]
- pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
- gists::all(&connection)
- .map(|gists| Json(gists))
- .map_err(|error| error_response(error))
- }
-
- #[get("/gists/<id>")]
- pub fn show_gist(id: i32, connection: DbConn) -> Template {
- let result = match gists::get(&connection, id) {
- Ok(gist) => Some(gist),
- Err(_) => None,
- };
-
- let (title, body) = result.map_or(
- (
- String::from("404 - Gist not found"),
- format!("<h3'>{}</h3>", "Gist not found"),
- ),
- |gist| (gist.title, format_gist(gist.filetype, gist.body)),
- );
-
- let mut context: HashMap<&str, String> = HashMap::new();
- context.insert("title", title);
- context.insert("body", body);
-
- Template::render("gists/show", &context)
- }
-
- #[post("/api/gists", format = "application/json", data = "<gist>")]
- pub fn create_gist<'a>(
- gist: Json<InsertableGist>,
- connection: DbConn,
- ) -> Result<status::Created<Json<Gist>>, Status> {
- gists::insert(gist.into_inner(), &connection)
- .map(|gist| status::Created(String::from(""), Some(Json(gist))))
- .map_err(|error| error_response(error))
- }
|