Expand description
Simply parse .d
files
use std::borrow::Cow;
let input = r"
x.cpp.o: x.cpp \
include/foo.h \
include/bar.h
include/foo.h:
include/bar.h: f\ iz.h
";
let targets = depfile::parse(input).unwrap();
// this is a bit ugly because of Cow<'_, str>
// the Depfile struct only clones if unescaping is needed
assert_eq!(
targets.find("x.cpp.o").unwrap()
.iter()
.map(|x| x.as_ref())
.collect::<Vec<_>>(),
vec!["x.cpp", "include/foo.h", "include/bar.h"]
);
// iterator gives (&str, &[Cow<str>])
let mut iter = targets.iter();
let (target, deps) = iter.next().unwrap();
assert_eq!(target, "x.cpp.o");
assert_eq!(deps, &[
Cow::Borrowed("x.cpp"),
Cow::Borrowed("include/foo.h"),
Cow::Borrowed("include/bar.h")
]);
let (target, deps) = iter.next().unwrap();
assert_eq!(target, "include/foo.h");
assert!(deps.is_empty());
let (target, deps) = iter.next().unwrap();
assert_eq!(target, "include/bar.h");
// escaped strings are cloned when unescaping
assert_eq!(deps, &[Cow::<'static, str>::Owned("f iz.h".to_string())]);
assert_eq!(iter.next(), None);
// recursive (DFS)
let targets = depfile::parse(input).unwrap();
let mut all_deps = targets.recurse_deps("x.cpp.o");
assert_eq!(all_deps.next(), Some("x.cpp"));
assert_eq!(all_deps.next(), Some("include/foo.h"));
assert_eq!(all_deps.next(), Some("include/bar.h"));
assert_eq!(all_deps.next(), Some("f iz.h"));
assert_eq!(all_deps.next(), None);
Structs§
- Depfile
- Structure of a
.d
depfile. Constructed withparse
- Depfile
Iter - Iterator returned by
Depfile::iter
- Depfile
Recur Deps - Iterator returned by
Depfile::recurse_deps