X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmodules.rs;h=30deafd3f9e99d584ac987f0c22136a5c15546ad;hb=89f27764edcd5086278e032603efb8ea9b1591c1;hp=5b5050d51c9641576029b8731246c6d8e006228d;hpb=bd1eff55bb70112c7f0cbca9333d4864625484c1;p=rust.git diff --git a/src/modules.rs b/src/modules.rs index 5b5050d51c9..30deafd3f9e 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -8,22 +8,22 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use utils; - -use std::path::{Path, PathBuf}; use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; +use std::io; use syntax::ast; use syntax::codemap; use syntax::parse::parser; +use utils::contains_skip; /// List all the files containing modules of a crate. /// If a file is used twice in a crate, it appears only once. pub fn list_files<'a>( krate: &'a ast::Crate, codemap: &codemap::CodeMap, -) -> BTreeMap { +) -> Result, io::Error> { let mut result = BTreeMap::new(); // Enforce file order determinism let root_filename: PathBuf = codemap.span_to_filename(krate.span).into(); list_submodules( @@ -31,9 +31,9 @@ pub fn list_files<'a>( root_filename.parent().unwrap(), codemap, &mut result, - ); + )?; result.insert(root_filename, &krate.module); - result + Ok(result) } /// Recursively list all external modules included in a module. @@ -42,25 +42,26 @@ fn list_submodules<'a>( search_dir: &Path, codemap: &codemap::CodeMap, result: &mut BTreeMap, -) { +) -> Result<(), io::Error> { debug!("list_submodules: search_dir: {:?}", search_dir); for item in &module.items { if let ast::ItemKind::Mod(ref sub_mod) = item.node { - if !utils::contains_skip(&item.attrs) { - let is_internal = codemap.span_to_filename(item.span) == - codemap.span_to_filename(sub_mod.inner); + if !contains_skip(&item.attrs) { + let is_internal = + codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner); let dir_path = if is_internal { search_dir.join(&item.ident.to_string()) } else { - let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap); + let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap)?; let dir_path = mod_path.parent().unwrap().to_owned(); result.insert(mod_path, sub_mod); dir_path }; - list_submodules(sub_mod, &dir_path, codemap, result); + list_submodules(sub_mod, &dir_path, codemap, result)?; } } } + Ok(()) } /// Find the file corresponding to an external mod @@ -69,13 +70,16 @@ fn module_file( attrs: &[ast::Attribute], dir_path: &Path, codemap: &codemap::CodeMap, -) -> PathBuf { +) -> Result { if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) { - return path; + return Ok(path); } match parser::Parser::default_submod_path(id, dir_path, codemap).result { - Ok(parser::ModulePathSuccess { path, .. }) => path, - Err(_) => panic!("Couldn't find module {}", id), + Ok(parser::ModulePathSuccess { path, .. }) => Ok(path), + Err(_) => Err(io::Error::new( + io::ErrorKind::Other, + format!("Couldn't find module {}", id), + )), } }