kernel_cmdline: convert to ThisError and sort

BUG=b:197143586
TEST=cargo check

Change-Id: Ic86f0d08b36e5f1b30ea35def38e3875c972c938
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105081
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2021-08-18 14:22:51 -07:00 committed by Commit Bot
parent 793b4703cf
commit a5884b5a6e
3 changed files with 17 additions and 21 deletions

2
Cargo.lock generated
View file

@ -609,6 +609,8 @@ name = "kernel_cmdline"
version = "0.1.0"
dependencies = [
"libc",
"remain",
"thiserror",
]
[[package]]

View file

@ -5,6 +5,8 @@ edition = "2018"
[dependencies]
libc = "*"
remain = "*"
thiserror = "*"
[lib]
path = "src/kernel_cmdline.rs"

View file

@ -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<T> = result::Result<T, Error>;