-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from NobodyXu/feature/crate-version
Feature: Support specifing versions via `crate_name@req`
- Loading branch information
Showing
4 changed files
with
79 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters