@@ -35,6 +35,8 @@ use std::collections::hash_map::Entry;
3535
3636use url:: { Url , UrlParser } ;
3737
38+ use Redirect :: * ;
39+
3840macro_rules! t {
3941 ( $e: expr) => ( match $e {
4042 Ok ( e) => e,
@@ -57,6 +59,12 @@ fn main() {
5759pub enum LoadError {
5860 IOError ( std:: io:: Error ) ,
5961 BrokenRedirect ( PathBuf , std:: io:: Error ) ,
62+ IsRedirect ,
63+ }
64+
65+ enum Redirect {
66+ SkipRedirect ,
67+ FromRedirect ( bool ) ,
6068}
6169
6270struct FileEntry {
@@ -156,7 +164,7 @@ fn check(cache: &mut Cache,
156164 let mut parser = UrlParser :: new ( ) ;
157165 parser. base_url ( base) ;
158166
159- let res = load_file ( cache, root, PathBuf :: from ( file) , false , false ) ;
167+ let res = load_file ( cache, root, PathBuf :: from ( file) , SkipRedirect ) ;
160168 let ( pretty_file, contents) = match res {
161169 Ok ( res) => res,
162170 Err ( _) => return None ,
@@ -182,7 +190,7 @@ fn check(cache: &mut Cache,
182190 if path. is_dir ( ) {
183191 return ;
184192 }
185- let res = load_file ( cache, root, path. clone ( ) , true , false ) ;
193+ let res = load_file ( cache, root, path. clone ( ) , FromRedirect ( false ) ) ;
186194 let ( pretty_path, contents) = match res {
187195 Ok ( res) => res,
188196 Err ( LoadError :: IOError ( err) ) => panic ! ( format!( "{}" , err) ) ,
@@ -191,6 +199,7 @@ fn check(cache: &mut Cache,
191199 pretty_file. display( ) , i + 1 , target. display( ) ) ;
192200 return ;
193201 }
202+ Err ( LoadError :: IsRedirect ) => unreachable ! ( ) ,
194203 } ;
195204
196205 if let Some ( ref fragment) = parsed_url. fragment {
@@ -225,8 +234,7 @@ fn check(cache: &mut Cache,
225234fn load_file ( cache : & mut Cache ,
226235 root : & Path ,
227236 file : PathBuf ,
228- follow_redirects : bool ,
229- is_redirect : bool ) -> Result < ( PathBuf , String ) , LoadError > {
237+ redirect : Redirect ) -> Result < ( PathBuf , String ) , LoadError > {
230238 let mut contents = String :: new ( ) ;
231239 let pretty_file = PathBuf :: from ( file. strip_prefix ( root) . unwrap_or ( & file) ) ;
232240
@@ -237,7 +245,7 @@ fn load_file(cache: &mut Cache,
237245 } ,
238246 Entry :: Vacant ( entry) => {
239247 let mut fp = try!( File :: open ( file. clone ( ) ) . map_err ( |err| {
240- if is_redirect {
248+ if let FromRedirect ( true ) = redirect {
241249 LoadError :: BrokenRedirect ( file. clone ( ) , err)
242250 } else {
243251 LoadError :: IOError ( err)
@@ -246,12 +254,12 @@ fn load_file(cache: &mut Cache,
246254 try!( fp. read_to_string ( & mut contents)
247255 . map_err ( |err| LoadError :: IOError ( err) ) ) ;
248256
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+ }
251262 } else {
252- None
253- } ;
254- if maybe. is_none ( ) {
255263 entry. insert ( FileEntry {
256264 source : contents. clone ( ) ,
257265 ids : HashSet :: new ( ) ,
@@ -266,9 +274,8 @@ fn load_file(cache: &mut Cache,
266274
267275 match maybe_redirect. and_then ( |url| url_to_file_path ( & parser, & url) ) {
268276 Some ( ( _, redirect_file) ) => {
269- assert ! ( follow_redirects) ;
270277 let path = PathBuf :: from ( redirect_file) ;
271- load_file ( cache, root, path, follow_redirects , true )
278+ load_file ( cache, root, path, FromRedirect ( true ) )
272279 }
273280 None => Ok ( ( pretty_file, contents) )
274281 }
0 commit comments