[][src]Struct ructe::StaticFiles

pub struct StaticFiles { /* fields omitted */ }

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.

Methods

impl StaticFiles[src]

pub fn new(outdir: &Path) -> Result<Self>[src]

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.

pub fn add_files(&mut self, indir: &Path) -> Result<()>[src]

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

pub fn add_files_as(&mut self, indir: &Path, to: &str) -> Result<()>[src]

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.

pub fn add_file(&mut self, path: &Path) -> Result<()>[src]

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.

pub fn add_file_as(&mut self, path: &Path, to_name: &str) -> Result<()>[src]

Add one specific file as a static file.

Use to_name in the url without adding any hash characters.

pub fn add_file_data(&mut self, path: &Path, data: &[u8]) -> Result<()>[src]

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.

pub fn get_names(&self) -> &BTreeMap<String, String>[src]

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

impl Drop for StaticFiles[src]

fn drop(&mut self)[src]

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

Auto Trait Implementations

impl Send for StaticFiles

impl Sync for StaticFiles

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.