pub trait Transformer {
// Required method
fn transform(&self, source: &Path, dest: &Path) -> Result<bool>;
}Expand description
A type that can transform or copy a single asset file during Dist::build.
Implement this trait to customise how individual files in the assets directory are processed before they land in the dist directory — for example to compile SASS to CSS, minify JavaScript, or generate additional output files from a source file.
Return Ok(true) if the file was handled (the transformer wrote its own output).
Return Ok(false) to fall through to the next transformer, or to the default
plain-copy behaviour if no transformer claims the file.
A blanket implementation is provided for () (no-op, always returns Ok(false)),
so the trait is easy to stub out in tests.
§Examples
use std::path::Path;
use xtask_wasm::{anyhow::Result, clap, Transformer};
struct UppercaseText;
impl Transformer for UppercaseText {
fn transform(&self, source: &Path, dest: &Path) -> Result<bool> {
if source.extension().and_then(|e| e.to_str()) == Some("txt") {
let content = std::fs::read_to_string(source)?;
std::fs::write(dest, content.to_uppercase())?;
return Ok(true);
}
Ok(false)
}
}
#[derive(clap::Parser)]
enum Opt {
Dist(xtask_wasm::Dist),
}
fn main() -> Result<()> {
let opt: Opt = clap::Parser::parse();
match opt {
Opt::Dist(dist) => {
dist.transformer(UppercaseText)
.build("my-project")?;
}
}
Ok(())
}Required Methods§
Sourcefn transform(&self, source: &Path, dest: &Path) -> Result<bool>
fn transform(&self, source: &Path, dest: &Path) -> Result<bool>
Process a single asset file.
source is the absolute path to the file in the assets directory.
dest is the intended output path inside the dist directory, preserving
the same relative path as source (the implementor may change the extension).
Return Ok(true) if the file was handled, Ok(false) to defer.
Implementations on Foreign Types§
Source§impl Transformer for ()
impl Transformer for ()
Implementors§
impl Transformer for SassTransformer
sass only.