flep_protocol/command/
mod.rs

1pub use self::port::PORT;
2pub use self::mode::{MODE, Mode};
3pub use self::basic::{ABOR, CDUP, EPSV, FEAT, NOOP, PASV, PWD,
4                      QUIT, REIN, STOU, SYST};
5pub use self::misc::{ACCT, APPE, CWD, DELE, HELP, LIST, MDTM, MKD, NLST,
6                     RETR, RMD, RNFR, RNTO, SITE, SIZE, STAT, STOR, TYPE,
7                     USER, PASS};
8pub use self::security::{ADAT, AUTH, CCC, CONF, ENC, MIC, PBSZ, PROT};
9pub use self::unimplemented::*;
10
11#[macro_use]
12pub mod macros;
13pub mod port;
14pub mod mode;
15/// Commands which take no arguments.
16pub mod basic;
17pub mod misc;
18pub mod security;
19pub mod unimplemented;
20
21use Error;
22use std::io::prelude::*;
23use std::{io, fmt};
24
25/// An FTP command.
26pub trait Command : Clone + fmt::Debug + PartialEq + Eq
27{
28    /// Writes the command to a buffer.
29    fn write(&self, write: &mut Write) -> Result<(), Error> {
30        // Write the payload to a temporary space
31        let mut payload_buffer = io::Cursor::new(Vec::new());
32        self.write_payload(&mut payload_buffer)?;
33        let payload = payload_buffer.into_inner();
34
35        // Don't write a redundant space unless there actually is a payload.
36        if payload.is_empty() {
37            write!(write, "{}", self.command_name())?;
38        } else {
39            write!(write, "{} ", self.command_name())?;
40            write.write(&payload)?;
41        }
42
43        Ok(())
44    }
45
46    /// Writes the payload data.
47    fn write_payload(&self, write: &mut Write) -> Result<(), Error>;
48
49    /// Reads payload data.
50    fn read_payload(read: &mut BufRead) -> Result<Self, Error>;
51
52    /// Gets the name of the command.
53    fn command_name(&self) -> &'static str;
54
55    fn bytes(&self) -> Vec<u8> {
56        let mut buffer = io::Cursor::new(Vec::new());
57        self.write(&mut buffer).expect("IO failure while writing to memory buffer");
58        buffer.into_inner()
59    }
60
61    /// Generates the text string for this packet.
62    fn to_string(&self) -> String {
63        String::from_utf8(self.bytes()).unwrap()
64    }
65}