use thiserror::Error;
use crate::{SiteConfig, error::HauchiwaError, loader::GlobRegistryTask, task::Handle};
#[derive(Debug, Error)]
pub enum StyleError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Sass compilation error: {0}")]
Sass(#[from] Box<grass::Error>),
#[error("Build error: {0}")]
Build(#[from] crate::error::BuildError),
}
#[derive(Debug, Clone)]
pub struct Stylesheet {
pub path: camino::Utf8PathBuf,
}
impl<G> SiteConfig<G>
where
G: Send + Sync + 'static,
{
pub fn load_css(
&mut self,
glob_entry: &'static str,
glob_watch: &'static str,
) -> Result<Handle<super::Registry<Stylesheet>>, HauchiwaError> {
Ok(self.add_task_opaque(GlobRegistryTask::new(
vec![glob_entry],
vec![glob_watch],
move |_, rt, file| {
let data = grass::from_path(&file.path, &grass::Options::default())
.map_err(StyleError::Sass)?;
let path = rt
.store(data.as_bytes(), "css")
.map_err(StyleError::Build)?;
Ok((file.path, Stylesheet { path }))
},
)?))
}
}