ftp/
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  InvalidAddress(::std::net::AddrParseError),
18}
19
20/// Text Format Control used in `TYPE` command
21#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
22pub enum FormatControl {
23  /// Default text format control (is NonPrint)
24  Default,
25  /// Non-print (not destined for printing)
26  NonPrint,
27  /// Telnet format control (\<CR\>, \<FF\>, etc.)
28  Telnet,
29  /// ASA (Fortran) Carriage Control
30  Asa,
31}
32
33/// File Type used in `TYPE` command
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
35pub enum FileType {
36  /// ASCII text (the argument is the text format control)
37  Ascii(FormatControl),
38  /// EBCDIC text (the argument is the text format control)
39  Ebcdic(FormatControl),
40  /// Image,
41  Image,
42  /// Binary (the synonym to Image)
43  Binary,
44  /// Local format (the argument is the number of bits in one byte on local machine)
45  Local(u8),
46}
47
48/// `Line` contains a command code and the contents of a line of text read from the network.
49pub struct Line(pub u32, pub String);
50
51impl ToString for FormatControl {
52  fn to_string(&self) -> String {
53    match self {
54      &FormatControl::Default | &FormatControl::NonPrint => String::from("N"),
55      &FormatControl::Telnet => String::from("T"),
56      &FormatControl::Asa => String::from("C"),
57    }
58  }
59}
60
61impl ToString for FileType {
62  fn to_string(&self) -> String {
63    match self {
64      &FileType::Ascii(ref fc) => format!("A {}", fc.to_string()),
65      &FileType::Ebcdic(ref fc) => format!("E {}", fc.to_string()),
66      &FileType::Image | &FileType::Binary => String::from("I"),
67      &FileType::Local(ref bits) => format!("L {}", bits),
68    }
69  }
70}
71
72impl fmt::Display for FtpError {
73  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74    match *self {
75      FtpError::ConnectionError(ref ioerr) => write!(f, "FTP ConnectionError: {}", ioerr),
76      FtpError::SecureError(ref desc) => write!(f, "FTP SecureError: {}", desc.clone()),
77      FtpError::InvalidResponse(ref desc) => {
78        write!(f, "FTP InvalidResponse: {}", desc.clone())
79      }
80      FtpError::InvalidAddress(ref perr) => write!(f, "FTP InvalidAddress: {}", perr),
81    }
82  }
83}
84
85impl Error for FtpError {
86  fn cause(&self) -> Option<&dyn Error> {
87    match *self {
88      FtpError::ConnectionError(ref ioerr) => Some(ioerr),
89      FtpError::SecureError(_) => None,
90      FtpError::InvalidResponse(_) => None,
91      FtpError::InvalidAddress(ref perr) => Some(perr),
92    }
93  }
94}