]> git.lizzy.rs Git - rust.git/blob - src/modules.rs
Add a test for non-mod.rs mods
[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::io;
13 use std::path::{Path, PathBuf};
14
15 use syntax::ast;
16 use syntax::codemap::{self, FileName};
17 use syntax::parse::{parser, DirectoryOwnership};
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, None, 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     relative: Option<ast::Ident>,
45     codemap: &codemap::CodeMap,
46     result: &mut BTreeMap<FileName, &'a ast::Mod>,
47 ) -> Result<(), io::Error> {
48     debug!("list_submodules: search_dir: {:?}", search_dir);
49     for item in &module.items {
50         if let ast::ItemKind::Mod(ref sub_mod) = item.node {
51             if !contains_skip(&item.attrs) {
52                 let is_internal =
53                     codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
54                 let (dir_path, relative) = if is_internal {
55                     (search_dir.join(&item.ident.to_string()), None)
56                 } else {
57                     let (mod_path, relative) =
58                         module_file(item.ident, &item.attrs, search_dir, relative, codemap)?;
59                     let dir_path = mod_path.parent().unwrap().to_owned();
60                     result.insert(FileName::Real(mod_path), sub_mod);
61                     (dir_path, relative)
62                 };
63                 list_submodules(sub_mod, &dir_path, relative, codemap, result)?;
64             }
65         }
66     }
67     Ok(())
68 }
69
70 /// Find the file corresponding to an external mod
71 fn module_file(
72     id: ast::Ident,
73     attrs: &[ast::Attribute],
74     dir_path: &Path,
75     relative: Option<ast::Ident>,
76     codemap: &codemap::CodeMap,
77 ) -> Result<(PathBuf, Option<ast::Ident>), io::Error> {
78     if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
79         return Ok((path, None));
80     }
81
82     match parser::Parser::default_submod_path(id, relative, dir_path, codemap).result {
83         Ok(parser::ModulePathSuccess {
84             path,
85             directory_ownership,
86             ..
87         }) => {
88             let relative = if let DirectoryOwnership::Owned { relative } = directory_ownership {
89                 relative
90             } else {
91                 None
92             };
93             Ok((path, relative))
94         }
95         Err(_) => Err(io::Error::new(
96             io::ErrorKind::Other,
97             format!("Couldn't find module {}", id),
98         )),
99     }
100 }