add ability to specify handlebars template as file (not very helpful for development, since it is still preloaded and cannot be dynamically loaded yet)

This commit is contained in:
Adrian Malacoda 2017-12-29 01:03:22 -06:00
parent 05c3ff94db
commit aae5f26973

View File

@ -74,8 +74,7 @@ impl ResourcesGenerator {
let mut handlebars = String::new();
for resource in &self.resources {
let canonical_source = canonicalize(&resource.source).expect(&format!("failed to get canonical path for {:?}", resource.source));
let include = self.create_include(&resource, &canonical_source);
let include = self.create_include(&resource);
if resource.constant.is_some() {
consts.push_str(&self.create_const(&resource, include.as_ref().expect("Expected include for constant")));
@ -83,8 +82,8 @@ impl ResourcesGenerator {
if resource.mount_at.is_some() {
let (function_name, route_function) = match include {
Some(ref include) => self.create_rocket_route(resource, &include),
None => self.create_rocket_route_link(resource, &canonical_source)
Some(ref include) => self.create_rocket_route_include(resource, &include),
None => self.create_rocket_route_link(resource)
};
routes.push_str(&route_function);
routes_fn.push_str(&function_name);
@ -92,7 +91,10 @@ impl ResourcesGenerator {
}
if resource.template_name.is_some() {
handlebars.push_str(&self.create_handlebars_template(resource, include.as_ref().expect("Expected include for handlebars template")));
handlebars.push_str(&(match include {
Some(ref include) => self.create_handlebars_template_include(resource, &include),
None => self.create_handlebars_template_link(resource)
}));
}
}
@ -108,14 +110,15 @@ impl ResourcesGenerator {
outfile.write(b"\n");
}
fn create_include(&self, resource: &Resource, source: &PathBuf) -> Option<String> {
fn create_include(&self, resource: &Resource) -> Option<String> {
if resource.link {
None
} else {
let canonical_source = canonicalize(&resource.source).expect(&format!("failed to get canonical path for {:?}", resource.source));
Some(format!(
"include_{}!(\"{}\")",
if resource.raw { "bytes" } else { "str" },
source.display()
canonical_source.display()
))
}
}
@ -129,7 +132,7 @@ impl ResourcesGenerator {
)
}
fn create_rocket_route(&self, resource: &Resource, include: &str) -> (String, String) {
fn create_rocket_route_include(&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";
@ -151,7 +154,7 @@ impl ResourcesGenerator {
))
}
fn create_rocket_route_link(&self, resource: &Resource, source: &PathBuf) -> (String, String) {
fn create_rocket_route_link(&self, resource: &Resource) -> (String, String) {
let mount_at = resource.mount_at.as_ref().unwrap();
let mut function_name = String::from("route");
function_name.push_str(&mount_at.endpoint.replace("/", "_").replace(".", "_").replace("-", "_"));
@ -159,7 +162,7 @@ impl ResourcesGenerator {
"#[get(\"{}\")]\nfn {}() -> io::Result<NamedFile> {{\n\tNamedFile::open(\"{}\")\n}}\n\n",
mount_at.endpoint,
function_name,
source.display()
resource.source.display()
))
}
@ -180,7 +183,7 @@ impl ResourcesGenerator {
outfile.write(b"]\n}");
}
fn create_handlebars_template(&self, resource: &Resource, include: &str) -> String {
fn create_handlebars_template_include(&self, resource: &Resource, include: &str) -> String {
let template_name = resource.template_name.unwrap();
format!(
"\n\thbs.register_template_string(\"{}\", {}).expect(\"Failed to register template: {}\");",
@ -190,6 +193,16 @@ impl ResourcesGenerator {
)
}
fn create_handlebars_template_link(&self, resource: &Resource) -> String {
let template_name = resource.template_name.unwrap();
format!(
"\n\thbs.register_template_file(\"{}\", {}).expect(\"Failed to register template: {}\");",
template_name,
resource.source.display(),
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");