use crate::checksum::Checksum;
use crate::loosepath::loose_path;
use crate::mode::RepoMode;
use crate::objtype::ObjectType;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ObjectName {
pub checksum: Checksum,
pub ty: ObjectType,
}
impl ObjectName {
pub fn new(checksum: Checksum, ty: ObjectType) -> ObjectName {
ObjectName { checksum, ty }
}
pub fn loose_path(&self, mode: RepoMode) -> String {
loose_path(&self.checksum, self.ty, mode)
}
}
impl std::fmt::Display for ObjectName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}", self.checksum.to_hex(), self.ty.type_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn string_form_uses_the_mode_independent_type() {
let c =
Checksum::from_hex("b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89")
.unwrap();
assert_eq!(
ObjectName::new(c, ObjectType::File).to_string(),
"b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89.file"
);
assert_eq!(
ObjectName::new(c, ObjectType::DirTree).to_string(),
"b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89.dirtree"
);
assert_eq!(
ObjectName::new(c, ObjectType::File).loose_path(RepoMode::Archive),
"b3/c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89.filez"
);
}
}