use std::fmt;
use crate::error::StikkError;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObjectId(String);
impl ObjectId {
pub fn parse(value: &str) -> crate::error::Result<Self> {
if value.len() != 64 {
return Err(StikkError::environment_msg(format!(
"object id must be 64 hex characters, got {}",
value.len()
)));
}
if !value
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
{
return Err(StikkError::environment_msg(
"object id must be lowercase hexadecimal".to_string(),
));
}
Ok(Self(value.to_string()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn abbreviated(&self, len: usize) -> &str {
self.0.get(..len).unwrap_or(&self.0)
}
}
impl fmt::Display for ObjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RefName(String);
impl RefName {
pub fn parse(value: &str) -> crate::error::Result<Self> {
if value.is_empty() {
return Err(StikkError::environment_msg("ref name must not be empty"));
}
if value.chars().any(char::is_control) {
return Err(StikkError::environment_msg(
"ref name must not contain control characters",
));
}
Ok(Self(value.to_string()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn is_received(&self) -> bool {
self.0.starts_with("remotes/")
}
}
impl fmt::Display for RefName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests;