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