Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
oid: New crate
Lorenz Leutgeb committed 7 months ago
commit 1d6a1c077cc918d533397300ecbcc579936a4cd1
parent 2c09fdc8cd0a064aa4c0848c0b4506bee76e3332
5 files changed +492 -3
modified Cargo.lock
@@ -3000,6 +3000,19 @@ dependencies = [
]

[[package]]
+
name = "radicle-oid"
+
version = "0.1.0"
+
dependencies = [
+
 "git2",
+
 "gix-hash",
+
 "qcheck",
+
 "radicle-git-ext",
+
 "radicle-git-ref-format",
+
 "schemars",
+
 "serde",
+
]
+

+
[[package]]
name = "radicle-protocol"
version = "0.3.0"
dependencies = [
modified Cargo.toml
@@ -28,6 +28,7 @@ crossbeam-channel = "0.5.6"
cyphernet = "0.5.2"
dunce = "1.0.5"
fastrand = { version = "2.0.0", default-features = false }
+
git-ref-format = { version = "0.3.0", default-features = false }
git2 = { version = "0.19.0", default-features = false }
human-panic = "2"
itertools = "0.14"
@@ -50,13 +51,14 @@ radicle-fetch = { version = "0.15", path = "crates/radicle-fetch" }
radicle-git-ext = { version = "0.8", default-features = false }
radicle-git-ref-format = { version = "0.1.0", path = "crates/radicle-git-ref-format", default-features = false }
radicle-node = { version = "0.15", path = "crates/radicle-node" }
+
radicle-oid = { version = "0.1.0", path = "crates/radicle-oid", default-features = false }
radicle-protocol = { version = "0.3", path = "crates/radicle-protocol" }
radicle-signals = { version = "0.11", path = "crates/radicle-signals" }
radicle-ssh = { version = "0.10", path = "crates/radicle-ssh", default-features = false }
radicle-systemd = { version = "0.10", path = "crates/radicle-systemd" }
radicle-term = { version = "0.15", path = "crates/radicle-term" }
-
schemars = { version = "1.0.4" }
-
serde = "1.0"
+
schemars = { version = "1.0.4", default-features = false }
+
serde = { version = "1.0", default-features = false }
serde_json = "1.0"
shlex = "1.1.0"
signature = "2.2"
added crates/radicle-oid/Cargo.toml
@@ -0,0 +1,26 @@
+
[package]
+
name = "radicle-oid"
+
description = "Radicle representation of Git object identifiers"
+
homepage.workspace = true
+
repository.workspace = true
+
version = "0.1.0"
+
edition.workspace = true
+
license.workspace = true
+
keywords = ["radicle", "git", "oid"]
+
rust-version.workspace = true
+

+
# For documentation of features refer to the module documentation in `./lib.rs`
+
[features]
+
default = ["std"]
+
gix = ["dep:gix-hash"]
+
std = []
+
radicle-git-ext = ["git2", "dep:radicle-git-ext"]
+

+
[dependencies]
+
git2 = { workspace = true, optional = true, default-features = false }
+
gix-hash = { version = "0.15.1", optional = true, default-features = false }
+
qcheck = { workspace = true, optional = true, default-features = false }
+
radicle-git-ext = { workspace = true, optional = true, default-features = false }
+
radicle-git-ref-format = { workspace = true, optional = true, default-features = false }
+
schemars = { workspace = true, optional = true, default-features = false }
+
serde = { workspace = true, optional = true, default-features = false }

\ No newline at end of file
added crates/radicle-oid/src/lib.rs
@@ -0,0 +1,448 @@
+
#![no_std]
+

+
//! This is a `no_std` crate carries the struct [`Oid`].
+
//!
+
//! ## Background
+
//!
+
//! [`git2`]: ::git2
+
//!
+
//! Free from [`git2`].
+
//!
+
//! IDs are ubiquitious, have a struct under our control.
+
//!
+
//! ## Feature Flags
+
//!
+
//! ### `std`
+
//!
+
//! [`Hash`]: ::doc_std::hash::Hash
+
//!
+
//! Enabled by default, since it is expected that most dependents will use the
+
//! standard library.
+
//!
+
//! Implementation of [`Hash`].
+
//!
+
//! ### `git2`
+
//!
+
//! [`git2::Oid`]: ::git2::Oid
+
//!
+
//! Conversions to/from [`git2::Oid`].
+
//!
+
//! ### `gix`
+
//!
+
//! [`ObjectId`]: ::gix_hash::ObjectId
+
//!
+
//! Conversions to/from [`ObjectId`].
+
//!
+
//! ### `schemars`
+
//!
+
//! [`JsonSchema`]: ::schemars::JsonSchema
+
//!
+
//! Implementation of [`JsonSchema`].
+
//!
+
//! ### `serde`
+
//!
+
//! [`Serialize`]: ::serde::ser::Serialize
+
//! [`Deserialize`]: ::serde::de::Deserialize
+
//!
+
//! Implementions of [`Serialize`] and [`Deserialize`].
+
//!
+
//! ### `radicle-git-ext`
+
//!
+
//! [`radicle_git_ext::Oid`]: ::radicle_git_ext::Oid
+
//!
+
//! Deprecated conversion to/from [`radicle_git_ext::Oid`].
+
//!
+
//! ### `qcheck`
+
//!
+
//! [`qcheck::Arbitrary`]: ::qcheck::Arbitrary
+
//!
+
//! Implementation of [`qcheck::Arbitrary`].
+
//!
+
//! ### `git-ref-format`
+
//!
+
//! [`git_ref_format::Component`]: ::git_ref_format::Component
+
//! [`git_ref_format::RefString`]: ::git_ref_format::RefString
+
//!
+
//! Conversion to [`git_ref_format::Component`] (and also [`git_ref_format::RefString`]).
+
//! This only exists as a convenience for use in other Radicle crates.
+

+
#[cfg(doc)]
+
extern crate std as doc_std;
+

+
extern crate alloc;
+

+
use alloc::boxed::Box;
+

+
const LEN: usize = 20;
+

+
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Copy, Default)]
+
pub struct Oid([u8; 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 {
+
        Self(hash)
+
    }
+

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

+
/// Views.
+
impl Oid {
+
    pub fn as_sha1(&self) -> Option<&[u8; LEN]> {
+
        Some(&self.0)
+
    }
+
}
+

+
/// Interaction with zero.
+
impl Oid {
+
    /// Test whether all bytes in this object identifier are zero.
+
    /// See also [`::git2::Oid::is_zero`].
+
    pub fn is_zero(&self) -> bool {
+
        self.0.iter().all(|b| *b == 0)
+
    }
+
}
+

+
impl AsRef<[u8]> for Oid {
+
    fn as_ref(&self) -> &[u8] {
+
        &self.0
+
    }
+
}
+

+
impl From<Oid> for Box<[u8]> {
+
    fn from(val: Oid) -> Self {
+
        Box::new(val.0)
+
    }
+
}
+

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

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

+
    impl str::FromStr for Oid {
+
        type Err = error::ParseOidError;
+

+
        fn from_str(s: &str) -> Result<Self, Self::Err> {
+
            use error::ParseOidError::*;
+

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

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

+
            Ok(Self(bytes))
+
        }
+
    }
+

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

+
        pub enum ParseOidError {
+
            Len(usize),
+
            At {
+
                index: usize,
+
                source: num::ParseIntError,
+
            },
+
        }
+

+
        impl fmt::Display for ParseOidError {
+
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+
                use ParseOidError::*;
+
                match self {
+
                    Len(len) => write!(f, "invalid length (have {len}, want {LEN_STR})"),
+
                    At { index, source } => write!(
+
                        f,
+
                        "parse error at byte {index} (characters {} and {}): {source}",
+
                        index * 2,
+
                        index * 2 + 1
+
                    ),
+
                }
+
            }
+
        }
+

+
        impl fmt::Debug for ParseOidError {
+
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+
                fmt::Display::fmt(self, f)
+
            }
+
        }
+

+
        impl core::error::Error for ParseOidError {
+
            fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
+
                match self {
+
                    ParseOidError::At { source, .. } => Some(source),
+
                    _ => None,
+
                }
+
            }
+
        }
+
    }
+

+
    pub use error::ParseOidError;
+
}
+

+
mod fmt {
+
    use alloc::format;
+

+
    use super::Oid;
+
    use core::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.
+
            // The indices below are manually verified to not be out of bounds.
+
            unsafe {
+
                format!(
+
                    "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
+
                    self.0.get_unchecked(0),
+
                    self.0.get_unchecked(1),
+
                    self.0.get_unchecked(2),
+
                    self.0.get_unchecked(3),
+
                    self.0.get_unchecked(4),
+
                    self.0.get_unchecked(5),
+
                    self.0.get_unchecked(6),
+
                    self.0.get_unchecked(7),
+
                    self.0.get_unchecked(8),
+
                    self.0.get_unchecked(9),
+
                    self.0.get_unchecked(10),
+
                    self.0.get_unchecked(11),
+
                    self.0.get_unchecked(12),
+
                    self.0.get_unchecked(13),
+
                    self.0.get_unchecked(14),
+
                    self.0.get_unchecked(15),
+
                    self.0.get_unchecked(16),
+
                    self.0.get_unchecked(17),
+
                    self.0.get_unchecked(18),
+
                    self.0.get_unchecked(19),
+
                ).fmt(f)
+
            }
+
        }
+
    }
+

+
    impl fmt::Debug for Oid {
+
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+
            fmt::Display::fmt(self, f)
+
        }
+
    }
+
}
+

+
#[cfg(feature = "std")]
+
mod std {
+
    extern crate std;
+
    use super::Oid;
+

+
    mod hash {
+
        use super::*;
+
        use std::hash;
+

+
        #[allow(clippy::derived_hash_with_manual_eq)]
+
        impl hash::Hash for Oid {
+
            fn hash<H: hash::Hasher>(&self, state: &mut H) {
+
                state.write(&self.0);
+
            }
+
        }
+
    }
+
}
+

+
#[cfg(feature = "gix")]
+
mod gix {
+
    use super::Oid;
+
    use gix_hash::ObjectId as Other;
+

+
    impl From<Other> for Oid {
+
        fn from(other: Other) -> Self {
+
            match other {
+
                Other::Sha1(array) => Self(array),
+
            }
+
        }
+
    }
+

+
    impl From<Oid> for Other {
+
        fn from(oid: Oid) -> Other {
+
            Other::Sha1(oid.0)
+
        }
+
    }
+
}
+

+
#[cfg(feature = "git2")]
+
mod git2 {
+
    use super::*;
+
    use ::git2::Oid as Other;
+

+
    const EXPECT: &str = "git2::Oid and radicle_oid::Oid are both exactly 20 bytes long";
+

+
    impl From<Other> for Oid {
+
        fn from(other: Other) -> Self {
+
            let slice = other.as_bytes();
+
            let vec = {
+
                extern crate alloc;
+
                alloc::borrow::ToOwned::to_owned(slice)
+
            };
+
            Self(vec.try_into().expect(EXPECT))
+
        }
+
    }
+

+
    impl From<Oid> for Other {
+
        fn from(oid: Oid) -> Self {
+
            Other::from(&oid)
+
        }
+
    }
+

+
    impl From<&Oid> for Other {
+
        fn from(oid: &Oid) -> Self {
+
            Other::from_bytes(&oid.0).expect(EXPECT)
+
        }
+
    }
+

+
    impl core::cmp::PartialEq<Other> for Oid {
+
        fn eq(&self, other: &Other) -> bool {
+
            other.as_bytes() == self.as_ref()
+
        }
+
    }
+
}
+

+
/// Deprecated!
+
#[cfg(feature = "radicle-git-ext")]
+
mod radicle_git_ext {
+
    use super::Oid;
+
    use ::radicle_git_ext::Oid as Other;
+

+
    impl From<Other> for Oid {
+
        fn from(other: Other) -> Self {
+
            Oid::from(::git2::Oid::from(other))
+
        }
+
    }
+

+
    impl From<Oid> for Other {
+
        fn from(oid: Oid) -> Other {
+
            Other::from(<::git2::Oid>::from(oid))
+
        }
+
    }
+
}
+

+
#[cfg(feature = "qcheck")]
+
mod qcheck {
+
    use super::*;
+
    use ::qcheck::{Arbitrary, Gen};
+

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

+
#[cfg(feature = "serde")]
+
mod serde {
+
    mod ser {
+
        use crate::*;
+
        use ::serde::ser;
+

+
        impl ser::Serialize for Oid {
+
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+
            where
+
                S: ser::Serializer,
+
            {
+
                serializer.collect_str(self)
+
            }
+
        }
+
    }
+

+
    mod de {
+
        use crate::*;
+
        use ::serde::de;
+
        use core::fmt;
+

+
        impl<'de> de::Deserialize<'de> for Oid {
+
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+
            where
+
                D: de::Deserializer<'de>,
+
            {
+
                struct OidVisitor;
+

+
                impl<'de> de::Visitor<'de> for OidVisitor {
+
                    type Value = Oid;
+

+
                    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
+
                        write!(f, "a Git object identifier (SHA-1 hash in hexadeximal notation; 40 characters; 20 bytes)")
+
                    }
+

+
                    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+
                    where
+
                        E: de::Error,
+
                    {
+
                        s.parse().map_err(de::Error::custom)
+
                    }
+
                }
+

+
                deserializer.deserialize_str(OidVisitor)
+
            }
+
        }
+
    }
+
}
+

+
#[cfg(feature = "radicle-git-ref-format")]
+
mod radicle_git_ref_format {
+
    use super::*;
+
    use ::radicle_git_ref_format::{Component, RefString};
+

+
    impl From<&Oid> for Component<'_> {
+
        fn from(id: &Oid) -> Self {
+
            Component::from_refstr(RefString::from(id))
+
                .expect("Git object identifiers are valid component strings")
+
        }
+
    }
+

+
    impl From<&Oid> for RefString {
+
        fn from(id: &Oid) -> Self {
+
            RefString::try_from({
+
                extern crate alloc;
+
                alloc::format!("{id}")
+
            })
+
            .expect("Git object identifiers are valid reference strings")
+
        }
+
    }
+
}
+

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

+
    impl JsonSchema for Oid {
+
        fn schema_name() -> Cow<'static, str> {
+
            "Oid".into()
+
        }
+

+
        fn schema_id() -> Cow<'static, str> {
+
            concat!(module_path!(), "::Oid").into()
+
        }
+

+
        fn json_schema(_: &mut SchemaGenerator) -> Schema {
+
            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}}}$"),
+
            })
+
        }
+
    }
+
}
modified crates/radicle/Cargo.toml
@@ -36,7 +36,7 @@ radicle-cob = { workspace = true }
radicle-crypto = { workspace = true, features = ["radicle-git-ext", "ssh", "sqlite", "cyphernet"] }
radicle-git-ext = { workspace = true, features = ["serde"] }
radicle-ssh = { workspace = true }
-
schemars = { workspace = true, optional = true }
+
schemars = { workspace = true, optional = true, features = ["derive", "std"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
serde-untagged = "0.1.7"