]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/debug.rs
e22552008d5a8578079cf7fd3955081ec715c006
[rust.git] / src / librustc / dep_graph / debug.rs
1 // Copyright 2012-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 //! Code for debugging the dep-graph.
12
13 use super::dep_node::DepNode;
14 use std::error::Error;
15
16 /// A dep-node filter goes from a user-defined string to a query over
17 /// nodes. Right now the format is like this:
18 ///
19 ///     x & y & z
20 ///
21 /// where the format-string of the dep-node must contain `x`, `y`, and
22 /// `z`.
23 #[derive(Debug)]
24 pub struct DepNodeFilter {
25     text: String
26 }
27
28 impl DepNodeFilter {
29     pub fn new(text: &str) -> Self {
30         DepNodeFilter {
31             text: text.trim().to_string()
32         }
33     }
34
35     /// True if all nodes always pass the filter.
36     pub fn accepts_all(&self) -> bool {
37         self.text.is_empty()
38     }
39
40     /// Tests whether `node` meets the filter, returning true if so.
41     pub fn test(&self, node: &DepNode) -> bool {
42         let debug_str = format!("{:?}", node);
43         self.text.split("&")
44                  .map(|s| s.trim())
45                  .all(|f| debug_str.contains(f))
46     }
47 }
48
49 /// A filter like `F -> G` where `F` and `G` are valid dep-node
50 /// filters. This can be used to test the source/target independently.
51 pub struct EdgeFilter {
52     pub source: DepNodeFilter,
53     pub target: DepNodeFilter,
54 }
55
56 impl EdgeFilter {
57     pub fn new(test: &str) -> Result<EdgeFilter, Box<Error>> {
58         let parts: Vec<_> = test.split("->").collect();
59         if parts.len() != 2 {
60             Err(format!("expected a filter like `a&b -> c&d`, not `{}`", test).into())
61         } else {
62             Ok(EdgeFilter {
63                 source: DepNodeFilter::new(parts[0]),
64                 target: DepNodeFilter::new(parts[1]),
65             })
66         }
67     }
68
69     pub fn test(&self,
70                 source: &DepNode,
71                 target: &DepNode)
72                 -> bool {
73         self.source.test(source) && self.target.test(target)
74     }
75 }