-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change help us to save 13 LOC. Additionally, this PR makes config errors more explicit (including file path and line number)
- Loading branch information
1 parent
46566d2
commit dfca4be
Showing
3 changed files
with
22 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,34 @@ | ||
//! # Errors | ||
use std::fmt::{self, Debug, Formatter}; | ||
|
||
/// These errors can used in the program. | ||
/// Kibi error type. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// Wrapper around `std::io::Error` | ||
IO(std::io::Error), | ||
/// Wrapper around `nix::Error` | ||
#[cfg(unix)] | ||
Nix(nix::Error), | ||
InvalidWindowSize, | ||
/// Wrapper around `std::sync::mpsc::TryRecvError` | ||
MPSCTryRecv(std::sync::mpsc::TryRecvError), | ||
/// Error returned when the window size obtained through a system call is invalid. | ||
InvalidWindowSize, | ||
/// Error setting or retrieving the cursor position. | ||
CursorPosition, | ||
Config(String, String), | ||
/// Configuration error. The three attributes correspond the file path, the line number and the | ||
/// error message. | ||
Config(std::path::PathBuf, usize, String), | ||
/// Too many arguments given to kibi. The attribute corresponds to the total number of command | ||
/// line armuments. | ||
TooManyArguments(usize), | ||
} | ||
|
||
impl Debug for Error { | ||
/// Format the value using the given formatter. | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
match self { | ||
Self::IO(err) => write!(f, "IO error: {}", err), | ||
#[cfg(unix)] | ||
Self::Nix(err) => write!(f, "System call error: {}", err), | ||
Self::InvalidWindowSize => write!(f, "Invalid window size"), | ||
Self::MPSCTryRecv(err) => write!(f, "MSPC try_recv error: {}", err), | ||
Self::CursorPosition => write!(f, "Could not obtain cursor position"), | ||
Self::Config(line, reason) => write!(f, "Could not parse config {}: {}", line, reason), | ||
Self::TooManyArguments(n) => write!(f, "Expected 0 or 1 argument, got {}", n), | ||
} | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for Error { | ||
/// Convert an IO Error into a Kibi Error. | ||
fn from(err: std::io::Error) -> Self { Self::IO(err) } | ||
} | ||
|
||
#[cfg(unix)] | ||
impl From<nix::Error> for Error { | ||
/// Convert a nix IO Error into a Kibi Error. | ||
/// Convert a nix Error into a Kibi Error. | ||
fn from(err: nix::Error) -> Self { Self::Nix(err) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters