From f8b7d9d7e936bb53db3f978b54592b7af791d1ec Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 14 Jan 2017 14:33:13 -0800 Subject: add repr to webface --- src/bin/mt-webface.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src') diff --git a/src/bin/mt-webface.rs b/src/bin/mt-webface.rs index 96bc456..5361a73 100644 --- a/src/bin/mt-webface.rs +++ b/src/bin/mt-webface.rs @@ -19,6 +19,8 @@ use pencil::{Request, PencilResult, Response, HTTPError, PencilError}; use pencil::{redirect, abort}; use pencil::method::{Get, Post}; use regex::Regex; +use modelthing::transpile_scheme::TranspileScheme; +use modelthing::transpile_js::TranspileJS; /* This command doesn't use error_chain (or raise errors in general) because the @@ -90,6 +92,29 @@ fn model_view(r: &mut Request) -> PencilResult { } } +fn model_repr(r: &mut Request) -> PencilResult { + let namespace = r.view_args.get("namespace").unwrap(); + let model_slug = r.view_args.get("model_slug").unwrap(); + let model_path = Path::new(namespace).join(model_slug); + let format = r.url + .query_pairs() + .filter(|x| x.0 == "format") + .map(|(_,v)| v.into_owned()) + .nth(0) + .unwrap_or("".to_string()); + let format: &str = &format; + match modelthing::load_model_entry(model_path.as_path()) { + Ok(me) => { + match format { + "scheme" => Ok(Response::from(me.ast.repr_scheme().unwrap())), + "javascript" => Ok(Response::from(me.ast.repr_js().unwrap())), + _ => abort(403), + } + }, + Err(_) => Err(PencilError::PenHTTPError(pencil::HTTPError::NotFound)), + } +} + // Sends the modelica file fn model_raw(r: &mut Request) -> PencilResult { let namespace = r.view_args.get("namespace").unwrap(); @@ -305,6 +330,7 @@ fn main() { app.register_template("model_view.html"); app.get("/m///", "model_view", model_view); app.get("/m///raw/", "model_raw", model_raw); + app.get("/m///repr/", "model_repr", model_repr); app.get("/m///markdown/", "model_markdown", model_markdown); app.register_template("model_create.html"); app.route("/create/", &[Get, Post], "model_create", model_create); -- cgit v1.2.3