Combine error types

This commit is contained in:
David Tolnay 2025-03-02 22:18:55 -08:00
parent d876f6e238
commit b1a731c5f7
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
5 changed files with 13 additions and 31 deletions

View file

@ -1,4 +1,3 @@
use crate::etcetera;
use std::error::Error as StdError;
use std::fmt::{self, Display};
use std::io;
@ -9,7 +8,7 @@ pub enum Error {
TomlSer(toml::ser::Error),
TomlDe(toml::de::Error),
Quote(shlex::QuoteError),
HomeDir(etcetera::HomeDirError),
HomeDir,
Bat(bat::error::Error),
}
@ -39,12 +38,6 @@ impl From<shlex::QuoteError> for Error {
}
}
impl From<etcetera::HomeDirError> for Error {
fn from(error: etcetera::HomeDirError) -> Self {
Error::HomeDir(error)
}
}
impl From<bat::error::Error> for Error {
fn from(error: bat::error::Error) -> Self {
Error::Bat(error)
@ -58,7 +51,7 @@ impl Display for Error {
Error::TomlSer(e) => e.fmt(formatter),
Error::TomlDe(e) => e.fmt(formatter),
Error::Quote(e) => e.fmt(formatter),
Error::HomeDir(e) => e.fmt(formatter),
Error::HomeDir => formatter.write_str("could not locate home directory"),
Error::Bat(e) => e.fmt(formatter),
}
}
@ -71,7 +64,7 @@ impl StdError for Error {
Error::TomlSer(e) => e.source(),
Error::TomlDe(e) => e.source(),
Error::Quote(e) => e.source(),
Error::HomeDir(e) => e.source(),
Error::HomeDir => None,
Error::Bat(e) => e.source(),
}
}

View file

@ -1,4 +1,4 @@
use crate::etcetera::HomeDirError;
use crate::error::Result;
use std::path::PathBuf;
pub trait BaseStrategy {
@ -7,7 +7,7 @@ pub trait BaseStrategy {
macro_rules! create_strategies {
($base: ty) => {
pub fn choose_base_strategy() -> Result<$base, HomeDirError> {
pub fn choose_base_strategy() -> Result<$base> {
<$base>::new()
}
};

View file

@ -1,13 +1,12 @@
use crate::error::Result;
use std::path::PathBuf;
use crate::etcetera::HomeDirError;
pub struct Windows {
home_dir: PathBuf,
}
impl Windows {
pub fn new() -> Result<Self, HomeDirError> {
pub fn new() -> Result<Self> {
Ok(Self {
home_dir: crate::etcetera::home_dir()?,
})

View file

@ -1,14 +1,13 @@
use crate::error::Result;
use std::path::Path;
use std::path::PathBuf;
use crate::etcetera::HomeDirError;
pub struct Xdg {
home_dir: PathBuf,
}
impl Xdg {
pub fn new() -> Result<Self, HomeDirError> {
pub fn new() -> Result<Self> {
Ok(Self {
home_dir: crate::etcetera::home_dir()?,
})

View file

@ -1,18 +1,9 @@
use crate::error::{Error, Result};
pub mod base_strategy;
pub use base_strategy::{choose_base_strategy, BaseStrategy};
pub fn home_dir() -> Result<std::path::PathBuf, HomeDirError> {
home::home_dir().ok_or(HomeDirError)
pub fn home_dir() -> Result<std::path::PathBuf> {
home::home_dir().ok_or(Error::HomeDir)
}
#[derive(Debug)]
pub struct HomeDirError;
impl std::fmt::Display for HomeDirError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "could not locate home directory")
}
}
impl std::error::Error for HomeDirError {}