Skip to content

Commit af56f94

Browse files
Nemo157jyn514
authored andcommitted
Switch build.rs to use anyhow
1 parent 3d2d3cb commit af56f94

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ git2 = { version = "0.14", default-features = false }
121121
sass-rs = "0.2.2"
122122
string_cache_codegen = "0.5.1"
123123
walkdir = "2"
124+
anyhow = { version = "1.0.42", features = ["backtrace"] }
124125

125126
[[bench]]
126127
name = "compression"

build.rs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
1+
use anyhow::{Context as _, Error, Result};
12
use git2::Repository;
2-
use std::{env, error::Error, fs::File, io::Write, path::Path};
3-
4-
fn main() {
5-
write_git_version();
6-
if let Err(sass_err) = compile_sass() {
7-
panic!("Error compiling sass: {}", sass_err);
8-
}
9-
write_known_targets().unwrap();
3+
use std::{env, fs::File, io::Write, path::Path};
4+
5+
fn main() -> Result<()> {
6+
let out_dir = env::var("OUT_DIR").context("missing OUT_DIR")?;
7+
let out_dir = Path::new(&out_dir);
8+
write_git_version(out_dir)?;
9+
compile_sass(out_dir)?;
10+
write_known_targets(out_dir)?;
11+
Ok(())
1012
}
1113

12-
fn write_git_version() {
13-
let maybe_hash = get_git_hash();
14+
fn write_git_version(out_dir: &Path) -> Result<()> {
15+
let maybe_hash = get_git_hash()?;
1416
let git_hash = maybe_hash.as_deref().unwrap_or("???????");
1517

1618
let build_date = time::OffsetDateTime::now_utc().date();
17-
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("git_version");
19+
let dest_path = out_dir.join("git_version");
1820

19-
let mut file = File::create(&dest_path).unwrap();
20-
write!(file, "({} {})", git_hash, build_date).unwrap();
21+
let mut file = File::create(&dest_path)?;
22+
write!(file, "({} {})", git_hash, build_date)?;
2123

2224
// TODO: are these right?
2325
println!("cargo:rerun-if-changed=.git/HEAD");
2426
println!("cargo:rerun-if-changed=.git/index");
27+
28+
Ok(())
2529
}
2630

27-
fn get_git_hash() -> Option<String> {
28-
let repo = Repository::open(env::current_dir().unwrap()).ok()?;
29-
let head = repo.head().unwrap();
31+
fn get_git_hash() -> Result<Option<String>> {
32+
match Repository::open(env::current_dir()?) {
33+
Ok(repo) => {
34+
let head = repo.head()?;
3035

31-
head.target().map(|h| {
32-
let mut h = format!("{}", h);
33-
h.truncate(7);
34-
h
35-
})
36+
Ok(head.target().map(|h| {
37+
let mut h = format!("{}", h);
38+
h.truncate(7);
39+
h
40+
}))
41+
}
42+
Err(err) => {
43+
eprintln!("failed to get git repo: {err}");
44+
Ok(None)
45+
}
46+
}
3647
}
3748

3849
fn compile_sass_file(
50+
out_dir: &Path,
3951
name: &str,
4052
target: &str,
4153
include_paths: &[String],
42-
) -> Result<(), Box<dyn Error>> {
54+
) -> Result<()> {
4355
use sass_rs::{Context, Options, OutputStyle};
4456

4557
const STYLE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/templates/style");
@@ -56,31 +68,33 @@ fn compile_sass_file(
5668
}
5769
}
5870

59-
let mut context = Context::new_file(format!("{}/{}.scss", STYLE_DIR, name))?;
71+
let mut context =
72+
Context::new_file(format!("{}/{}.scss", STYLE_DIR, name)).map_err(Error::msg)?;
6073
context.set_options(Options {
6174
output_style: OutputStyle::Compressed,
6275
include_paths,
6376
..Default::default()
6477
});
6578

66-
let css = context.compile()?;
67-
let dest_path = Path::new(&env::var("OUT_DIR")?).join(format!("{}.css", target));
79+
let css = context.compile().map_err(Error::msg)?;
80+
let dest_path = out_dir.join(format!("{}.css", target));
6881
let mut file = File::create(&dest_path)?;
6982
file.write_all(css.as_bytes())?;
7083

7184
Ok(())
7285
}
7386

74-
fn compile_sass() -> Result<(), Box<dyn Error>> {
87+
fn compile_sass(out_dir: &Path) -> Result<()> {
7588
// Compile base.scss -> style.css
76-
compile_sass_file("base", "style", &[])?;
89+
compile_sass_file(out_dir, "base", "style", &[])?;
7790

7891
// Compile rustdoc.scss -> rustdoc.css
79-
compile_sass_file("rustdoc", "rustdoc", &[])?;
80-
compile_sass_file("rustdoc-2021-12-05", "rustdoc-2021-12-05", &[])?;
92+
compile_sass_file(out_dir, "rustdoc", "rustdoc", &[])?;
93+
compile_sass_file(out_dir, "rustdoc-2021-12-05", "rustdoc-2021-12-05", &[])?;
8194

8295
// Compile vendored.scss -> vendored.css
8396
compile_sass_file(
97+
out_dir,
8498
"vendored",
8599
"vendored",
86100
&[concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned()],
@@ -89,7 +103,7 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
89103
Ok(())
90104
}
91105

92-
fn write_known_targets() -> std::io::Result<()> {
106+
fn write_known_targets(out_dir: &Path) -> Result<()> {
93107
use std::io::BufRead;
94108

95109
let targets: Vec<String> = std::process::Command::new("rustc")
@@ -102,7 +116,7 @@ fn write_known_targets() -> std::io::Result<()> {
102116

103117
string_cache_codegen::AtomType::new("target::TargetAtom", "target_atom!")
104118
.atoms(&targets)
105-
.write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("target_atom.rs"))?;
119+
.write_to_file(&out_dir.join("target_atom.rs"))?;
106120

107121
Ok(())
108122
}

0 commit comments

Comments
 (0)