From ecabef4a5fc3167801194c194dfe480b8dbc93d8 Mon Sep 17 00:00:00 2001 From: Dominic Date: Fri, 9 Sep 2022 18:24:01 +0200 Subject: [PATCH] Add `is_lib`, `is_bin` etc to `Target` --- src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4a9cb152..b71fb17a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -425,7 +425,7 @@ impl std::fmt::Display for Source { pub struct Target { /// Name as given in the `Cargo.toml` or generated from the file name pub name: String, - /// Kind of target ("bin", "example", "test", "bench", "lib") + /// Kind of target ("bin", "example", "test", "bench", "lib", "custom-build") pub kind: Vec, /// Almost the same as `kind`, except when an example is a library instead of an executable. /// In that case `crate_types` contains things like `rlib` and `dylib` while `kind` is `example` @@ -466,6 +466,42 @@ pub struct Target { pub doc: bool, } +impl Target { + fn is_kind(&self, name: &str) -> bool { + self.kind.iter().any(|kind| kind == name) + } + + /// Return true if this target is of kind "lib". + pub fn is_lib(&self) -> bool { + self.is_kind("lib") + } + + /// Return true if this target is of kind "bin". + pub fn is_bin(&self) -> bool { + self.is_kind("bin") + } + + /// Return true if this target is of kind "example". + pub fn is_example(&self) -> bool { + self.is_kind("example") + } + + /// Return true if this target is of kind "test". + pub fn is_test(&self) -> bool { + self.is_kind("test") + } + + /// Return true if this target is of kind "bench". + pub fn is_bench(&self) -> bool { + self.is_kind("bench") + } + + /// Return true if this target is of kind "custom-build". + pub fn is_custom_build(&self) -> bool { + self.is_kind("custom-build") + } +} + /// The Rust edition /// /// As of writing this comment rust editions 2024, 2027 and 2030 are not actually a thing yet but are parsed nonetheless for future proofing.