Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from TryFrom to FromStr for Manifest #107

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::{self, Display},
str::FromStr,
};
use tokio::fs;

Expand Down Expand Up @@ -54,11 +55,11 @@ impl From<Manifest> for RawManifest {
}
}

impl TryFrom<String> for RawManifest {
type Error = toml::de::Error;
impl FromStr for RawManifest {
type Err = toml::de::Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
toml::from_str::<RawManifest>(&value)
fn from_str(input: &str) -> Result<Self, Self::Err> {
toml::from_str(input)
}
}

Expand Down Expand Up @@ -89,7 +90,7 @@ impl Manifest {
.await
.wrap_err("Failed to read manifest")?;

let raw: RawManifest = toml::from_str(&toml).wrap_err("Failed to parse manifest")?;
let raw: RawManifest = toml.parse().wrap_err("Failed to parse manifest")?;

Ok(raw.into())
}
Expand Down Expand Up @@ -122,11 +123,11 @@ impl From<RawManifest> for Manifest {
}
}

impl TryFrom<String> for Manifest {
type Error = toml::de::Error;
impl FromStr for Manifest {
type Err = toml::de::Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(RawManifest::try_from(value)?.into())
fn from_str(input: &str) -> Result<Self, Self::Err> {
input.parse::<RawManifest>().map(Self::from)
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ impl PackageStore {
"Failed to locate local manifest for package: {package}"
))?;

Manifest::try_from(manifest)
manifest
.parse()
.wrap_err(format!("Malformed manifest of package {package}"))
.map(Manifest::from)
}

/// Packages a release from the local file system state
Expand Down Expand Up @@ -334,7 +334,8 @@ impl TryFrom<Bytes> for Package {
.wrap_err("Failed to find manifest in package")?;

let manifest = manifest.bytes().collect::<io::Result<Vec<_>>>()?;
let manifest = Manifest::try_from(String::from_utf8(manifest)?)
let manifest = String::from_utf8(manifest)?
.parse()
.wrap_err("Failed to parse manifest")?;

Ok(Self { manifest, tgz })
Expand Down
5 changes: 2 additions & 3 deletions src/registry/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ mod tests {
use crate::manifest::{Dependency, Manifest, PackageManifest};
use crate::package::{Package, PackageType};
use crate::registry::local::LocalRegistry;
use crate::registry::RegistryUri;
use bytes::Bytes;
use std::path::PathBuf;
use std::str::FromStr;
use std::{env, fs};

#[tokio::test]
Expand Down Expand Up @@ -145,7 +143,8 @@ mod tests {
package_bytes
);

let registry_uri = RegistryUri::from_str("http://some-registry/artifactory")
let registry_uri = "http://some-registry/artifactory"
.parse()
.expect("Failed to parse registry URL");

// Download package from local registry and assert the tgz bytes and the metadata match what we
Expand Down