11use anyhow:: { Context as _, Error , Result } ;
2- use std:: { env, fs:: File , io:: Write as _, path:: Path } ;
2+ use std:: { collections :: BTreeMap , env, fs:: File , io:: Write as _, path:: Path } ;
33
44mod tracked {
55 use std:: {
@@ -68,7 +68,7 @@ mod tracked {
6868 }
6969}
7070
71- type ETagMap < ' a > = phf_codegen :: Map < ' a , String > ;
71+ type ETagMap = BTreeMap < String , String > ;
7272
7373fn main ( ) -> Result < ( ) > {
7474 let out_dir = env:: var ( "OUT_DIR" ) . context ( "missing OUT_DIR" ) ?;
@@ -81,11 +81,13 @@ fn main() -> Result<()> {
8181 calculate_static_etags ( & mut etag_map) ?;
8282
8383 let mut etag_file = File :: create ( out_dir. join ( "static_etag_map.rs" ) ) ?;
84- writeln ! (
85- & mut etag_file,
86- "pub static STATIC_ETAG_MAP: ::phf::Map<&'static str, &'static str> = {};" ,
87- etag_map. build( )
88- ) ?;
84+ writeln ! ( etag_file, "pub const STATIC_ETAG_MAP: &[(&str, &str)] = &[" ) ?;
85+ for ( path, etag) in etag_map. iter ( ) {
86+ // the debug repr of a `str` is also a valid escaped string literal in the code
87+ writeln ! ( etag_file, r#" ({:?}, {:?}), "# , path, etag) ?;
88+ }
89+ writeln ! ( etag_file, "];" ) ?;
90+
8991 etag_file. sync_all ( ) ?;
9092
9193 // trigger recompilation when a new migration is added
@@ -100,7 +102,7 @@ fn etag_from_path(path: impl AsRef<Path>) -> Result<String> {
100102fn etag_from_content ( content : impl AsRef < [ u8 ] > ) -> String {
101103 let digest = md5:: compute ( content) ;
102104 let md5_hex = format ! ( "{:x}" , digest) ;
103- format ! ( r#""\" {md5_hex}\" ""# )
105+ format ! ( r#""{md5_hex}""# )
104106}
105107
106108fn compile_sass_file ( src : & Path , dest : & Path ) -> Result < ( ) > {
@@ -137,7 +139,7 @@ fn compile_sass(out_dir: &Path, etag_map: &mut ETagMap) -> Result<()> {
137139 } ) ?;
138140
139141 let dest_str = dest. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) ;
140- etag_map. entry ( dest_str, etag_from_path ( & dest) ?) ;
142+ etag_map. insert ( dest_str, etag_from_path ( & dest) ?) ;
141143 }
142144 }
143145 }
@@ -148,7 +150,7 @@ fn compile_sass(out_dir: &Path, etag_map: &mut ETagMap) -> Result<()> {
148150 let vendored = pure + & grids;
149151 std:: fs:: write ( out_dir. join ( "vendored" ) . with_extension ( "css" ) , & vendored) ?;
150152
151- etag_map. entry (
153+ etag_map. insert (
152154 "vendored.css" . to_owned ( ) ,
153155 etag_from_content ( vendored. as_bytes ( ) ) ,
154156 ) ;
@@ -169,7 +171,7 @@ fn calculate_static_etags(etag_map: &mut ETagMap) -> Result<()> {
169171
170172 let partial_path = path. strip_prefix ( static_dir) . unwrap ( ) ;
171173 let partial_path_str = partial_path. to_string_lossy ( ) . to_string ( ) ;
172- etag_map. entry ( partial_path_str, etag_from_path ( path) ?) ;
174+ etag_map. insert ( partial_path_str, etag_from_path ( path) ?) ;
173175 }
174176 }
175177
0 commit comments