1+ use anyhow:: { Context as _, Error , Result } ;
12use 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
3849fn 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