use crate::anyhow::{Context, Result};
use crate::dist::Transformer;
use std::{fs, path::Path};
#[derive(Debug, Default)]
pub struct SassTransformer {
pub options: sass_rs::Options,
}
impl Transformer for SassTransformer {
fn transform(&self, source: &Path, dest: &Path) -> Result<bool> {
fn is_sass(path: &Path) -> bool {
matches!(
path.extension()
.and_then(|x| x.to_str().map(|x| x.to_lowercase()))
.as_deref(),
Some("sass") | Some("scss")
)
}
fn is_partial(path: &Path) -> bool {
path.file_name()
.expect("WalkDir does not yield paths ending with `..` or `.`")
.to_str()
.map(|x| x.starts_with('_'))
.unwrap_or(false)
}
if !is_sass(source) {
return Ok(false);
}
if is_partial(source) {
return Ok(true);
}
let dest = dest.with_extension("css");
let css = sass_rs::compile_file(source, self.options.clone()).map_err(|e| {
crate::anyhow::anyhow!("could not compile SASS file `{}`: {}", source.display(), e)
})?;
fs::write(&dest, css)
.with_context(|| format!("could not write CSS to `{}`", dest.display()))?;
Ok(true)
}
}