Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Target::is_kind pub and use macro to support all kind #265

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading