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 pub fn into_file(self) -> Option<File> {
86 match self {
87 Entry::File(file) => Some(file),
88 Entry::Directory(_) => None,
89 Entry::DirectoryEnd(_) => None,
90 }
91 }
92
93 pub fn into_directory(self) -> Option<Directory> {
94 match self {
95 Entry::File(_) => None,
96 Entry::Directory(directory) => Some(directory),
97 Entry::DirectoryEnd(_) => None,
98 }
99 }
100}
101
102impl From<v1::File> for File {
103 fn from(value: v1::File) -> Self {
104 File::V1(value)
105 }
106}
107
108impl From<v5::File> for File {
109 fn from(value: v5::File) -> Self {
110 File::V5(value)
111 }
112}
113
114impl From<v1::Directory> for Directory {
115 fn from(value: v1::Directory) -> Self {
116 Directory::V1(value)
117 }
118}
119
120impl From<v5::Directory> for Directory {
121 fn from(value: v5::Directory) -> Self {
122 Directory::V5(value)
123 }
124}