Struct ructe::StaticFiles
source · pub struct StaticFiles { /* private fields */ }
Expand description
Handler for static files.
To just add all files in a single directory, there is a shorthand method
compile_static_files
.
For more complex setups (static files in more than one directory,
generated static files, etc), use this struct.
Each static file will be available as a
StaticFile
struct instance in
your templates::statics
module.
Also, the const STATICS
array in the same module will contain a
reference to each of those instances.
Implementations
sourceimpl StaticFiles
impl StaticFiles
sourcepub fn new(outdir: &Path) -> Result<Self>
pub fn new(outdir: &Path) -> Result<Self>
Create a new set of static files.
There should only be one StaticFiles
for a set of compiled templates.
The outdir
should be the same as in the call to compile_templates
.
sourcepub fn add_files(&mut self, indir: &Path) -> Result<()>
pub fn add_files(&mut self, indir: &Path) -> Result<()>
Add all files from a specific directory, indir
, as static files.
sourcepub fn add_files_as(&mut self, indir: &Path, to: &str) -> Result<()>
pub fn add_files_as(&mut self, indir: &Path, to: &str) -> Result<()>
Add all files from a specific directory, indir
, as static files.
The to
string is used as a directory path of the resulting
urls, the file names are taken as is, without adding any hash.
This is usefull for resources used by preexisting javascript
packages, where it might be hard to change the used urls.
sourcepub fn add_file(&mut self, path: &Path) -> Result<()>
pub fn add_file(&mut self, path: &Path) -> Result<()>
Add one specific file as a static file.
Create a name to use in the url like name-hash.ext
where
name and ext are the name and extension from path
and has is
a few url-friendly bytes from a hash of the file content.
sourcepub fn add_file_as(&mut self, path: &Path, to_name: &str) -> Result<()>
pub fn add_file_as(&mut self, path: &Path, to_name: &str) -> Result<()>
Add one specific file as a static file.
Use to_name
in the url without adding any hash characters.
sourcepub fn add_file_data(&mut self, path: &Path, data: &[u8]) -> Result<()>
pub fn add_file_data(&mut self, path: &Path, data: &[u8]) -> Result<()>
Add a file by its name and content.
The path
parameter is used only to create a file name, the actual
content of the static file will be the data
parameter.
sourcepub fn get_names(&self) -> &BTreeMap<String, String>
pub fn get_names(&self) -> &BTreeMap<String, String>
Get a mapping of names, from without hash to with.
let mut statics = StaticFiles::new(&p).unwrap();
statics.add_file_data("black.css".as_ref(), b"body{color:black}\n");
statics.add_file_data("blue.css".as_ref(), b"body{color:blue}\n");
assert_eq!(statics.get_names().iter()
.map(|(a, b)| format!("{} -> {}", a, b))
.collect::<Vec<_>>(),
vec!["black_css -> black-r3rltVhW.css".to_string(),
"blue_css -> blue-GZGxfXag.css".to_string()])