| 1 | use std::convert::From; |
| 2 | use std::error::Error; |
| 3 | use std::fmt; |
| 4 | use std::process::Output; |
| 5 | |
| 6 | #[derive(Debug)] |
| 7 | pub struct IptablesError { |
| 8 | pub code: i32, |
| 9 | pub msg: String, |
| 10 | } |
| 11 | |
| 12 | impl fmt::Display for IptablesError { |
| 13 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 14 | write!(f, "code: {}, msg: {}", self.code, self.msg) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | impl From<Output> for IptablesError { |
| 19 | fn from(output: Output) -> Self { |
| 20 | Self { |
| 21 | code: output.status.code().unwrap_or(-1), |
| 22 | msg: String::from_utf8_lossy(output.stderr.as_slice()).into(), |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl Error for IptablesError {} |