Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: rename LEN to SHA1_LEN
Fintan Halpenny committed 7 months ago
commit 212942145cafbdabadfb85dd3feb616b463efd40
parent 514fa55da9dadbab73b5d654f25f7b94d85add3b
1 file changed +18 -18
modified crates/radicle-oid/src/lib.rs
@@ -72,29 +72,29 @@ extern crate alloc;

use alloc::boxed::Box;

-
const LEN: usize = 20;
+
const SHA1_LEN: usize = 20;

#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Copy, Default)]
-
pub struct Oid([u8; LEN]);
+
pub struct Oid([u8; SHA1_LEN]);

/// Conversions to/from SHA-1.
// Note that we deliberately do not implement `From<[u8; 20]>` and `Into<[u8; 20]>`,
// for forwards compatibility: What if another hash of the same
// length becomes popular?
impl Oid {
-
    pub fn from_sha1(hash: [u8; LEN]) -> Self {
+
    pub fn from_sha1(hash: [u8; SHA1_LEN]) -> Self {
        Self(hash)
    }

    pub fn try_from_sha1(hash: &[u8]) -> Result<Self, core::array::TryFromSliceError> {
-
        let array: [u8; LEN] = <[u8; LEN]>::try_from(hash)?;
+
        let array: [u8; SHA1_LEN] = <[u8; SHA1_LEN]>::try_from(hash)?;
        Ok(Self(array))
    }
}

/// Views.
impl Oid {
-
    pub fn as_sha1(&self) -> Option<&[u8; LEN]> {
+
    pub fn as_sha1(&self) -> Option<&[u8; SHA1_LEN]> {
        Some(&self.0)
    }
}
@@ -121,11 +121,11 @@ impl From<Oid> for Box<[u8]> {
}

pub mod str {
-
    use super::{Oid, LEN};
+
    use super::{Oid, SHA1_LEN};
    use core::str;

    /// Length of the string representation;
-
    pub(super) const LEN_STR: usize = LEN * 2;
+
    pub(super) const SHA1_LEN_STR: usize = SHA1_LEN * 2;

    impl str::FromStr for Oid {
        type Err = error::ParseOidError;
@@ -134,12 +134,12 @@ pub mod str {
            use error::ParseOidError::*;

            let len = s.len();
-
            if len != LEN_STR {
+
            if len != SHA1_LEN_STR {
                return Err(Len(len));
            }

-
            let mut bytes = [0u8; LEN];
-
            for i in 0..LEN {
+
            let mut bytes = [0u8; SHA1_LEN];
+
            for i in 0..SHA1_LEN {
                bytes[i] = u8::from_str_radix(&s[i * 2..=i * 2 + 1], 16)
                    .map_err(|source| At { index: i, source })?;
            }
@@ -149,7 +149,7 @@ pub mod str {
    }

    pub mod error {
-
        use super::LEN_STR;
+
        use super::SHA1_LEN_STR;
        use core::{fmt, num};

        pub enum ParseOidError {
@@ -164,7 +164,7 @@ pub mod str {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                use ParseOidError::*;
                match self {
-
                    Len(len) => write!(f, "invalid length (have {len}, want {LEN_STR})"),
+
                    Len(len) => write!(f, "invalid length (have {len}, want {SHA1_LEN_STR})"),
                    At { index, source } => write!(
                        f,
                        "parse error at byte {index} (characters {} and {}): {source}",
@@ -202,7 +202,7 @@ mod fmt {

    impl fmt::Display for Oid {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-
            // SAFETY: The length of `Oid` is known to be `LEN`, which is 20.
+
            // SAFETY: The length of `Oid` is known to be `SHA1_LEN`, which is 20.
            // The indices below are manually verified to not be out of bounds.
            unsafe {
                format!(
@@ -321,7 +321,7 @@ mod qcheck {

    impl Arbitrary for Oid {
        fn arbitrary(g: &mut Gen) -> Self {
-
            let slice = [0u8; LEN];
+
            let slice = [0u8; SHA1_LEN];
            g.fill(slice);
            Self(slice)
        }
@@ -402,8 +402,8 @@ mod radicle_git_ref_format {

#[cfg(feature = "schemars")]
mod schemars {
-
    use super::{str::LEN_STR, Oid};
    use ::schemars::{json_schema, JsonSchema, Schema, SchemaGenerator};
+
    use super::{str::SHA1_LEN_STR, Oid};
    use alloc::{borrow::Cow, format};

    impl JsonSchema for Oid {
@@ -419,9 +419,9 @@ mod schemars {
            json_schema!({
                "description": "A Git object identifier (SHA-1 hash) in hexadecimal encoding.",
                "type": "string",
-
                "maxLength": LEN_STR,
-
                "minLength": LEN_STR,
-
                "pattern":  format!("^[0-9a-fA-F]{{{LEN_STR}}}$"),
+
                "maxLength": SHA1_LEN_STR,
+
                "minLength": SHA1_LEN_STR,
+
                "pattern":  format!("^[0-9a-fA-F]{{{SHA1_LEN_STR}}}$"),
            })
        }
    }