use std::{error::Error, fmt};
use crate::result::{ClassifyError, ErrorKind};
pub const MAX_CONFIG_ISSUES: usize = 128;
const DEFAULT_CONFIG_ISSUE_PATH: &str = "configuration";
const DEFAULT_CONFIG_ISSUE_MESSAGE: &str = "is invalid";
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConfigIssue {
path: String,
message: String,
}
impl ConfigIssue {
#[must_use]
pub fn new(path: impl Into<String>, message: impl Into<String>) -> Self {
Self {
path: normalize_config_issue_field(path, DEFAULT_CONFIG_ISSUE_PATH),
message: normalize_config_issue_field(message, DEFAULT_CONFIG_ISSUE_MESSAGE),
}
}
#[must_use]
pub fn path(&self) -> &str {
&self.path
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
fn normalize_config_issue_field(value: impl Into<String>, fallback: &'static str) -> String {
let value = value.into().trim().to_owned();
if value.is_empty() {
fallback.to_owned()
} else {
value
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConfigIssues {
issues: Vec<ConfigIssue>,
truncated: bool,
}
impl ConfigIssues {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, path: impl Into<String>, message: impl Into<String>) {
self.push_issue(ConfigIssue::new(path, message));
}
pub fn require(
&mut self,
condition: bool,
path: impl Into<String>,
message: impl Into<String>,
) {
if !condition {
self.push(path, message);
}
}
pub fn require_non_empty(&mut self, path: impl Into<String>, value: &str) {
let path = path.into();
self.require(!value.trim().is_empty(), path, "must not be empty");
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.issues.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.issues.len()
}
#[must_use]
pub const fn is_truncated(&self) -> bool {
self.truncated
}
#[must_use]
pub fn as_slice(&self) -> &[ConfigIssue] {
&self.issues
}
pub fn extend(&mut self, error: ConfigValidationError) {
let nested = error.into_issues();
for issue in nested.issues {
self.push_issue(issue);
}
self.truncated |= nested.truncated;
}
pub fn extend_result(&mut self, result: Result<(), ConfigValidationError>) {
if let Err(error) = result {
self.extend(error);
}
}
#[must_use]
pub fn issues(&self) -> &[ConfigIssue] {
self.as_slice()
}
pub fn into_result(self) -> Result<(), ConfigValidationError> {
if self.issues.is_empty() {
Ok(())
} else {
Err(ConfigValidationError { issues: self })
}
}
fn push_issue(&mut self, issue: ConfigIssue) {
if self.issues.len() < MAX_CONFIG_ISSUES {
self.issues.push(issue);
} else {
self.truncated = true;
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConfigValidationError {
issues: ConfigIssues,
}
impl ConfigValidationError {
#[must_use]
pub fn new(mut issues: ConfigIssues) -> Self {
if issues.is_empty() {
issues.push(DEFAULT_CONFIG_ISSUE_PATH, DEFAULT_CONFIG_ISSUE_MESSAGE);
}
Self { issues }
}
#[must_use]
pub fn single(path: impl Into<String>, message: impl Into<String>) -> Self {
let mut issues = ConfigIssues::new();
issues.push(path, message);
Self { issues }
}
#[must_use]
pub fn issues(&self) -> &[ConfigIssue] {
self.issues.as_slice()
}
#[must_use]
pub fn into_issues(self) -> ConfigIssues {
self.issues
}
}
impl fmt::Display for ConfigValidationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("configuration validation failed")?;
for issue in self.issues() {
write!(formatter, "\n- {}: {}", issue.path(), issue.message())?;
}
if self.issues.is_truncated() {
formatter.write_str("\n- additional issues omitted")?;
}
Ok(())
}
}
impl Error for ConfigValidationError {}
impl ClassifyError for ConfigValidationError {
fn error_kind(&self) -> ErrorKind {
ErrorKind::Validation
}
fn error_code(&self) -> crate::result::ErrorCode {
crate::result::ErrorCode::internal("configuration_invalid")
}
}