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