config/errors/
mod.rs

1//! Git Interactive Rebase Tool - Config crate errors
2//!
3//! # Description
4//! This module contains error types used in the Config crate.
5
6mod config_error_cause;
7mod invalid_color;
8
9use std::fmt::{Display, Formatter};
10
11use thiserror::Error;
12
13pub use crate::errors::{config_error_cause::ConfigErrorCause, invalid_color::InvalidColorError};
14
15/// Config errors
16#[derive(Error, Debug, PartialEq)]
17#[non_exhaustive]
18#[allow(clippy::module_name_repetitions)]
19pub struct ConfigError {
20	name: String,
21	input: Option<String>,
22	#[source]
23	cause: ConfigErrorCause,
24}
25
26impl ConfigError {
27	pub(crate) fn new(name: &str, input: &str, cause: ConfigErrorCause) -> Self {
28		Self {
29			name: String::from(name),
30			input: Some(String::from(input)),
31			cause,
32		}
33	}
34
35	pub(crate) fn new_with_optional_input(name: &str, input: Option<String>, cause: ConfigErrorCause) -> Self {
36		Self {
37			name: String::from(name),
38			input,
39			cause,
40		}
41	}
42
43	pub(crate) fn new_read_error(name: &str, cause: ConfigErrorCause) -> Self {
44		Self {
45			name: String::from(name),
46			input: None,
47			cause,
48		}
49	}
50}
51
52impl Display for ConfigError {
53	#[inline]
54	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55		if let Some(input) = self.input.as_deref() {
56			write!(
57				f,
58				"Provided value '{input}' is invalid for '{}': {}.",
59				self.name, self.cause
60			)
61		}
62		else {
63			write!(f, "Provided value is invalid for '{}': {}.", self.name, self.cause)
64		}
65	}
66}