]> git.lizzy.rs Git - rust.git/blob - src/librustc/session/search_paths.rs
Auto merge of #30457 - Manishearth:rollup, r=Manishearth
[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 use std::path::{Path, PathBuf};
13 use session::early_error;
14 use syntax::errors;
15
16 #[derive(Clone, Debug)]
17 pub struct SearchPaths {
18     paths: Vec<(PathKind, PathBuf)>,
19 }
20
21 pub struct Iter<'a> {
22     kind: PathKind,
23     iter: slice::Iter<'a, (PathKind, PathBuf)>,
24 }
25
26 #[derive(Eq, PartialEq, Clone, Copy, Debug)]
27 pub enum PathKind {
28     Native,
29     Crate,
30     Dependency,
31     Framework,
32     ExternFlag,
33     All,
34 }
35
36 impl SearchPaths {
37     pub fn new() -> SearchPaths {
38         SearchPaths { paths: Vec::new() }
39     }
40
41     pub fn add_path(&mut self, path: &str, color: errors::ColorConfig) {
42         let (kind, path) = if path.starts_with("native=") {
43             (PathKind::Native, &path["native=".len()..])
44         } else if path.starts_with("crate=") {
45             (PathKind::Crate, &path["crate=".len()..])
46         } else if path.starts_with("dependency=") {
47             (PathKind::Dependency, &path["dependency=".len()..])
48         } else if path.starts_with("framework=") {
49             (PathKind::Framework, &path["framework=".len()..])
50         } else if path.starts_with("all=") {
51             (PathKind::All, &path["all=".len()..])
52         } else {
53             (PathKind::All, path)
54         };
55         if path.is_empty() {
56             early_error(color, "empty search path given via `-L`");
57         }
58         self.paths.push((kind, PathBuf::from(path)));
59     }
60
61     pub fn iter(&self, kind: PathKind) -> Iter {
62         Iter { kind: kind, iter: self.paths.iter() }
63     }
64 }
65
66 impl<'a> Iterator for Iter<'a> {
67     type Item = (&'a Path, PathKind);
68
69     fn next(&mut self) -> Option<(&'a Path, PathKind)> {
70         loop {
71             match self.iter.next() {
72                 Some(&(kind, ref p)) if self.kind == PathKind::All ||
73                                         kind == PathKind::All ||
74                                         kind == self.kind => {
75                     return Some((p, kind))
76                 }
77                 Some(..) => {}
78                 None => return None,
79             }
80         }
81     }
82 }