Skip to content

Commit

Permalink
Add is_lib, is_bin etc to Target
Browse files Browse the repository at this point in the history
  • Loading branch information
msrd0 committed Sep 9, 2022
1 parent c0e2194 commit ecabef4
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/lib.rs
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 ecabef4

Please sign in to comment.