Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
oid: Make `Oid::SHA1_LEN` public
Lorenz Leutgeb committed 2 days ago
commit 3d1b37fcbb485a58e2f3a8ec98337362fdc8c543
parent f46624b
3 files changed +13 -19
modified crates/radicle-core/src/repo.rs
@@ -14,8 +14,8 @@ pub const RAD_PREFIX: &str = "rad:";
pub enum IdError {
    #[error(transparent)]
    Multibase(#[from] multibase::Error),
-
    #[error("invalid length: expected {expected} bytes, got {actual} bytes")]
-
    Length { expected: usize, actual: usize },
+
    #[error("invalid length: expected {} bytes, got {actual} bytes", Oid::LEN_SHA1)]
+
    Length { actual: usize },
    #[error(fmt = fmt_mismatched_base_encoding)]
    MismatchedBaseEncoding {
        input: String,
@@ -106,12 +106,10 @@ impl RepoId {
    ///
    /// [multibase]: https://github.com/multiformats/multibase?tab=readme-ov-file#multibase-table
    pub fn from_canonical(input: &str) -> Result<Self, IdError> {
-
        const EXPECTED_LEN: usize = 20;
        let (base, bytes) = multibase::decode(input)?;
        Self::guard_base_encoding(input, base)?;
-
        let bytes: [u8; EXPECTED_LEN] =
+
        let bytes: [u8; Oid::LEN_SHA1] =
            bytes.try_into().map_err(|bytes: Vec<u8>| IdError::Length {
-
                expected: EXPECTED_LEN,
                actual: bytes.len(),
            })?;
        Ok(Self(Oid::from_sha1(bytes)))
modified crates/radicle-oid/src/lib.rs
@@ -86,7 +86,7 @@ pub enum Oid {
// length becomes popular?
impl Oid {
    /// The length of a SHA-1 object identifier in bytes.
-
    const SHA1_LEN: usize = 20;
+
    pub const LEN_SHA1: usize = 20;

    /// A SHA-1 object identifier with all digest bytes set to zero.
    /// This is sometimes used as a sentinel value to indicate the absence of
modified crates/radicle-protocol/src/wire.rs
@@ -7,7 +7,6 @@ pub use message::{AddressType, MessageType};

use std::convert::TryFrom;
use std::fmt::Debug;
-
use std::mem;
use std::ops::Deref;
use std::str::FromStr;
use std::string::FromUtf8Error;
@@ -45,8 +44,11 @@ pub type Size = u16;

#[derive(thiserror::Error, Debug)]
pub enum Invalid {
-
    #[error("invalid Git object identifier size: expected {expected}, got {actual}")]
-
    Oid { expected: usize, actual: usize },
+
    #[error(
+
        "invalid Git object identifier size: expected {}, got {actual}",
+
        git::Oid::LEN_SHA1
+
    )]
+
    Oid { actual: usize },
    #[error(transparent)]
    Bounded(#[from] crate::bounded::Error),
    #[error("invalid filter size: {actual}")]
@@ -382,19 +384,13 @@ where

impl Decode for git::Oid {
    fn decode(buf: &mut impl Buf) -> Result<Self, Error> {
-
        const LEN_EXPECTED: usize = mem::size_of::<raw::Oid>();
-

        let len = Size::decode(buf)? as usize;

-
        if len != LEN_EXPECTED {
-
            return Err(Invalid::Oid {
-
                expected: LEN_EXPECTED,
-
                actual: len,
-
            }
-
            .into());
+
        if len != git::Oid::LEN_SHA1 {
+
            return Err(Invalid::Oid { actual: len }.into());
        }

-
        let buf: [u8; LEN_EXPECTED] = Decode::decode(buf)?;
+
        let buf: [u8; git::Oid::SHA1_LEN] = Decode::decode(buf)?;
        let oid = raw::Oid::from_bytes(&buf).expect("the buffer is exactly the right size");
        let oid = git::Oid::from(oid);

@@ -633,7 +629,7 @@ mod tests {
    }

    #[quickcheck]
-
    fn prop_oid(input: [u8; 20]) {
+
    fn prop_oid(input: [u8; git::Oid::SHA1_LEN]) {
        roundtrip(git::Oid::from_sha1(input));
    }