1use crate::checksum::Checksum;
7use crate::loosepath::loose_path;
8use crate::mode::RepoMode;
9use crate::objtype::ObjectType;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19pub struct ObjectName {
20 pub checksum: Checksum,
22 pub ty: ObjectType,
24}
25
26impl ObjectName {
27 pub fn new(checksum: Checksum, ty: ObjectType) -> ObjectName {
29 ObjectName { checksum, ty }
30 }
31
32 pub fn loose_path(&self, mode: RepoMode) -> String {
37 loose_path(&self.checksum, self.ty, mode)
38 }
39}
40
41impl std::fmt::Display for ObjectName {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 write!(f, "{}.{}", self.checksum.to_hex(), self.ty.type_str())
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn string_form_uses_the_mode_independent_type() {
53 let c =
54 Checksum::from_hex("b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89")
55 .unwrap();
56 assert_eq!(
58 ObjectName::new(c, ObjectType::File).to_string(),
59 "b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89.file"
60 );
61 assert_eq!(
62 ObjectName::new(c, ObjectType::DirTree).to_string(),
63 "b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89.dirtree"
64 );
65 assert_eq!(
67 ObjectName::new(c, ObjectType::File).loose_path(RepoMode::Archive),
68 "b3/c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89.filez"
69 );
70 }
71}