Skip to main content

sit/
entry.rs

1use macintosh_utils::Fork;
2
3use crate::structs::{Directory, File, v1, v5};
4
5#[derive(Debug, Clone)]
6pub enum Entry {
7    File(File),
8    Directory(Directory),
9    DirectoryEnd(u64),
10}
11
12impl Entry {
13    pub fn name(&self) -> &str {
14        match self {
15            Entry::File(f) => f.name(),
16            Entry::Directory(d) => d.name(),
17            Entry::DirectoryEnd(_) => "",
18        }
19    }
20
21    pub fn is_directory(&self) -> bool {
22        matches!(self, Entry::Directory(_))
23    }
24
25    pub fn is_file(&self) -> bool {
26        matches!(self, Entry::File(_))
27    }
28
29    pub fn uncompressed_size(&self, fork: Fork) -> usize {
30        match self {
31            Entry::File(e) => e.uncompressed_size(fork),
32            Entry::Directory(e) => e.uncompressed_size(fork),
33            Entry::DirectoryEnd(_) => 0,
34        }
35    }
36
37    pub fn checksum(&self, fork: Fork) -> u16 {
38        match self {
39            Entry::File(file) => file.checksum(fork),
40            Entry::Directory(dir) => dir.checksum(fork),
41            Entry::DirectoryEnd(_) => 0,
42        }
43    }
44
45    pub fn has(&self, fork: Fork) -> bool {
46        match self {
47            Entry::File(e) => e.has(fork),
48            Entry::Directory(e) => e.has(fork),
49            Entry::DirectoryEnd(_) => false,
50        }
51    }
52
53    pub fn encrypted(&self, fork: Fork) -> bool {
54        match self {
55            Entry::File(e) => e.encrypted(fork),
56            Entry::Directory(e) => e.encrypted(fork),
57            Entry::DirectoryEnd(_) => false,
58        }
59    }
60
61    pub fn comment(&self) -> &str {
62        match self {
63            Entry::File(file) => file.comment(),
64            Entry::Directory(dir) => dir.comment(),
65            Entry::DirectoryEnd(_) => "",
66        }
67    }
68
69    pub fn as_file(&self) -> Option<&File> {
70        match self {
71            Entry::File(file) => Some(file),
72            Entry::Directory(_) => None,
73            Entry::DirectoryEnd(_) => None,
74        }
75    }
76
77    pub fn as_directory(&self) -> Option<&Directory> {
78        match self {
79            Entry::File(_) => None,
80            Entry::Directory(directory) => Some(directory),
81            Entry::DirectoryEnd(_) => None,
82        }
83    }
84}
85
86impl From<v1::File> for File {
87    fn from(value: v1::File) -> Self {
88        File::V1(value)
89    }
90}
91
92impl From<v5::File> for File {
93    fn from(value: v5::File) -> Self {
94        File::V5(value)
95    }
96}
97
98impl From<v1::Directory> for Directory {
99    fn from(value: v1::Directory) -> Self {
100        Directory::V1(value)
101    }
102}
103
104impl From<v5::Directory> for Directory {
105    fn from(value: v5::Directory) -> Self {
106        Directory::V5(value)
107    }
108}