Skip to content

Commit

Permalink
Convert Target::is_kind pub and use macro to support all kind
Browse files Browse the repository at this point in the history
  • Loading branch information
Binlogo committed Jul 2, 2024
1 parent 9cf9539 commit 97c7440
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 97c7440

Please sign in to comment.