Skip to content

Commit

Permalink
Support chaining in StaticFiles.
Browse files Browse the repository at this point in the history
Make most methods of `StaticFiles` return `Result<&mut Self>`, to
support method chaining, making typical build scripts nicer.
  • Loading branch information
kaj committed Aug 26, 2022
1 parent 42e556f commit 460133b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 38 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,13 @@ The format is based on
project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

* Breaking change: Most methods of `StaticFiles` now supports method
chaining, by returning `Result<&mut Self>`, making typical build scripts
nicer (PR #115).


## Release 0.14.2 - 2022-08-20

* Improve error reporting. The debug output for `RucteError` is now the
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "ructe"
version = "0.14.2"
version = "0.15.0-PRE"
authors = ["Rasmus Kaj <kaj@kth.se>"]
description = "Rust Compiled Templates, efficient type-safe web page templates."
documentation = "https://docs.rs/ructe"
Expand Down
7 changes: 4 additions & 3 deletions examples/actix/src/build.rs
Expand Up @@ -4,8 +4,9 @@ use ructe::{Ructe, RucteError};

fn main() -> Result<(), RucteError> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("statics")?;
statics.add_sass_file("style.scss")?;
ructe
.statics()?
.add_files("statics")?
.add_sass_file("style.scss")?;
ructe.compile_templates("templates")
}
7 changes: 4 additions & 3 deletions examples/gotham/src/build.rs
Expand Up @@ -4,8 +4,9 @@ use ructe::{Result, Ructe};

fn main() -> Result<()> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("statics")?;
statics.add_sass_file("style.scss")?;
ructe
.statics()?
.add_files("statics")?
.add_sass_file("style.scss")?;
ructe.compile_templates("templates")
}
7 changes: 4 additions & 3 deletions examples/iron/src/build.rs
Expand Up @@ -5,8 +5,9 @@ use ructe::{Result, Ructe};

fn main() -> Result<()> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("statics")?;
statics.add_sass_file("style.scss")?;
ructe
.statics()?
.add_files("statics")?
.add_sass_file("style.scss")?;
ructe.compile_templates("templates")
}
7 changes: 4 additions & 3 deletions examples/nickel/src/build.rs
Expand Up @@ -6,8 +6,9 @@ use ructe::{Result, Ructe};

fn main() -> Result<()> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("statics")?;
statics.add_sass_file("style.scss")?;
ructe
.statics()?
.add_files("statics")?
.add_sass_file("style.scss")?;
ructe.compile_templates("templates")
}
8 changes: 4 additions & 4 deletions examples/static-sass/src/build.rs
Expand Up @@ -2,9 +2,9 @@ use ructe::{Ructe, RucteError};

fn main() -> Result<(), RucteError> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("static")?;
statics.add_sass_file("scss/style.scss")?;

ructe
.statics()?
.add_files("static")?
.add_sass_file("scss/style.scss")?;
ructe.compile_templates("templates")
}
7 changes: 4 additions & 3 deletions examples/tide/src/build.rs
Expand Up @@ -5,8 +5,9 @@ use ructe::{Ructe, RucteError};

fn main() -> Result<(), RucteError> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("statics")?;
statics.add_sass_file("style.scss")?;
ructe
.statics()?
.add_files("statics")?
.add_sass_file("style.scss")?;
ructe.compile_templates("templates")
}
7 changes: 4 additions & 3 deletions examples/warp03/src/build.rs
Expand Up @@ -4,8 +4,9 @@ use ructe::{Ructe, RucteError};

fn main() -> Result<(), RucteError> {
let mut ructe = Ructe::from_env()?;
let mut statics = ructe.statics()?;
statics.add_files("statics")?;
statics.add_sass_file("style.scss")?;
ructe
.statics()?
.add_files("statics")?
.add_sass_file("style.scss")?;
ructe.compile_templates("templates")
}
3 changes: 2 additions & 1 deletion src/lib.rs
Expand Up @@ -299,7 +299,8 @@ impl Ructe {
/// # use ructe::{Ructe, RucteError};
/// # fn main() -> Result<(), RucteError> {
/// let mut ructe = Ructe::from_env()?;
/// ructe.statics()?.add_files("static")
/// ructe.statics()?.add_files("static")?;
/// Ok(())
/// # }
/// ```
///
Expand Down
35 changes: 21 additions & 14 deletions src/staticfiles.rs
Expand Up @@ -4,7 +4,7 @@ use std::ascii::escape_default;
use std::collections::BTreeMap;
use std::fmt::{self, Display};
use std::fs::{read_dir, File};
use std::io::{self, Read, Write};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

/// Handler for static files.
Expand Down Expand Up @@ -285,7 +285,10 @@ impl StaticFile {
}

/// Add all files from a specific directory, `indir`, as static files.
pub fn add_files(&mut self, indir: impl AsRef<Path>) -> Result<()> {
pub fn add_files(
&mut self,
indir: impl AsRef<Path>,
) -> Result<&mut Self> {
let indir = self.path_for(indir);
println!("cargo:rerun-if-changed={}", indir.display());
for entry in read_dir(indir)? {
Expand All @@ -294,7 +297,7 @@ impl StaticFile {
self.add_file(&entry.path())?;
}
}
Ok(())
Ok(self)
}

/// Add all files from a specific directory, `indir`, as static files.
Expand All @@ -318,7 +321,7 @@ impl StaticFile {
&mut self,
indir: impl AsRef<Path>,
to: &str,
) -> Result<()> {
) -> Result<&mut Self> {
for entry in read_dir(self.path_for(indir))? {
let entry = entry?;
let file_type = entry.file_type()?;
Expand All @@ -333,7 +336,7 @@ impl StaticFile {
self.add_files_as(&entry.path(), &to)?;
}
}
Ok(())
Ok(self)
}

/// Add one specific file as a static file.
Expand All @@ -342,7 +345,7 @@ impl StaticFile {
/// 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(&mut self, path: impl AsRef<Path>) -> io::Result<()> {
pub fn add_file(&mut self, path: impl AsRef<Path>) -> Result<&mut Self> {
let path = self.path_for(path);
if let Some((name, ext)) = name_and_ext(&path) {
println!("cargo:rerun-if-changed={}", path.display());
Expand All @@ -360,7 +363,7 @@ impl StaticFile {
ext,
)?;
}
Ok(())
Ok(self)
}

/// Add one specific file as a static file.
Expand All @@ -370,12 +373,12 @@ impl StaticFile {
&mut self,
path: impl AsRef<Path>,
url_name: &str,
) -> io::Result<()> {
) -> Result<&mut Self> {
let path = &self.path_for(path);
let ext = name_and_ext(path).map_or("", |(_, e)| e);
println!("cargo:rerun-if-changed={}", path.display());
self.add_static(path, url_name, url_name, &FileContent(path), ext)?;
Ok(())
Ok(self)
}

/// Add a resource by its name and content, without reading an actual file.
Expand Down Expand Up @@ -418,7 +421,11 @@ impl StaticFile {
/// # }
/// assert_eq!(statics::black_css.name, "black-r3rltVhW.css");
/// ````
pub fn add_file_data<P>(&mut self, path: P, data: &[u8]) -> Result<()>
pub fn add_file_data<P>(
&mut self,
path: P,
data: &[u8],
) -> Result<&mut Self>
where
P: AsRef<Path>,
{
Expand All @@ -435,7 +442,7 @@ impl StaticFile {
ext,
)?;
}
Ok(())
Ok(self)
}

/// Compile a sass file and add the resulting css.
Expand All @@ -448,7 +455,7 @@ impl StaticFile {
/// This method is only available when ructe is built with the
/// "sass" feature.
#[cfg(feature = "sass")]
pub fn add_sass_file<P>(&mut self, src: P) -> Result<()>
pub fn add_sass_file<P>(&mut self, src: P) -> Result<&mut Self>
where
P: AsRef<Path>,
{
Expand Down Expand Up @@ -516,7 +523,7 @@ impl StaticFile {
url_name: &str,
content: &impl Display,
suffix: &str,
) -> io::Result<()> {
) -> Result<&mut Self> {
let rust_name = rust_name
.replace('/', "_")
.replace('-', "_")
Expand All @@ -538,7 +545,7 @@ impl StaticFile {
)?;
self.names.insert(rust_name.clone(), url_name.into());
self.names_r.insert(url_name.into(), rust_name);
Ok(())
Ok(self)
}

/// Get a mapping of names, from without hash to with.
Expand Down

0 comments on commit 460133b

Please sign in to comment.