1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
mod msdos;
mod unix;
use std::{
convert::{TryFrom, TryInto},
fmt::Display,
ops::Deref,
};
pub use msdos::FtpEntryMsdos;
pub use unix::FtpEntryUnix;
#[non_exhaustive]
#[derive(Debug)]
pub struct FtpEntryPermissions(String);
impl FtpEntryPermissions {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for FtpEntryPermissions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum FtpEntryKind {
UNKNOWN,
Directory,
File,
BlockDevice,
CharacterDevice,
Pipe,
Socket,
Symlink,
}
impl From<char> for FtpEntryKind {
fn from(value: char) -> Self {
match value {
'-' => Self::File,
'd' => Self::Directory,
'b' => Self::BlockDevice,
'c' => Self::CharacterDevice,
'p' => Self::Pipe,
's' => Self::Socket,
'l' => Self::Symlink,
_ => Self::UNKNOWN,
}
}
}
impl TryFrom<&str> for FtpEntryKind {
type Error = &'static str;
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.len() == 1 {
Ok(value.chars().nth(0).unwrap().into())
} else {
Err("length of the value must be equal to 1")
}
}
}
pub trait FtpEntryInfo {
fn new(string: &str) -> Option<Self>
where
for<'a> Self: TryFrom<&'a str>,
{
Self::try_from(string).ok()
}
fn kind(&self) -> FtpEntryKind;
fn name(&self) -> &str;
fn size(&self) -> usize;
fn date_str(&self) -> &str;
}
#[derive(Debug)]
pub enum FtpEntry {
Unix(FtpEntryUnix),
Msdos(FtpEntryMsdos),
}
impl FtpEntry {
pub fn new(string: &str) -> Option<Self> {
string.try_into().ok()
}
pub fn is_unix_type(&self) -> bool {
match self {
FtpEntry::Unix(_) => true,
_ => false,
}
}
pub fn is_msdos_type(&self) -> bool {
match self {
FtpEntry::Msdos(_) => true,
_ => false,
}
}
pub fn to_unix_type(self) -> FtpEntryUnix {
self.try_to_unix_type().expect("FtpEntryType missmatch")
}
pub fn to_msdos_type(self) -> FtpEntryMsdos {
self.try_to_msdos_type().expect("FtpEntryType missmatch")
}
pub fn try_to_unix_type(self) -> Result<FtpEntryUnix, Self> {
if let FtpEntry::Unix(entry) = self {
Ok(entry)
} else {
Err(self)
}
}
pub fn try_to_msdos_type(self) -> Result<FtpEntryMsdos, Self> {
if let FtpEntry::Msdos(entry) = self {
Ok(entry)
} else {
Err(self)
}
}
}
impl Deref for FtpEntry {
type Target = dyn FtpEntryInfo;
fn deref(&self) -> &Self::Target {
match self {
FtpEntry::Msdos(entry) => entry,
FtpEntry::Unix(entry) => entry,
}
}
}
impl TryFrom<&str> for FtpEntry {
type Error = ();
fn try_from(value: &str) -> Result<Self, Self::Error> {
if let Ok(entry) = FtpEntryUnix::try_from(value) {
return Ok(FtpEntry::Unix(entry));
}
if let Ok(entry) = FtpEntryMsdos::try_from(value) {
return Ok(FtpEntry::Msdos(entry));
}
Err(())
}
}