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

add FromStr and Display impls for TargetKind and CrateType #276

Merged
merged 1 commit into from
Dec 3, 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
52 changes: 51 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use std::fmt;
use std::hash::Hash;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::str::from_utf8;
use std::str::{from_utf8, FromStr};

pub use camino;
pub use semver;
Expand Down Expand Up @@ -699,6 +699,33 @@ impl From<&str> for TargetKind {
}
}

impl FromStr for TargetKind {
type Err = std::convert::Infallible;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(TargetKind::from(s))
}
}

impl fmt::Display for TargetKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bench => "bench".fmt(f),
Self::Bin => "bin".fmt(f),
Self::CustomBuild => "custom-build".fmt(f),
Self::CDyLib => "cdylib".fmt(f),
Self::DyLib => "dylib".fmt(f),
Self::Example => "example".fmt(f),
Self::Lib => "lib".fmt(f),
Self::ProcMacro => "proc-macro".fmt(f),
Self::RLib => "rlib".fmt(f),
Self::StaticLib => "staticlib".fmt(f),
Self::Test => "test".fmt(f),
Self::Unknown(x) => x.fmt(f),
}
}
}

/// Similar to `kind`, but only reports the
/// [Cargo crate types](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field):
/// `bin`, `lib`, `rlib`, `dylib`, `cdylib`, `staticlib`, `proc-macro`.
Expand Down Expand Up @@ -749,6 +776,29 @@ impl From<&str> for CrateType {
}
}

impl FromStr for CrateType {
type Err = std::convert::Infallible;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(CrateType::from(s))
}
}

impl fmt::Display for CrateType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bin => "bin".fmt(f),
Self::CDyLib => "cdylib".fmt(f),
Self::DyLib => "dylib".fmt(f),
Self::Lib => "lib".fmt(f),
Self::ProcMacro => "proc-macro".fmt(f),
Self::RLib => "rlib".fmt(f),
Self::StaticLib => "staticlib".fmt(f),
Self::Unknown(x) => x.fmt(f),
}
}
}

/// 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
Loading