summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 65e7aff263ccd89192abacbb5ab78e61af49b3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Definition of an iris [error][Error].

use std::fmt;
use std::io;

macro_rules! error {
    ($($arg:tt)*) => {
        $crate::error::Error::new(format!($($arg)*))
    };
}

pub(crate) use error;

/// Errors that can occur in iris
#[derive(thiserror::Error, Debug)]
pub enum Error {
	/// Occurs when a file could not be read, or could not be written to.
	#[error(transparent)]
	Io(#[from] io::Error),
	/// Occurs when an error was raised from the local git repository
	#[error(transparent)]
	Git(#[from] git2::Error),
	/// Occurs when iris failed to convert its internal structures into the
	/// config string representation.
	#[error(transparent)]
	De(#[from] crate::parse::de::Error),
	/// Occurs when a provided string representation of an iris structure is
	/// invalid and could not be converted/parsed.
	#[error(transparent)]
	Ser(#[from] crate::parse::ser::Error),
	/// Generic error message, occurs anywhere
	#[error("{0}")]
	Custom(String),
}

impl Error {
	pub fn new(fmt: impl fmt::Display) -> Self {
		Self::Custom(fmt.to_string())
	}
}

impl From<&str> for Error {
	fn from(msg: &str) -> Self {
		Self::new(msg)
	}
}

/// iris result type
pub type Result<T> = std::result::Result<T, Error>;