]> git.lizzy.rs Git - rust.git/blob - src/modules.rs
Merge pull request #2291 from topecongiro/issue-2289
[rust.git] / src / modules.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::collections::BTreeMap;
12 use std::path::{Path, PathBuf};
13 use std::io;
14
15 use syntax::ast;
16 use syntax::codemap::{self, FileName};
17 use syntax::parse::parser;
18
19 use utils::contains_skip;
20
21 /// List all the files containing modules of a crate.
22 /// If a file is used twice in a crate, it appears only once.
23 pub fn list_files<'a>(
24     krate: &'a ast::Crate,
25     codemap: &codemap::CodeMap,
26 ) -> Result<BTreeMap<FileName, &'a ast::Mod>, io::Error> {
27     let mut result = BTreeMap::new(); // Enforce file order determinism
28     let root_filename = codemap.span_to_filename(krate.span);
29     {
30         let parent = match root_filename {
31             FileName::Real(ref path) => path.parent().unwrap(),
32             _ => Path::new(""),
33         };
34         list_submodules(&krate.module, parent, codemap, &mut result)?;
35     }
36     result.insert(root_filename, &krate.module);
37     Ok(result)
38 }
39
40 /// Recursively list all external modules included in a module.
41 fn list_submodules<'a>(
42     module: &'a ast::Mod,
43     search_dir: &Path,
44     codemap: &codemap::CodeMap,
45     result: &mut BTreeMap<FileName, &'a ast::Mod>,
46 ) -> Result<(), io::Error> {
47     debug!("list_submodules: search_dir: {:?}", search_dir);
48     for item in &module.items {
49         if let ast::ItemKind::Mod(ref sub_mod) = item.node {
50             if !contains_skip(&item.attrs) {
51                 let is_internal =
52                     codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
53                 let dir_path = if is_internal {
54                     search_dir.join(&item.ident.to_string())
55                 } else {
56                     let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap)?;
57                     let dir_path = mod_path.parent().unwrap().to_owned();
58                     result.insert(FileName::Real(mod_path), sub_mod);
59                     dir_path
60                 };
61                 list_submodules(sub_mod, &dir_path, codemap, result)?;
62             }
63         }
64     }
65     Ok(())
66 }
67
68 /// Find the file corresponding to an external mod
69 fn module_file(
70     id: ast::Ident,
71     attrs: &[ast::Attribute],
72     dir_path: &Path,
73     codemap: &codemap::CodeMap,
74 ) -> Result<PathBuf, io::Error> {
75     if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
76         return Ok(path);
77     }
78
79     match parser::Parser::default_submod_path(id, None, dir_path, codemap).result {
80         Ok(parser::ModulePathSuccess { path, .. }) => Ok(path),
81         Err(_) => Err(io::Error::new(
82             io::ErrorKind::Other,
83             format!("Couldn't find module {}", id),
84         )),
85     }
86 }