Skip to content
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
17 changes: 15 additions & 2 deletions src/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Fingerprint(pub(crate) Hash);

impl Fingerprint {
Expand All @@ -19,9 +19,22 @@ impl Display for Fingerprint {
}

impl FromStr for Fingerprint {
type Err = blake3::HexError;
type Err = HashError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.parse()?))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn uppercase_is_forbidden() {
test::HASH
.to_uppercase()
.parse::<Fingerprint>()
.unwrap_err();
}
}
15 changes: 13 additions & 2 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ impl From<[u8; Hash::LEN]> for Hash {
}

impl FromStr for Hash {
type Err = blake3::HexError;
type Err = HashError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.parse()?))
let hash = s.parse()?;

if !is_lowercase_hex(s) {
return Err(hash_error::Case { hash: s }.build());
}

Ok(Self(hash))
}
}

Expand Down Expand Up @@ -103,4 +109,9 @@ mod tests {
);
assert_eq!(serde_json::from_str::<Hash>(&json).unwrap(), input);
}

#[test]
fn uppercase_is_forbidden() {
test::HASH.to_uppercase().parse::<Hash>().unwrap_err();
}
}
10 changes: 10 additions & 0 deletions src/hash_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::*;

#[derive(Debug, Snafu)]
#[snafu(context(suffix(false)), visibility(pub(crate)))]
pub enum HashError {
#[snafu(display("hashes must be lowercase hex: `{hash}`"))]
Case { hash: String },
#[snafu(transparent)]
Hex { source: blake3::HexError },
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use {
fingerprint_prefix::FingerprintPrefix,
format::Format,
functions::{current_dir, decode_path, default, is_default, is_lowercase_hex, now},
hash_error::HashError,
index::Index,
key_identifier::KeyIdentifier,
key_name::KeyName,
Expand Down Expand Up @@ -142,6 +143,7 @@ mod fingerprint_prefix;
mod format;
mod functions;
mod hash;
mod hash_error;
mod index;
mod key_identifier;
mod key_name;
Expand Down