ftp_rs/
types.rs

1//! The set of valid values for FTP commands
2
3use std::convert::From;
4use std::error::Error;
5use std::fmt;
6
7/// A shorthand for a Result whose error type is always an FtpError.
8pub type Result<T> = ::std::result::Result<T, FtpError>;
9
10/// `FtpError` is a library-global error type to describe the different kinds of
11/// errors that might occur while using FTP.
12#[derive(Debug)]
13pub enum FtpError {
14    ConnectionError(::std::io::Error),
15    SecureError(String),
16    InvalidResponse(String),
17    InvalidArgument(String),
18    InvalidAddress(::std::net::AddrParseError),
19}
20
21/// Text Format Control used in `TYPE` command
22#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
23pub enum FormatControl {
24    /// Default text format control (is NonPrint)
25    Default,
26    /// Non-print (not destined for printing)
27    NonPrint,
28    /// Telnet format control (\<CR\>, \<FF\>, etc.)
29    Telnet,
30    /// ASA (Fortran) Carriage Control
31    Asa,
32}
33
34/// File Type used in `TYPE` command
35#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
36pub enum FileType {
37    /// ASCII text (the argument is the text format control)
38    Ascii(FormatControl),
39    /// EBCDIC text (the argument is the text format control)
40    Ebcdic(FormatControl),
41    /// Image,
42    Image,
43    /// Binary (the synonym to Image)
44    Binary,
45    /// Local format (the argument is the number of bits in one byte on local machine)
46    Local(u8),
47}
48
49impl ToString for FormatControl {
50    fn to_string(&self) -> String {
51        match self {
52            &FormatControl::Default | &FormatControl::NonPrint => String::from("N"),
53            &FormatControl::Telnet => String::from("T"),
54            &FormatControl::Asa => String::from("C"),
55        }
56    }
57}
58
59impl ToString for FileType {
60    fn to_string(&self) -> String {
61        match self {
62            &FileType::Ascii(ref fc) => format!("A {}", fc.to_string()),
63            &FileType::Ebcdic(ref fc) => format!("E {}", fc.to_string()),
64            &FileType::Image | &FileType::Binary => String::from("I"),
65            &FileType::Local(ref bits) => format!("L {}", bits),
66        }
67    }
68}
69
70impl fmt::Display for FtpError {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        match *self {
73            FtpError::ConnectionError(ref ioerr) => write!(f, "FTP ConnectionError: {}", ioerr),
74            FtpError::SecureError(ref desc) => write!(f, "FTP SecureError: {}", desc.clone()),
75            FtpError::InvalidResponse(ref desc) => {
76                write!(f, "FTP InvalidResponse: {}", desc.clone())
77            }
78            FtpError::InvalidArgument(ref desc) => {
79                write!(f, "FTP InvalidArgument: {}", desc.clone())
80            }
81            FtpError::InvalidAddress(ref perr) => write!(f, "FTP InvalidAddress: {}", perr),
82        }
83    }
84}
85
86impl Error for FtpError {
87    fn source(&self) -> Option<&(dyn Error + 'static)> {
88        match *self {
89            FtpError::ConnectionError(ref ioerr) => Some(ioerr),
90            FtpError::SecureError(_) => None,
91            FtpError::InvalidResponse(_) => None,
92            FtpError::InvalidArgument(_) => None,
93            FtpError::InvalidAddress(ref perr) => Some(perr),
94        }
95    }
96}
97
98#[cfg(test)]
99mod tests {
100
101    use super::*;
102
103    #[test]
104    fn format_control_str() {
105        assert_eq!(FormatControl::Default.to_string(), "N");
106        assert_eq!(FormatControl::NonPrint.to_string(), "N");
107        assert_eq!(FormatControl::Telnet.to_string(), "T");
108        assert_eq!(FormatControl::Asa.to_string(), "C");
109    }
110
111    #[test]
112    fn file_type_str() {
113        assert_eq!(FileType::Ascii(FormatControl::Default).to_string(), "A N");
114        assert_eq!(FileType::Ebcdic(FormatControl::Asa).to_string(), "E C");
115        assert_eq!(FileType::Image.to_string(), "I");
116        assert_eq!(FileType::Binary.to_string(), "I");
117        assert_eq!(FileType::Local(6).to_string(), "L 6");
118    }
119}