Skip to content

Commit

Permalink
Don't panic from invalid version specifiers (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer authored Aug 10, 2023
1 parent f76536a commit 00156bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
1 change: 0 additions & 1 deletion crates/huak_ops/src/ops/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ mod tests {
package::Package,
test_resources_dir_path, Verbosity,
};
use std::str::FromStr;
use tempfile::tempdir;

#[test]
Expand Down
90 changes: 43 additions & 47 deletions crates/huak_ops/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,58 +41,20 @@ impl Package {
pub fn metadata(&self) -> &Metadata {
&self.metadata
}
}

impl Display for Package {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}=={}", self.name(), self.version())
}
}

/// A wrapper for implementing iterables on `Package`s.
struct PackageIter<'a> {
iter: std::slice::Iter<'a, Package>,
}

impl<'a> Iterator for PackageIter<'a> {
type Item = &'a Package;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}

impl From<Metadata> for Package {
fn from(value: Metadata) -> Self {
Package {
id: PackageId {
name: value.project_name().to_string(),
version: value
.project_version()
.unwrap_or(&Version::from_str("0.0.1").unwrap())
.clone(),
},
metadata: value,
}
}
}

/// Initialize a `Package` from a `&str`.
///
/// ```
/// use huak_ops::Package;
///
/// let package = Package::from_str("my-package == 0.0.1").unwrap();
/// ```
impl FromStr for Package {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
/// Initialize a `Package` from a `&str`.
///
/// ```
/// use huak_ops::Package;
///
/// let package = Package::from_str("my-package == 0.0.1").unwrap();
/// ```
pub fn from_str(s: &str) -> HuakResult<Package> {
// A naive approach to parsing the name and `VersionSpecifiers` from the `&str`.
// Find the first character of the `VersionSpecifiers`. Everything prior is considered
// the name.
let spec_str = parse_version_specifiers_str(s)
.expect("package version specifier(s)");
.ok_or(Error::InvalidVersionString(s.to_string()))?;
let name = s.strip_suffix(spec_str).unwrap_or(s).to_string();
let version_specifiers = VersionSpecifiers::from_str(spec_str)?;

Expand Down Expand Up @@ -127,6 +89,40 @@ impl FromStr for Package {
}
}

impl Display for Package {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}=={}", self.name(), self.version())
}
}

/// A wrapper for implementing iterables on `Package`s.
struct PackageIter<'a> {
iter: std::slice::Iter<'a, Package>,
}

impl<'a> Iterator for PackageIter<'a> {
type Item = &'a Package;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}

impl From<Metadata> for Package {
fn from(value: Metadata) -> Self {
Package {
id: PackageId {
name: value.project_name().to_string(),
version: value
.project_version()
.unwrap_or(&Version::from_str("0.0.1").unwrap())
.clone(),
},
metadata: value,
}
}
}

/// Two `Package`s are currently considered partially equal if their names are the same.
/// NOTE: This may change in the future.
impl PartialEq for Package {
Expand Down

0 comments on commit 00156bd

Please sign in to comment.