use crossterm::style::Stylize;
use miette::{NamedSource, SourceSpan};
use std::ops::Range;
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
pub enum ConfigError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
#[diagnostic(transparent)]
TomlError(#[from] TomlError),
#[error(
"Config already initialized.\nPlease report the bug to {}", "https://github.com/tkatter/sericom".bold()
)]
AlreadyInitialized,
}
#[derive(thiserror::Error, miette::Diagnostic, Debug)]
#[error("{}", "Error reading config file".red())]
#[diagnostic(
code("See valid config options"),
url("https://github.com/tkatter/sericom/blob/main/configuration/values.md"),
help("{}", self.msg.split_once(',').unwrap_or(("", self.msg.as_str())).1.trim())
)]
pub struct TomlError {
#[label("{}", self.msg.split_once(',').unwrap_or((self.msg.as_str(), "")).0.trim())]
at: SourceSpan,
#[source_code]
src: NamedSource<String>,
msg: String,
}
impl TomlError {
pub(crate) fn new(span: Range<usize>, source: String, message: String) -> Self {
let span_len = span.end - span.start;
let at: SourceSpan = (span.start, span_len).into();
let src = NamedSource::new("config.toml", source);
let msg = message;
Self { at, src, msg }
}
}