Skip to content

Commit

Permalink
Merge pull request #265 from Binlogo/op/target-kind-check
Browse files Browse the repository at this point in the history
Convert `Target::is_kind` pub and use macro to support all kind
  • Loading branch information
oli-obk authored Jul 4, 2024
2 parents 9cf9539 + 97c7440 commit 48d9985
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,44 +562,36 @@ pub struct Target {
pub doc: bool,
}

impl Target {
fn is_kind(&self, name: TargetKind) -> 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(TargetKind::Lib)
}

/// Return true if this target is of kind "bin".
pub fn is_bin(&self) -> bool {
self.is_kind(TargetKind::Bin)
}

/// Return true if this target is of kind "example".
pub fn is_example(&self) -> bool {
self.is_kind(TargetKind::Example)
}

/// Return true if this target is of kind "test".
pub fn is_test(&self) -> bool {
self.is_kind(TargetKind::Test)
}

/// Return true if this target is of kind "bench".
pub fn is_bench(&self) -> bool {
self.is_kind(TargetKind::Bench)
macro_rules! methods_target_is_kind {
($($name:ident => $kind:expr),*) => {
$(
/// Return true if this target is of kind `$kind`.
pub fn $name(&self) -> bool {
self.is_kind($kind)
}
)*
}
}

/// Return true if this target is of kind "custom-build".
pub fn is_custom_build(&self) -> bool {
self.is_kind(TargetKind::CustomBuild)
impl Target {
/// Return true if this target is of the given kind.
pub fn is_kind(&self, name: TargetKind) -> bool {
self.kind.iter().any(|kind| kind == &name)
}

/// Return true if this target is of kind "proc-macro".
pub fn is_proc_macro(&self) -> bool {
self.is_kind(TargetKind::ProcMacro)
// Generate `is_*` methods for each `TargetKind`
methods_target_is_kind! {
is_lib => TargetKind::Lib,
is_bin => TargetKind::Bin,
is_example => TargetKind::Example,
is_test => TargetKind::Test,
is_bench => TargetKind::Bench,
is_custom_build => TargetKind::CustomBuild,
is_proc_macro => TargetKind::ProcMacro,
is_cdylib => TargetKind::CDyLib,
is_dylib => TargetKind::DyLib,
is_rlib => TargetKind::RLib,
is_staticlib => TargetKind::StaticLib
}
}

Expand Down

0 comments on commit 48d9985

Please sign in to comment.