Skip to content

Commit

Permalink
[clone] Don't expose hex-error in public interfaces anymore
Browse files Browse the repository at this point in the history
That's usually a chose unless we also re-export these crates.
And that we just avoid for now as I have a feeling we can gain
some speed by leveraging the lower-case garantuee offered by git.
And even if not, we can be less abstract than what 'hex' does.
  • Loading branch information
Byron committed Sep 1, 2020
1 parent 4bc7842 commit 92dab30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 14 additions & 2 deletions git-object/src/owned/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use crate::{borrowed, SHA1_SIZE};
use bstr::ByteSlice;
use quick_error::quick_error;
use std::{fmt, io, ops::Deref};

quick_error! {
#[derive(Debug)]
pub enum Error {
HexDecode(err: String) {
display("Failed to hex hash: {}", err)
}
}
}

/// An owned SHA1 identifying objects
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -30,9 +40,11 @@ impl Id {

/// Sha1 hash specific methods
impl Id {
pub fn from_40_bytes_in_hex(buf: &[u8]) -> Result<Id, hex::FromHexError> {
pub fn from_40_bytes_in_hex(buf: &[u8]) -> Result<Id, Error> {
use hex::FromHex;
Ok(Id(<[u8; 20]>::from_hex(buf)?))
Ok(Id(
<[u8; 20]>::from_hex(buf).map_err(|err| Error::HexDecode(err.to_string()))?
))
}
pub fn sha1(&self) -> &[u8; SHA1_SIZE] {
&self.0
Expand Down
8 changes: 3 additions & 5 deletions git-packetline/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use quick_error::quick_error;
quick_error! {
#[derive(Debug)]
pub enum Error {
HexDecode(err: hex::FromHexError) {
display("Failed to decode the first four hex bytes indicating the line length")
from()
source(err)
HexDecode(err: String) {
display("Failed to decode the first four hex bytes indicating the line length: {}", err)
}
DataLengthLimitExceeded(length_in_bytes: usize) {
display("The data received claims to be larger than than the maximum allowed size: got {}, exceeds {}", length_in_bytes, MAX_DATA_LEN)
Expand Down Expand Up @@ -60,7 +58,7 @@ pub fn hex_prefix(four_bytes: &[u8]) -> Result<PacketLineOrWantedSize, Error> {
}

let mut buf = [0u8; U16_HEX_BYTES / 2];
hex::decode_to_slice(four_bytes, &mut buf)?;
hex::decode_to_slice(four_bytes, &mut buf).map_err(|err| Error::HexDecode(err.to_string()))?;
let wanted_bytes = u16::from_be_bytes(buf);
if wanted_bytes == 3 {
return Err(Error::InvalidLineLength);
Expand Down

0 comments on commit 92dab30

Please sign in to comment.