umanux/src/userlib_error.rs

47 lines
977 B
Rust
Raw Normal View History

2020-09-29 11:10:24 +02:00
use std::error::Error;
use std::fmt::{self, Display};
2020-09-29 12:14:07 +02:00
#[derive(Debug, PartialEq)]
pub enum ParseError {
Username,
Password,
Uid,
Gid,
Gecos,
HomeDir,
ShellDir,
}
#[derive(Debug, PartialEq)]
2020-09-29 11:10:24 +02:00
pub enum UserLibError {
NotFound,
2020-09-29 12:14:07 +02:00
ParseError,
2020-09-29 11:10:24 +02:00
Message(String),
}
impl Display for UserLibError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NotFound => write!(f, ""),
2020-09-29 12:14:07 +02:00
Self::ParseError => write!(f, "Failed to parse"), // TODO details
2020-09-29 11:10:24 +02:00
Self::Message(message) => write!(f, "{}", message),
}
}
}
impl Error for UserLibError {
fn description(&self) -> &str {
match self {
Self::NotFound => "not found",
2020-09-29 12:14:07 +02:00
Self::ParseError => "failed to parse",
2020-09-29 11:10:24 +02:00
Self::Message(message) => message,
}
}
}
2020-09-29 12:14:07 +02:00
impl From<&str> for UserLibError {
fn from(err: &str) -> Self {
Self::Message(err.to_owned())
2020-09-29 11:10:24 +02:00
}
}