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

digest: Add TryFrom<&str + String> #222

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
18 changes: 17 additions & 1 deletion src/image/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ impl FromStr for Digest {
type Err = crate::OciSpecError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Digest::try_from(s)
}
}

impl TryFrom<String> for Digest {
type Error = crate::OciSpecError;

fn try_from(s: String) -> Result<Self, Self::Error> {
let Some(split) = s.find(':') else {
return Err(crate::OciSpecError::Other("missing ':' in digest".into()));
};
Expand Down Expand Up @@ -201,6 +209,14 @@ impl FromStr for Digest {
}
}

impl TryFrom<&str> for Digest {
type Error = crate::OciSpecError;

fn try_from(string: &str) -> Result<Self, Self::Error> {
TryFrom::try_from(string.to_owned())
}
}

/// A SHA-256 digest, guaranteed to be 64 lowercase hexadecimal ASCII characters.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Sha256Digest {
Expand Down Expand Up @@ -296,7 +312,7 @@ mod tests {
Digest::from_str(case).unwrap();
}

let d = Digest::from_str("multihash+base58:QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8")
let d = Digest::try_from("multihash+base58:QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8")
.unwrap();
assert_eq!(d.algorithm(), &DigestAlgorithm::from("multihash+base58"));
assert_eq!(d.digest(), "QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8");
Expand Down
Loading