oid: Make `Oid::SHA1_LEN` public

The length of a SHA-1 object identifier is defined in various locations.
Instead of this redundancy, make the definition in `radicle-oid` public, and
use it in `radicle{-{core,protocol},}`.
This commit is contained in:
Lorenz Leutgeb 2026-04-23 17:43:24 +02:00 committed by Lorenz Leutgeb
parent f46624b81c
commit 3d1b37fcbb
3 changed files with 13 additions and 19 deletions

View File

@ -14,8 +14,8 @@ pub const RAD_PREFIX: &str = "rad:";
pub enum IdError { pub enum IdError {
#[error(transparent)] #[error(transparent)]
Multibase(#[from] multibase::Error), Multibase(#[from] multibase::Error),
#[error("invalid length: expected {expected} bytes, got {actual} bytes")] #[error("invalid length: expected {} bytes, got {actual} bytes", Oid::LEN_SHA1)]
Length { expected: usize, actual: usize }, Length { actual: usize },
#[error(fmt = fmt_mismatched_base_encoding)] #[error(fmt = fmt_mismatched_base_encoding)]
MismatchedBaseEncoding { MismatchedBaseEncoding {
input: String, input: String,
@ -106,12 +106,10 @@ impl RepoId {
/// ///
/// [multibase]: https://github.com/multiformats/multibase?tab=readme-ov-file#multibase-table /// [multibase]: https://github.com/multiformats/multibase?tab=readme-ov-file#multibase-table
pub fn from_canonical(input: &str) -> Result<Self, IdError> { pub fn from_canonical(input: &str) -> Result<Self, IdError> {
const EXPECTED_LEN: usize = 20;
let (base, bytes) = multibase::decode(input)?; let (base, bytes) = multibase::decode(input)?;
Self::guard_base_encoding(input, base)?; 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 { bytes.try_into().map_err(|bytes: Vec<u8>| IdError::Length {
expected: EXPECTED_LEN,
actual: bytes.len(), actual: bytes.len(),
})?; })?;
Ok(Self(Oid::from_sha1(bytes))) Ok(Self(Oid::from_sha1(bytes)))

View File

@ -86,7 +86,7 @@ pub enum Oid {
// length becomes popular? // length becomes popular?
impl Oid { impl Oid {
/// The length of a SHA-1 object identifier in bytes. /// 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. /// 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 /// This is sometimes used as a sentinel value to indicate the absence of

View File

@ -7,7 +7,6 @@ pub use message::{AddressType, MessageType};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt::Debug; use std::fmt::Debug;
use std::mem;
use std::ops::Deref; use std::ops::Deref;
use std::str::FromStr; use std::str::FromStr;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
@ -45,8 +44,11 @@ pub type Size = u16;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Invalid { pub enum Invalid {
#[error("invalid Git object identifier size: expected {expected}, got {actual}")] #[error(
Oid { expected: usize, actual: usize }, "invalid Git object identifier size: expected {}, got {actual}",
git::Oid::LEN_SHA1
)]
Oid { actual: usize },
#[error(transparent)] #[error(transparent)]
Bounded(#[from] crate::bounded::Error), Bounded(#[from] crate::bounded::Error),
#[error("invalid filter size: {actual}")] #[error("invalid filter size: {actual}")]
@ -382,19 +384,13 @@ where
impl Decode for git::Oid { impl Decode for git::Oid {
fn decode(buf: &mut impl Buf) -> Result<Self, Error> { 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; let len = Size::decode(buf)? as usize;
if len != LEN_EXPECTED { if len != git::Oid::LEN_SHA1 {
return Err(Invalid::Oid { return Err(Invalid::Oid { actual: len }.into());
expected: LEN_EXPECTED,
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 = raw::Oid::from_bytes(&buf).expect("the buffer is exactly the right size");
let oid = git::Oid::from(oid); let oid = git::Oid::from(oid);
@ -633,7 +629,7 @@ mod tests {
} }
#[quickcheck] #[quickcheck]
fn prop_oid(input: [u8; 20]) { fn prop_oid(input: [u8; git::Oid::SHA1_LEN]) {
roundtrip(git::Oid::from_sha1(input)); roundtrip(git::Oid::from_sha1(input));
} }