ftp_client/
status_code.rs1#[derive(Debug, PartialEq)]
7pub enum StatusCodeKind {
8 TransferStarted,
10 TransferAboutToStart,
12 Ok,
14 FeatureNotImplemented,
16 SystemStatus,
18 HelpMessage,
20 NameSystemType,
22 ReadyForNewUser,
24 ClosingControlConnection,
26 RequestActionCompleted,
28 UserLoggedIn,
30 EnteredPassiveMode,
32 EnteredExtendedPassiveMode,
34 RequestFileActionCompleted,
36 PathCreated,
38 PasswordRequired,
40 RequestActionPending,
42 CommandUnrecognized,
44 SecurityMechanismNotImplemented,
46 RequestActionDenied,
48 FileNameNotAllowed,
50 Unknown,
53}
54
55impl From<u16> for StatusCodeKind {
56 fn from(code: u16) -> StatusCodeKind {
57 match code {
58 125 => StatusCodeKind::TransferStarted,
59 150 => StatusCodeKind::TransferAboutToStart,
60 200 => StatusCodeKind::Ok,
61 202 => StatusCodeKind::FeatureNotImplemented,
62 211 => StatusCodeKind::SystemStatus,
63 214 => StatusCodeKind::HelpMessage,
64 215 => StatusCodeKind::NameSystemType,
65 221 => StatusCodeKind::ClosingControlConnection,
66 220 => StatusCodeKind::ReadyForNewUser,
67 226 => StatusCodeKind::RequestActionCompleted,
68 227 => StatusCodeKind::EnteredPassiveMode,
69 229 => StatusCodeKind::EnteredExtendedPassiveMode,
70 230 => StatusCodeKind::UserLoggedIn,
71 250 => StatusCodeKind::RequestFileActionCompleted,
72 257 => StatusCodeKind::PathCreated,
73 331 => StatusCodeKind::PasswordRequired,
74 350 => StatusCodeKind::RequestActionPending,
75 500 => StatusCodeKind::CommandUnrecognized,
76 504 => StatusCodeKind::SecurityMechanismNotImplemented,
77 550 => StatusCodeKind::RequestActionDenied,
78 553 => StatusCodeKind::FileNameNotAllowed,
79 _ => StatusCodeKind::Unknown,
80 }
81 }
82}
83
84#[derive(Debug)]
88pub struct StatusCode {
89 pub kind: StatusCodeKind,
91 pub code: u16,
93}
94
95impl PartialEq for StatusCode {
96 fn eq(&self, other: &StatusCode) -> bool {
97 self.code == other.code
98 }
99}
100
101impl StatusCode {
102 pub fn parse(text: &str) -> Self {
104 let code: &u16 = &text[0..3].parse().unwrap();
105 let code: u16 = *code;
106 let kind = StatusCodeKind::from(code);
107
108 Self { kind, code }
109 }
110
111 pub fn is_valid(&self) -> bool {
113 self.code > 199 && self.code < 399
114 }
115
116 pub fn is_failure(&self) -> bool {
118 self.code > 399 && self.code < 599
119 }
120}