mirror of
https://github.com/deps-rs/deps.rs.git
synced 2024-11-24 11:06:31 +00:00
29 lines
614 B
Rust
29 lines
614 B
Rust
|
use std::str::FromStr;
|
||
|
|
||
|
pub struct CrateName(String);
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct CrateNameValidationError;
|
||
|
|
||
|
impl AsRef<str> for CrateName {
|
||
|
fn as_ref(&self) -> &str {
|
||
|
self.0.as_ref()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl FromStr for CrateName {
|
||
|
type Err = CrateNameValidationError;
|
||
|
|
||
|
fn from_str(input: &str) -> Result<CrateName, CrateNameValidationError> {
|
||
|
let is_valid = input.chars().all(|c| {
|
||
|
c.is_ascii_alphanumeric() || c == '_' || c == '-'
|
||
|
});
|
||
|
|
||
|
if !is_valid {
|
||
|
Err(CrateNameValidationError)
|
||
|
} else {
|
||
|
Ok(CrateName(input.to_string()))
|
||
|
}
|
||
|
}
|
||
|
}
|