gitdataai/lib/git/cmd/oid.rs
2026-05-30 01:38:40 +08:00

35 lines
875 B
Rust

use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
pub struct ObjectId(pub String);
impl ObjectId {
pub fn new(hex: impl AsRef<str>) -> Self {
Self(hex.as_ref().to_lowercase())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for ObjectId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for ObjectId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl TryFrom<&ObjectId> for gix::hash::ObjectId {
type Error = crate::errors::GitError;
fn try_from(id: &ObjectId) -> Result<Self, Self::Error> {
gix::hash::ObjectId::from_hex(id.as_str().as_bytes()).map_err(|e| {
crate::errors::GitError::InvalidOid(format!("invalid hex oid: {e}"))
})
}
}