use std::path::{Path, PathBuf};
use crate::{ExtensionConfigError, HighlighterThemeError, MetadataError, args::ConfigurationKind};
#[derive(Debug, thiserror::Error)]
pub enum SqlFunArgsError {
#[error("{1} not supported for {0}")]
NotSupported(String, String),
#[error("Io Error {0}")]
Io(std::io::Error),
#[error("File not found {0}")]
FilePathNotFound(std::path::PathBuf),
#[error("Unexpected error: {0}")]
Unexpected(String),
#[error("HOME environment variable not defined")]
UserHomeNotDefined(),
#[error("Environment variable access error : {0}")]
EnvVar(#[from] std::env::VarError),
#[error(transparent)]
Metadata(#[from] MetadataError),
#[error("Invalid PostgreSQL engine version specified as {0}")]
InvalidPostgresEngineVersion(String),
#[error("Insufficient configuration in metadata and arguments : {0} not specified")]
InsufficientConfiguration(ConfigurationKind),
#[error("parse error {0} : {1}")]
PgExtenstionsParse(String, String),
#[error(transparent)]
ExtensionConfig(#[from] ExtensionConfigError),
#[error("home of sql-fun not eists : {0}")]
SqlFunHomeNotExist(PathBuf),
#[error(transparent)]
Highlighter(#[from] HighlighterThemeError),
#[error(transparent)]
Clap(#[from] clap::Error),
}
impl From<std::io::Error> for SqlFunArgsError {
#[track_caller]
fn from(value: std::io::Error) -> Self {
log::error!("IO Error: {value}");
Self::Io(value)
}
}
impl SqlFunArgsError {
#[track_caller]
pub fn user_home_not_defined<T>() -> Result<T, Self> {
log::error!("std::env::home_dir returns none");
Err(Self::UserHomeNotDefined())
}
#[track_caller]
pub fn home_not_exist<T, P: AsRef<Path>>(path: P) -> Result<T, Self> {
Err(Self::SqlFunHomeNotExist(path.as_ref().to_path_buf()))
}
#[track_caller]
pub fn file_path_not_found<T, P: AsRef<Path> + std::fmt::Debug>(path: P) -> Result<T, Self> {
#[cfg(test)]
panic!("Not found {path:?}");
#[cfg(not(test))]
Err(Self::FilePathNotFound(path.as_ref().to_path_buf()))
}
pub fn not_suppoted<T>(value: &str, category: &str) -> Result<T, Self> {
log::error!("Not supported value {value} for {category}.");
Err(Self::NotSupported(category.to_string(), value.to_string()))
}
pub fn invalid_engine_version<T>(value: &str) -> Result<T, Self> {
log::error!("Invalid PostgreSQL engine version {value}");
Err(Self::InvalidPostgresEngineVersion(value.to_string()))
}
pub fn no_postgres_engine_version_specified<T>() -> Result<T, Self> {
Err(Self::InsufficientConfiguration(
ConfigurationKind::EngineVersion,
))
}
pub fn parsing_postgres_extensions<T>(message: &str, source: &str) -> Result<T, Self> {
Err(Self::PgExtenstionsParse(
message.to_string(),
source.to_string(),
))
}
}