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;

/// Permissions of the Unix-like entry.
#[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)
    }
}

/// Type of the ftp entry.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum FtpEntryKind {
    UNKNOWN,
    Directory,
    File,
    BlockDevice,
    CharacterDevice,
    Pipe,
    Socket,
    Symlink,
    // Symlink(FtpEntryPath)
}

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")
        }
    }
}

/// All fields that supports both servers: Unix & MSDOS
pub trait FtpEntryInfo {
    /// Returns a new [`FtpEntry`] by given string if parsing was successful.
    /// Also you can create new [`FtpEntry`] by use [`TryFrom`] trait.
    /// ```rust
    /// # use ftp_cmd_list_parse::FtpEntry;
    /// let ftp_response = "drwxr-xr-x  10 root   root    4096 Dec 21  2012 usr";
    ///
    /// match FtpEntry::new(ftp_response) {
    ///     Some(ftp_entry) => {
    ///         assert_eq!(ftp_entry.name(), "usr");
    ///         assert_eq!(ftp_entry.size(), 4096);
    ///         assert_eq!(ftp_entry.date_str(), "Dec 21 2012");
    ///     }
    ///     None => println!("ftp_response is not valid ftp-entry!")
    /// }
    /// ```
    fn new(string: &str) -> Option<Self>
    where
        for<'a> Self: TryFrom<&'a str>,
    {
        Self::try_from(string).ok()
    }

    /// Represents type of the entry: directory, file, symlink or other.
    fn kind(&self) -> FtpEntryKind;
    /// Returns name of the entry.
    fn name(&self) -> &str;
    /// Returns size of the entry.
    fn size(&self) -> usize;
    // fn date(&self) -> NaiveDateTime;
    /// Returns date of the entry.
    fn date_str(&self) -> &str;
}

/// Represents parsed string as ftp entry.
///
/// Implements [`Deref`] to `&dyn FtpEntryInfo`, so you can get access
/// to general fields that supports both servers: Unix & MSDOS.
#[derive(Debug)]
pub enum FtpEntry {
    Unix(FtpEntryUnix),
    Msdos(FtpEntryMsdos),
}

impl FtpEntry {
    /// Returns a new [`FtpEntry`] by given string if parsing was successful.
    /// Also you can create new [`FtpEntry`] by use [`TryFrom`] or [`TryInto`] traits.
    /// ```rust
    /// # use ftp_cmd_list_parse::FtpEntry;
    /// let ftp_response = "drwxr-xr-x  10 root   root    4096 Dec 21  2012 usr";
    ///
    /// match FtpEntry::new(ftp_response) {
    ///     Some(ftp_entry) => {
    ///         assert_eq!(ftp_entry.name(), "usr");
    ///         assert_eq!(ftp_entry.size(), 4096);
    ///         assert_eq!(ftp_entry.date_str(), "Dec 21 2012");
    ///     }
    ///     None => println!("ftp_response is not valid ftp-entry!")
    /// }
    /// ```
    pub fn new(string: &str) -> Option<Self> {
        string.try_into().ok()
    }

    /// Returns true if [`FtpEntry`] has UNIX-like entry, otherwise false.
    pub fn is_unix_type(&self) -> bool {
        match self {
            FtpEntry::Unix(_) => true,
            _ => false,
        }
    }

    /// Returns true if [`FtpEntry`] has MSDOS-like entry, otherwise false.
    pub fn is_msdos_type(&self) -> bool {
        match self {
            FtpEntry::Msdos(_) => true,
            _ => false,
        }
    }

    /// Converts [`FtpEntry`] to [`FtpEntryUnix`].
    /// Its may be useful if you need to get additional infomation
    /// like permissions, group, owner and others.
    ///
    /// # Panics
    ///
    /// Panics if the value is not a Unix-like entry.
    /// If you not sure what kind of [`FtpEntry`] is, use [`try_to_unix_type`](#method.try_to_unix_type) instead.
    pub fn to_unix_type(self) -> FtpEntryUnix {
        self.try_to_unix_type().expect("FtpEntryType missmatch")
    }

    /// Converts [`FtpEntry`] to [`FtpEntryMsdos`].
    ///
    /// # Panics
    ///
    /// Panics if the value is not a Msdos-like entry.
    /// If you not sure what kind of [`FtpEntry`] is, use [`try_to_msdos_type`](#method.try_to_msdos_type) instead.
    pub fn to_msdos_type(self) -> FtpEntryMsdos {
        self.try_to_msdos_type().expect("FtpEntryType missmatch")
    }

    /// Tries to convert [`FtpEntry`] to [`FtpEntryUnix`].
    /// If it is impossible, returns [`FtpEntry`] back to caller.
    pub fn try_to_unix_type(self) -> Result<FtpEntryUnix, Self> {
        if let FtpEntry::Unix(entry) = self {
            Ok(entry)
        } else {
            Err(self)
        }
    }

    /// Tries to convert [`FtpEntry`] to [`FtpEntryMsdos`].
    /// If it is impossible, returns [`FtpEntry`] back to caller.
    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(())
    }
}