refactor into multiple functions

This commit is contained in:
Adrian Malacoda 2017-12-28 23:43:47 -06:00
parent 11bdb6c878
commit 7c0aaf7efc

View File

@ -9,7 +9,9 @@ use std::fs::{File, create_dir_all};
use std::io::{Write, copy};
pub fn yarn_install () {
Command::new("yarn").output().expect("failed to execute yarn process");
if !Command::new("yarn").status().expect("failed to execute yarn process").success() {
panic!("Failed to execute yarn process");
}
}
pub fn browserify (infile: &str, outfile: &str) -> PathBuf {
@ -86,72 +88,85 @@ impl ResourcesGenerator {
));
}
if let Some(ref mount_at) = resource.mount_at {
let mut function_name = String::from("route");
let mut function_return_type = "&'static str";
let mut function_return_value = String::from(resource.constant.unwrap_or(&include));
if resource.raw {
function_return_type = "RawByteResource";
function_return_value = format!("RawByteResource({})", function_return_value);
}
function_name.push_str(&mount_at.endpoint.replace("/", "_").replace(".", "_").replace("-", "_"));
routes.push_str(&format!(
"#[get(\"{}\")]\nfn {}() -> Content<{}> {{\n\tContent(ContentType::{}, {})\n}}\n\n",
mount_at.endpoint,
function_name,
function_return_type,
mount_at.content_type,
function_return_value
));
if resource.mount_at.is_some() {
let (function_name, route_function) = self.create_rocket_route(resource, &include);
routes.push_str(&route_function);
routes_fn.push_str(&function_name);
routes_fn.push(',');
}
if let Some(ref handlebars_name) = resource.template_name {
handlebars.push_str(&format!(
"\n\thbs.register_template_string(\"{}\", {}).expect(\"Failed to register template: {}\");",
handlebars_name,
resource.constant.unwrap_or(&include),
handlebars_name
));
if resource.template_name.is_some() {
handlebars.push_str(&self.create_handlebars_template(resource, &include));
}
}
if !routes.is_empty() {
outfile.write(b"use rocket::{Route, Response, Request};\nuse rocket::response::{Content, Responder};\nuse rocket::http::{ContentType, Status};\n\n");
outfile.write(b"use std::io::Cursor;\n\n");
outfile.write(b"struct RawByteResource(&'static [u8]);\n");
outfile.write(b"impl<'r> Responder<'r> for RawByteResource {
fn respond_to(self, _: &Request) -> Result<Response<'r>, Status> {
Response::build()
.header(ContentType::Binary)
.sized_body(Cursor::new(self.0))
.ok()
}
}");
self.write_rocket_routes(&mut outfile, &routes, &routes_fn);
}
if !handlebars.is_empty () {
outfile.write(b"use handlebars::Handlebars;\n\n");
self.write_handlebars_templates(&mut outfile, &handlebars);
}
outfile.write(consts.as_bytes());
outfile.write(b"\n");
}
if !routes.is_empty() {
outfile.write(routes.as_bytes());
outfile.write(routes_fn.as_bytes());
outfile.write(b"]\n}");
fn create_rocket_route(&self, resource: &Resource, include: &str) -> (String, String) {
let mount_at = resource.mount_at.as_ref().unwrap();
let mut function_name = String::from("route");
let mut function_return_type = "&'static str";
let mut function_return_value = String::from(resource.constant.unwrap_or(&include));
if resource.raw {
function_return_type = "RawByteResource";
function_return_value = format!("RawByteResource({})", function_return_value);
}
if !handlebars.is_empty() {
outfile.write(b"pub fn handlebars() -> Handlebars {\n");
outfile.write(b"\tlet mut hbs = Handlebars::new();");
outfile.write(handlebars.as_bytes());
outfile.write(b"hbs\n}");
}
function_name.push_str(&mount_at.endpoint.replace("/", "_").replace(".", "_").replace("-", "_"));
(function_name.clone(), format!(
"#[get(\"{}\")]\nfn {}() -> Content<{}> {{\n\tContent(ContentType::{}, {})\n}}\n\n",
mount_at.endpoint,
function_name,
function_return_type,
mount_at.content_type,
function_return_value
))
}
fn write_rocket_routes(&self, outfile: &mut File, routes: &str, routes_fn: &str) {
outfile.write(b"use rocket::{Route, Response, Request};\nuse rocket::response::{Content, Responder};\nuse rocket::http::{ContentType, Status};\n\n");
outfile.write(b"use std::io::Cursor;\n\n");
outfile.write(b"struct RawByteResource(&'static [u8]);\n");
outfile.write(b"impl<'r> Responder<'r> for RawByteResource {
fn respond_to(self, _: &Request) -> Result<Response<'r>, Status> {
Response::build()
.header(ContentType::Binary)
.sized_body(Cursor::new(self.0))
.ok()
}
}");
outfile.write(routes.as_bytes());
outfile.write(routes_fn.as_bytes());
outfile.write(b"]\n}");
}
fn create_handlebars_template(&self, resource: &Resource, include: &str) -> String {
let template_name = resource.template_name.unwrap();
format!(
"\n\thbs.register_template_string(\"{}\", {}).expect(\"Failed to register template: {}\");",
template_name,
resource.constant.unwrap_or(&include),
template_name
)
}
fn write_handlebars_templates(&self, outfile: &mut File, handlebars: &str) {
outfile.write(b"use handlebars::Handlebars;\n\n");
outfile.write(b"pub fn handlebars() -> Handlebars {\n");
outfile.write(b"\tlet mut hbs = Handlebars::new();");
outfile.write(handlebars.as_bytes());
outfile.write(b"hbs\n}");
}
}