@@ -35,6 +35,8 @@ use std::collections::hash_map::Entry;
35
35
36
36
use url:: { Url , UrlParser } ;
37
37
38
+ use Redirect :: * ;
39
+
38
40
macro_rules! t {
39
41
( $e: expr) => ( match $e {
40
42
Ok ( e) => e,
@@ -57,6 +59,12 @@ fn main() {
57
59
pub enum LoadError {
58
60
IOError ( std:: io:: Error ) ,
59
61
BrokenRedirect ( PathBuf , std:: io:: Error ) ,
62
+ IsRedirect ,
63
+ }
64
+
65
+ enum Redirect {
66
+ SkipRedirect ,
67
+ FromRedirect ( bool ) ,
60
68
}
61
69
62
70
struct FileEntry {
@@ -156,7 +164,7 @@ fn check(cache: &mut Cache,
156
164
let mut parser = UrlParser :: new ( ) ;
157
165
parser. base_url ( base) ;
158
166
159
- let res = load_file ( cache, root, PathBuf :: from ( file) , false , false ) ;
167
+ let res = load_file ( cache, root, PathBuf :: from ( file) , SkipRedirect ) ;
160
168
let ( pretty_file, contents) = match res {
161
169
Ok ( res) => res,
162
170
Err ( _) => return None ,
@@ -182,7 +190,7 @@ fn check(cache: &mut Cache,
182
190
if path. is_dir ( ) {
183
191
return ;
184
192
}
185
- let res = load_file ( cache, root, path. clone ( ) , true , false ) ;
193
+ let res = load_file ( cache, root, path. clone ( ) , FromRedirect ( false ) ) ;
186
194
let ( pretty_path, contents) = match res {
187
195
Ok ( res) => res,
188
196
Err ( LoadError :: IOError ( err) ) => panic ! ( format!( "{}" , err) ) ,
@@ -191,6 +199,7 @@ fn check(cache: &mut Cache,
191
199
pretty_file. display( ) , i + 1 , target. display( ) ) ;
192
200
return ;
193
201
}
202
+ Err ( LoadError :: IsRedirect ) => unreachable ! ( ) ,
194
203
} ;
195
204
196
205
if let Some ( ref fragment) = parsed_url. fragment {
@@ -225,8 +234,7 @@ fn check(cache: &mut Cache,
225
234
fn load_file ( cache : & mut Cache ,
226
235
root : & Path ,
227
236
file : PathBuf ,
228
- follow_redirects : bool ,
229
- is_redirect : bool ) -> Result < ( PathBuf , String ) , LoadError > {
237
+ redirect : Redirect ) -> Result < ( PathBuf , String ) , LoadError > {
230
238
let mut contents = String :: new ( ) ;
231
239
let pretty_file = PathBuf :: from ( file. strip_prefix ( root) . unwrap_or ( & file) ) ;
232
240
@@ -237,7 +245,7 @@ fn load_file(cache: &mut Cache,
237
245
} ,
238
246
Entry :: Vacant ( entry) => {
239
247
let mut fp = try!( File :: open ( file. clone ( ) ) . map_err ( |err| {
240
- if is_redirect {
248
+ if let FromRedirect ( true ) = redirect {
241
249
LoadError :: BrokenRedirect ( file. clone ( ) , err)
242
250
} else {
243
251
LoadError :: IOError ( err)
@@ -246,12 +254,12 @@ fn load_file(cache: &mut Cache,
246
254
try!( fp. read_to_string ( & mut contents)
247
255
. map_err ( |err| LoadError :: IOError ( err) ) ) ;
248
256
249
- let maybe = if follow_redirects {
250
- maybe_redirect ( & contents)
257
+ let maybe = maybe_redirect ( & contents) ;
258
+ if maybe. is_some ( ) {
259
+ if let SkipRedirect = redirect {
260
+ return Err ( LoadError :: IsRedirect ) ;
261
+ }
251
262
} else {
252
- None
253
- } ;
254
- if maybe. is_none ( ) {
255
263
entry. insert ( FileEntry {
256
264
source : contents. clone ( ) ,
257
265
ids : HashSet :: new ( ) ,
@@ -266,9 +274,8 @@ fn load_file(cache: &mut Cache,
266
274
267
275
match maybe_redirect. and_then ( |url| url_to_file_path ( & parser, & url) ) {
268
276
Some ( ( _, redirect_file) ) => {
269
- assert ! ( follow_redirects) ;
270
277
let path = PathBuf :: from ( redirect_file) ;
271
- load_file ( cache, root, path, follow_redirects , true )
278
+ load_file ( cache, root, path, FromRedirect ( true ) )
272
279
}
273
280
None => Ok ( ( pretty_file, contents) )
274
281
}
0 commit comments