Skip to content

Commit

Permalink
Merge pull request #276 from sunshowers/fromstr-display
Browse files Browse the repository at this point in the history
add FromStr and Display impls for TargetKind and CrateType
  • Loading branch information
oli-obk authored Dec 3, 2024
2 parents e6c0f06 + 2983f51 commit 8f4c3a8
Showing 1 changed file with 51 additions and 1 deletion.
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

0 comments on commit 8f4c3a8

Please sign in to comment.