add flag to specify that a resource is a "link" to a file rather than embedding a file
This commit is contained in:
parent
7c0aaf7efc
commit
05c3ff94db
60
src/lib.rs
60
src/lib.rs
@ -75,28 +75,24 @@ 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 data_type = if resource.raw {
|
||||
"[u8]"
|
||||
} else {
|
||||
"str"
|
||||
};
|
||||
let include = self.create_include(&resource, &canonical_source);
|
||||
|
||||
let include = format!("include_{}!(\"{}\")", if resource.raw { "bytes" } else { "str" }, canonical_source.display());
|
||||
if let Some(constant) = resource.constant {
|
||||
consts.push_str(&format!(
|
||||
"const {}: &'static {} = {};\n", constant, data_type, include
|
||||
));
|
||||
if resource.constant.is_some() {
|
||||
consts.push_str(&self.create_const(&resource, include.as_ref().expect("Expected include for constant")));
|
||||
}
|
||||
|
||||
if resource.mount_at.is_some() {
|
||||
let (function_name, route_function) = self.create_rocket_route(resource, &include);
|
||||
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)
|
||||
};
|
||||
routes.push_str(&route_function);
|
||||
routes_fn.push_str(&function_name);
|
||||
routes_fn.push(',');
|
||||
}
|
||||
|
||||
if resource.template_name.is_some() {
|
||||
handlebars.push_str(&self.create_handlebars_template(resource, &include));
|
||||
handlebars.push_str(&self.create_handlebars_template(resource, include.as_ref().expect("Expected include for handlebars template")));
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,6 +108,27 @@ impl ResourcesGenerator {
|
||||
outfile.write(b"\n");
|
||||
}
|
||||
|
||||
fn create_include(&self, resource: &Resource, source: &PathBuf) -> Option<String> {
|
||||
if resource.link {
|
||||
None
|
||||
} else {
|
||||
Some(format!(
|
||||
"include_{}!(\"{}\")",
|
||||
if resource.raw { "bytes" } else { "str" },
|
||||
source.display()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn create_const(&self, resource: &Resource, include: &str) -> String {
|
||||
format!(
|
||||
"const {}: &'static {} = {};\n",
|
||||
resource.constant.unwrap(),
|
||||
if resource.raw { "[u8]" } else { "str" },
|
||||
include
|
||||
)
|
||||
}
|
||||
|
||||
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");
|
||||
@ -134,9 +151,21 @@ impl ResourcesGenerator {
|
||||
))
|
||||
}
|
||||
|
||||
fn create_rocket_route_link(&self, resource: &Resource, source: &PathBuf) -> (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("-", "_"));
|
||||
(function_name.clone(), format!(
|
||||
"#[get(\"{}\")]\nfn {}() -> io::Result<NamedFile> {{\n\tNamedFile::open(\"{}\")\n}}\n\n",
|
||||
mount_at.endpoint,
|
||||
function_name,
|
||||
source.display()
|
||||
))
|
||||
}
|
||||
|
||||
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"use rocket::{Route, Response, Request};\nuse rocket::response::{Content, Responder, NamedFile};\nuse rocket::http::{ContentType, Status};\n\n");
|
||||
outfile.write(b"use std::io;\nuse 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> {
|
||||
@ -176,7 +205,8 @@ pub struct Resource {
|
||||
pub mount_at: Option<ResourceMount>,
|
||||
pub constant: Option<&'static str>,
|
||||
pub template_name: Option<&'static str>,
|
||||
pub raw: bool
|
||||
pub raw: bool,
|
||||
pub link: bool
|
||||
}
|
||||
|
||||
pub struct ResourceMount {
|
||||
|
Loading…
x
Reference in New Issue
Block a user