Skip to content

Commit

Permalink
Merge pull request #205 from msrd0/add-target-is-xxx
Browse files Browse the repository at this point in the history
Add `is_lib`, `is_bin` etc to `Target`
  • Loading branch information
oli-obk authored Sep 13, 2022
2 parents c0e2194 + ecabef4 commit 2d2998c
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// 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`
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2d2998c

Please sign in to comment.