Skip to content

Commit

Permalink
Merge pull request #107 from helsing-ai/pe/manifest
Browse files Browse the repository at this point in the history
Switch from TryFrom to FromStr for Manifest
  • Loading branch information
mara-schulke authored Oct 10, 2023
2 parents 7467dbc + 49f5d32 commit acd5f27
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
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

0 comments on commit acd5f27

Please sign in to comment.