mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 13:08:42 +00:00
Impl new type helpes::CrateName
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
3b1b59c097
commit
38c8bc8cf2
2 changed files with 42 additions and 0 deletions
|
@ -34,6 +34,9 @@ pub use path_ext::*;
|
||||||
mod tls_version;
|
mod tls_version;
|
||||||
pub use tls_version::TLSVersion;
|
pub use tls_version::TLSVersion;
|
||||||
|
|
||||||
|
mod crate_name;
|
||||||
|
pub use crate_name::CrateName;
|
||||||
|
|
||||||
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
||||||
pub fn load_manifest_path<P: AsRef<Path>>(
|
pub fn load_manifest_path<P: AsRef<Path>>(
|
||||||
manifest_path: P,
|
manifest_path: P,
|
||||||
|
|
39
src/helpers/crate_name.rs
Normal file
39
src/helpers/crate_name.rs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
use std::convert::Infallible;
|
||||||
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CrateName {
|
||||||
|
pub name: String,
|
||||||
|
pub version: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for CrateName {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.name)?;
|
||||||
|
|
||||||
|
if let Some(version) = &self.version {
|
||||||
|
write!(f, "@{version}")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for CrateName {
|
||||||
|
type Err = Infallible;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Ok(if let Some((name, version)) = s.split_once('@') {
|
||||||
|
CrateName {
|
||||||
|
name: name.to_string(),
|
||||||
|
version: Some(version.to_string()),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
CrateName {
|
||||||
|
name: s.to_string(),
|
||||||
|
version: None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue