-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
54 lines (43 loc) · 1.52 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::{
env::{self, VarError},
fs::File,
io::{self, Write},
path::PathBuf,
};
use grass::{Options, OutputStyle};
use grass_compiler::Error as SassError;
use thiserror::Error;
fn main() {
if let Err(error) = built::write_built_file() {
panic!("failed to acquire build-time information: {}", error);
}
if let Err(error) = compile_stylesheet() {
panic!("{error}");
}
}
#[derive(Debug, Error)]
enum CompileStylesheetError {
#[error("failed to compile SCSS: {0}")]
CompileSass(#[from] Box<SassError>),
#[error("could not read value of env var {0}: {1}")]
Var(&'static str, #[source] VarError),
#[error("could not create stylesheet output file {0}: {1}")]
CreateOutFile(PathBuf, #[source] io::Error),
#[error("could not write compiled CSS to {0}: {1}")]
WriteCss(PathBuf, #[source] io::Error),
}
fn compile_stylesheet() -> Result<(), CompileStylesheetError> {
println!("cargo:rerun-if-changed=scss/");
let compiled_css = grass::from_path(
"scss/style.scss",
&Options::default().style(OutputStyle::Compressed),
)?;
let mut out_path: PathBuf = env::var("OUT_DIR")
.map_err(|err| CompileStylesheetError::Var("OUT_DIR", err))?
.into();
out_path.push("style.css");
let mut stylesheet = File::create(&out_path)
.map_err(|err| CompileStylesheetError::CreateOutFile(out_path.clone(), err))?;
write!(stylesheet, "{}", compiled_css)
.map_err(|err| CompileStylesheetError::WriteCss(out_path, err))
}