sql_fun_core/args/
error.rs1use std::path::{Path, PathBuf};
2
3use crate::{ExtensionConfigError, HighlighterThemeError, MetadataError, args::ConfigurationKind};
4
5#[derive(Debug, thiserror::Error)]
7pub enum SqlFunArgsError {
8 #[error("{1} not supported for {0}")]
10 NotSupported(String, String),
11
12 #[error("Io Error {0}")]
14 Io(std::io::Error),
15
16 #[error("File not found {0}")]
18 FilePathNotFound(std::path::PathBuf),
19
20 #[error("Unexpected error: {0}")]
22 Unexpected(String),
23
24 #[error("HOME environment variable not defined")]
26 UserHomeNotDefined(),
27
28 #[error("Environment variable access error : {0}")]
30 EnvVar(#[from] std::env::VarError),
31
32 #[error(transparent)]
34 Metadata(#[from] MetadataError),
35
36 #[error("Invalid PostgreSQL engine version specified as {0}")]
38 InvalidPostgresEngineVersion(String),
39
40 #[error("Insufficient configuration in metadata and arguments : {0} not specified")]
42 InsufficientConfiguration(ConfigurationKind),
43
44 #[error("parse error {0} : {1}")]
46 PgExtenstionsParse(String, String),
47
48 #[error(transparent)]
50 ExtensionConfig(#[from] ExtensionConfigError),
51
52 #[error("home of sql-fun not eists : {0}")]
54 SqlFunHomeNotExist(PathBuf),
55
56 #[error(transparent)]
58 Highlighter(#[from] HighlighterThemeError),
59
60 #[error(transparent)]
62 Clap(#[from] clap::Error),
63}
64
65impl From<std::io::Error> for SqlFunArgsError {
66 #[track_caller]
67 fn from(value: std::io::Error) -> Self {
68 log::error!("IO Error: {value}");
69 Self::Io(value)
70 }
71}
72
73impl SqlFunArgsError {
74 #[track_caller]
80 pub fn user_home_not_defined<T>() -> Result<T, Self> {
81 log::error!("std::env::home_dir returns none");
82 Err(Self::UserHomeNotDefined())
83 }
84
85 #[track_caller]
91 pub fn home_not_exist<T, P: AsRef<Path>>(path: P) -> Result<T, Self> {
92 Err(Self::SqlFunHomeNotExist(path.as_ref().to_path_buf()))
93 }
94
95 #[track_caller]
101 pub fn file_path_not_found<T, P: AsRef<Path> + std::fmt::Debug>(path: P) -> Result<T, Self> {
102 #[cfg(test)]
103 panic!("Not found {path:?}");
104
105 #[cfg(not(test))]
106 Err(Self::FilePathNotFound(path.as_ref().to_path_buf()))
107 }
108
109 pub fn not_suppoted<T>(value: &str, category: &str) -> Result<T, Self> {
115 log::error!("Not supported value {value} for {category}.");
116 Err(Self::NotSupported(category.to_string(), value.to_string()))
117 }
118
119 pub fn invalid_engine_version<T>(value: &str) -> Result<T, Self> {
125 log::error!("Invalid PostgreSQL engine version {value}");
126 Err(Self::InvalidPostgresEngineVersion(value.to_string()))
127 }
128
129 pub fn no_postgres_engine_version_specified<T>() -> Result<T, Self> {
135 Err(Self::InsufficientConfiguration(
136 ConfigurationKind::EngineVersion,
137 ))
138 }
139
140 pub fn parsing_postgres_extensions<T>(message: &str, source: &str) -> Result<T, Self> {
146 Err(Self::PgExtenstionsParse(
147 message.to_string(),
148 source.to_string(),
149 ))
150 }
151}