@@ -33,8 +33,13 @@ struct UnprocessedAsset {
33
33
file_path : PathBuf ,
34
34
}
35
35
36
+ pub struct Mapping {
37
+ pub path : PathBuf ,
38
+ pub content_type : Option < String > ,
39
+ }
40
+
36
41
pub struct Mappings {
37
- mappings : HashMap < String , PathBuf > ,
42
+ mappings : HashMap < String , Mapping > ,
38
43
imported_mappings : Vec < PathBuf > ,
39
44
file_cache : HashMap < PathBuf , Arc < PreprocessedAsset > > ,
40
45
caching_enabled : bool ,
@@ -68,9 +73,10 @@ impl Mappings {
68
73
where T : Into < Vec < u8 > > {
69
74
70
75
let asset = PreprocessedAsset :: process ( data. into ( ) ) ?;
76
+ let content_type = None ;
71
77
72
78
self . file_cache . insert ( key. into ( ) , Arc :: new ( asset) ) ;
73
- self . mappings . insert ( key. into ( ) , key. into ( ) ) ;
79
+ self . mappings . insert ( key. into ( ) , Mapping { path : key. into ( ) , content_type } ) ;
74
80
75
81
Ok ( ( ) )
76
82
}
@@ -95,10 +101,23 @@ impl Mappings {
95
101
let ( key, value) = mapping. split_at ( partition. unwrap ( ) ) ;
96
102
let ( key, value) = ( key. trim_end ( ) , value[ 2 ..] . trim_start ( ) ) ;
97
103
104
+ // extract content type
105
+ let ( value, content_type) = if let Some ( pos) = value. find ( '[' ) {
106
+ let ( value, type_start) = value. split_at ( pos) ;
107
+ let content_type = type_start[ 1 ..] . split ( ']' ) . next ( ) . unwrap ( ) ;
108
+ ( value. trim ( ) , Some ( content_type. trim ( ) . into ( ) ) )
109
+ } else {
110
+ ( value, None )
111
+ } ;
112
+
98
113
let path = [ prefix, Path :: new ( value) ] . iter ( ) . collect ( ) ;
99
114
100
- println ! ( "Adding mapping {} => {:?}" , key, path) ;
101
- self . mappings . insert ( key. to_owned ( ) , path) ;
115
+ if let Some ( content_type) = & content_type {
116
+ println ! ( "Adding mapping {} => {:?} [{}]" , key, path, content_type) ;
117
+ } else {
118
+ println ! ( "Adding mapping {} => {:?}" , key, path) ;
119
+ }
120
+ self . mappings . insert ( key. to_owned ( ) , Mapping { path, content_type } ) ;
102
121
}
103
122
104
123
self . imported_mappings . extend ( imports. iter ( ) . map ( From :: from) ) ;
@@ -128,7 +147,7 @@ impl Mappings {
128
147
println ! ( "Compressing mapped assets..." ) ;
129
148
let timer = Instant :: now ( ) ;
130
149
131
- for path in self . mappings . values ( ) {
150
+ for Mapping { path, .. } in self . mappings . values ( ) {
132
151
let entry = self . file_cache . entry ( path. clone ( ) ) ;
133
152
134
153
if let Entry :: Occupied ( _) = entry { continue ; }
@@ -148,26 +167,6 @@ impl Mappings {
148
167
}
149
168
}
150
169
151
- // let compression = Compression::best();
152
-
153
- // let mut enc = GzEncoder::new(Vec::new(), compression);
154
- // enc.write_all(&uncompressed_data)?;
155
- // let gzipped_data = enc.finish()?;
156
-
157
- // let mut enc = DeflateEncoder::new(Vec::new(), compression);
158
- // enc.write_all(&uncompressed_data)?;
159
- // let deflated_data = enc.finish()?;
160
-
161
- // println!(" {:.1}kB", uncompressed_data.len() as f32 / 2.0f32.powi(10));
162
- // println!("gzip -> {:.1}kB", gzipped_data.len() as f32 / 2.0f32.powi(10));
163
- // println!("defl -> {:.1}kB", gzipped_data.len() as f32 / 2.0f32.powi(10));
164
-
165
- // entry.or_insert(Arc::new(PreprocessedAsset{
166
- // uncompressed_data,
167
- // deflated_data,
168
- // gzipped_data
169
- // }));
170
-
171
170
entry. or_insert ( Arc :: new ( PreprocessedAsset :: process ( uncompressed_data) ?) ) ;
172
171
}
173
172
@@ -178,22 +177,18 @@ impl Mappings {
178
177
Ok ( ( ) )
179
178
}
180
179
181
- pub fn get_route ( & self , key : & str ) -> Option < & PathBuf > {
180
+ pub fn get_route ( & self , key : & str ) -> Option < & Mapping > {
182
181
self . mappings . get ( key)
183
182
}
184
183
185
- pub fn get_asset ( & self , key : & str ) -> Option < Arc < MappedAsset > > {
186
- let route = self . get_route ( key) ;
187
-
184
+ pub fn get_asset ( & self , route : & PathBuf ) -> Option < Arc < MappedAsset > > {
188
185
if self . caching_enabled {
189
- route. iter ( )
190
- . filter_map ( |& k| self . file_cache . get ( k) )
186
+ self . file_cache . get ( route)
191
187
. cloned ( )
192
- . next ( )
193
188
. map ( |a| a as Arc < MappedAsset > )
189
+
194
190
} else {
195
- route. cloned ( )
196
- . map ( |file_path| Arc :: new ( UnprocessedAsset { file_path} ) as Arc < MappedAsset > )
191
+ Some ( Arc :: new ( UnprocessedAsset { file_path : route. clone ( ) } ) as Arc < MappedAsset > )
197
192
}
198
193
}
199
194
}
0 commit comments