summaryrefslogtreecommitdiffstats
path: root/src/config/errors.rs
blob: a3aedb665de2edc54eb0181437b2aced95d7dcfd (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Git Interactive Rebase Tool - Config crate errors
//!
//! # Description
//! This module contains error types used in the Config crate.

mod config_error_cause;
mod invalid_color;

use std::fmt::{Display, Formatter};

use thiserror::Error;

pub(crate) use crate::config::errors::{config_error_cause::ConfigErrorCause, invalid_color::InvalidColorError};

/// Config errors
#[derive(Error, Debug, PartialEq)]
#[non_exhaustive]
#[allow(clippy::module_name_repetitions)]
pub(crate) struct ConfigError {
	name: String,
	input: Option<String>,
	#[source]
	cause: ConfigErrorCause,
}

impl ConfigError {
	pub(crate) fn new(name: &str, input: &str, cause: ConfigErrorCause) -> Self {
		Self {
			name: String::from(name),
			input: Some(String::from(input)),
			cause,
		}
	}

	pub(crate) fn new_with_optional_input(name: &str, input: Option<String>, cause: ConfigErrorCause) -> Self {
		Self {
			name: String::from(name),
			input,
			cause,
		}
	}

	pub(crate) fn new_read_error(name: &str, cause: ConfigErrorCause) -> Self {
		Self {
			name: String::from(name),
			input: None,
			cause,
		}
	}
}

impl Display for ConfigError {
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		if let Some(input) = self.input.as_deref() {
			write!(
				f,
				"Provided value '{input}' is invalid for '{}': {}.",
				self.name, self.cause
			)
		}
		else {
			write!(f, "Provided value is invalid for '{}': {}.", self.name, self.cause)
		}
	}
}