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

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.

Add all files from a specific directory, indir, as static files.

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.

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.

Add one specific file as a static file.

Use to_name in the url without adding any hash characters.

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.

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()])

Trait Implementations

Write the ending of the statics source code, declaring the STATICS variable.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.