]> git.lizzy.rs Git - rust.git/blob - src/librustc/session/search_paths.rs
Fallout from stabilization.
[rust.git] / src / librustc / session / search_paths.rs
1 // Copyright 2014 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::slice;
12
13 #[derive(Clone, Show)]
14 pub struct SearchPaths {
15     paths: Vec<(PathKind, Path)>,
16 }
17
18 pub struct Iter<'a> {
19     kind: PathKind,
20     iter: slice::Iter<'a, (PathKind, Path)>,
21 }
22
23 #[derive(Eq, PartialEq, Clone, Copy, Show)]
24 pub enum PathKind {
25     Native,
26     Crate,
27     Dependency,
28     ExternFlag,
29     All,
30 }
31
32 impl SearchPaths {
33     pub fn new() -> SearchPaths {
34         SearchPaths { paths: Vec::new() }
35     }
36
37     pub fn add_path(&mut self, path: &str) {
38         let (kind, path) = if path.starts_with("native=") {
39             (PathKind::Native, &path["native=".len()..])
40         } else if path.starts_with("crate=") {
41             (PathKind::Crate, &path["crate=".len()..])
42         } else if path.starts_with("dependency=") {
43             (PathKind::Dependency, &path["dependency=".len()..])
44         } else if path.starts_with("all=") {
45             (PathKind::All, &path["all=".len()..])
46         } else {
47             (PathKind::All, path)
48         };
49         self.paths.push((kind, Path::new(path)));
50     }
51
52     pub fn iter(&self, kind: PathKind) -> Iter {
53         Iter { kind: kind, iter: self.paths.iter() }
54     }
55 }
56
57 impl<'a> Iterator for Iter<'a> {
58     type Item = (&'a Path, PathKind);
59
60     fn next(&mut self) -> Option<(&'a Path, PathKind)> {
61         loop {
62             match self.iter.next() {
63                 Some(&(kind, ref p)) if self.kind == PathKind::All ||
64                                         kind == PathKind::All ||
65                                         kind == self.kind => {
66                     return Some((p, kind))
67                 }
68                 Some(..) => {}
69                 None => return None,
70             }
71         }
72     }
73 }