use std::collections::BTreeMap;
use std::io;
use std::path::{Path, PathBuf};
use syntax::ast;
use syntax::codemap::{self, FileName};
use syntax::parse::parser;
use utils::contains_skip;
pub fn list_files<'a>(
krate: &'a ast::Crate,
codemap: &codemap::CodeMap,
) -> Result<BTreeMap<FileName, &'a ast::Mod>, io::Error> {
let mut result = BTreeMap::new(); let root_filename = codemap.span_to_filename(krate.span);
{
let parent = match root_filename {
FileName::Real(ref path) => path.parent().unwrap(),
_ => Path::new(""),
};
list_submodules(&krate.module, parent, codemap, &mut result)?;
}
result.insert(root_filename, &krate.module);
Ok(result)
}
fn list_submodules<'a>(
module: &'a ast::Mod,
search_dir: &Path,
codemap: &codemap::CodeMap,
result: &mut BTreeMap<FileName, &'a ast::Mod>,
) -> 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 !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 dir_path = mod_path.parent().unwrap().to_owned();
result.insert(FileName::Real(mod_path), sub_mod);
dir_path
};
list_submodules(sub_mod, &dir_path, codemap, result)?;
}
}
}
Ok(())
}
fn module_file(
id: ast::Ident,
attrs: &[ast::Attribute],
dir_path: &Path,
codemap: &codemap::CodeMap,
) -> Result<PathBuf, io::Error> {
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
return Ok(path);
}
match parser::Parser::default_submod_path(id, None, dir_path, codemap).result {
Ok(parser::ModulePathSuccess { path, .. }) => Ok(path),
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
format!("Couldn't find module {}", id),
)),
}
}