diff --git a/Cargo.lock b/Cargo.lock index de61419cbf..830e814810 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -609,6 +609,8 @@ name = "kernel_cmdline" version = "0.1.0" dependencies = [ "libc", + "remain", + "thiserror", ] [[package]] diff --git a/kernel_cmdline/Cargo.toml b/kernel_cmdline/Cargo.toml index 0000610e36..bf2a5d6ef9 100644 --- a/kernel_cmdline/Cargo.toml +++ b/kernel_cmdline/Cargo.toml @@ -5,6 +5,8 @@ edition = "2018" [dependencies] libc = "*" +remain = "*" +thiserror = "*" [lib] path = "src/kernel_cmdline.rs" diff --git a/kernel_cmdline/src/kernel_cmdline.rs b/kernel_cmdline/src/kernel_cmdline.rs index ad040ecdda..589fccf4f5 100644 --- a/kernel_cmdline/src/kernel_cmdline.rs +++ b/kernel_cmdline/src/kernel_cmdline.rs @@ -4,37 +4,29 @@ //! Helper for creating valid kernel command line strings. -use std::fmt::{self, Display}; use std::result; +use remain::sorted; +use thiserror::Error; + /// The error type for command line building operations. -#[derive(PartialEq, Debug)] +#[sorted] +#[derive(Error, PartialEq, Debug)] pub enum Error { - /// Operation would have resulted in a non-printable ASCII character. - InvalidAscii, - /// Key/Value Operation would have had a space in it. - HasSpace, /// Key/Value Operation would have had an equals sign in it. + #[error("string contains an equals sign")] HasEquals, + /// Key/Value Operation would have had a space in it. + #[error("string contains a space")] + HasSpace, + /// Operation would have resulted in a non-printable ASCII character. + #[error("string contains non-printable ASCII character")] + InvalidAscii, /// Operation would have made the command line too large. + #[error("inserting string would make command line too long")] TooLarge, } -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - let description = match self { - InvalidAscii => "string contains non-printable ASCII character", - HasSpace => "string contains a space", - HasEquals => "string contains an equals sign", - TooLarge => "inserting string would make command line too long", - }; - - write!(f, "{}", description) - } -} - /// Specialized Result type for command line operations. pub type Result = result::Result;