Impl new type helpes::CrateName

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-12 16:47:44 +10:00
parent 3b1b59c097
commit 38c8bc8cf2
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 42 additions and 0 deletions

View file

@ -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
View 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,
}
})
}
}