]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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; | |
c34b1796 AL |
12 | use std::path::{Path, PathBuf}; |
13 | use session::early_error; | |
1a4d82fc | 14 | |
85aaf69f | 15 | #[derive(Clone, Debug)] |
1a4d82fc | 16 | pub struct SearchPaths { |
c34b1796 | 17 | paths: Vec<(PathKind, PathBuf)>, |
1a4d82fc JJ |
18 | } |
19 | ||
20 | pub struct Iter<'a> { | |
21 | kind: PathKind, | |
c34b1796 | 22 | iter: slice::Iter<'a, (PathKind, PathBuf)>, |
1a4d82fc JJ |
23 | } |
24 | ||
85aaf69f | 25 | #[derive(Eq, PartialEq, Clone, Copy, Debug)] |
1a4d82fc JJ |
26 | pub enum PathKind { |
27 | Native, | |
28 | Crate, | |
29 | Dependency, | |
85aaf69f SL |
30 | Framework, |
31 | ExternFlag, | |
1a4d82fc JJ |
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) { | |
41 | let (kind, path) = if path.starts_with("native=") { | |
85aaf69f | 42 | (PathKind::Native, &path["native=".len()..]) |
1a4d82fc | 43 | } else if path.starts_with("crate=") { |
85aaf69f | 44 | (PathKind::Crate, &path["crate=".len()..]) |
1a4d82fc | 45 | } else if path.starts_with("dependency=") { |
85aaf69f SL |
46 | (PathKind::Dependency, &path["dependency=".len()..]) |
47 | } else if path.starts_with("framework=") { | |
48 | (PathKind::Framework, &path["framework=".len()..]) | |
1a4d82fc | 49 | } else if path.starts_with("all=") { |
85aaf69f | 50 | (PathKind::All, &path["all=".len()..]) |
1a4d82fc JJ |
51 | } else { |
52 | (PathKind::All, path) | |
53 | }; | |
c34b1796 AL |
54 | if path.is_empty() { |
55 | early_error("empty search path given via `-L`"); | |
56 | } | |
57 | self.paths.push((kind, PathBuf::from(path))); | |
1a4d82fc JJ |
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> { | |
85aaf69f | 66 | type Item = (&'a Path, PathKind); |
1a4d82fc | 67 | |
85aaf69f | 68 | fn next(&mut self) -> Option<(&'a Path, PathKind)> { |
1a4d82fc JJ |
69 | loop { |
70 | match self.iter.next() { | |
71 | Some(&(kind, ref p)) if self.kind == PathKind::All || | |
72 | kind == PathKind::All || | |
85aaf69f SL |
73 | kind == self.kind => { |
74 | return Some((p, kind)) | |
75 | } | |
1a4d82fc JJ |
76 | Some(..) => {} |
77 | None => return None, | |
78 | } | |
79 | } | |
80 | } | |
81 | } |