Skip to main content

sql_fun_core/args/
error.rs

1use std::path::{Path, PathBuf};
2
3use crate::{ExtensionConfigError, HighlighterThemeError, MetadataError, args::ConfigurationKind};
4
5/// Error definition for [`crate::SqlFunArgs`]
6#[derive(Debug, thiserror::Error)]
7pub enum SqlFunArgsError {
8    /// Not supported value
9    #[error("{1} not supported for {0}")]
10    NotSupported(String, String),
11
12    /// IO Error
13    #[error("Io Error {0}")]
14    Io(std::io::Error),
15
16    /// path not found
17    #[error("File not found {0}")]
18    FilePathNotFound(std::path::PathBuf),
19
20    /// unexpected error
21    #[error("Unexpected error: {0}")]
22    Unexpected(String),
23
24    /// HOME environment variable not defined
25    #[error("HOME environment variable not defined")]
26    UserHomeNotDefined(),
27
28    /// Environment variable access error
29    #[error("Environment variable access error : {0}")]
30    EnvVar(#[from] std::env::VarError),
31
32    /// Metadata error
33    #[error(transparent)]
34    Metadata(#[from] MetadataError),
35
36    /// Invalid `PostgreSQL` engine version specified
37    #[error("Invalid PostgreSQL engine version specified as {0}")]
38    InvalidPostgresEngineVersion(String),
39
40    /// `InsufficientConfiguration` in metadata / args
41    #[error("Insufficient configuration in metadata and arguments : {0} not specified")]
42    InsufficientConfiguration(ConfigurationKind),
43
44    /// Parse error in `PostgreSQL` extensions
45    #[error("parse error {0} : {1}")]
46    PgExtenstionsParse(String, String),
47
48    /// `PostgreSQL` extension related errors
49    #[error(transparent)]
50    ExtensionConfig(#[from] ExtensionConfigError),
51
52    /// Home for `sql-fun` not exists
53    #[error("home of sql-fun not eists : {0}")]
54    SqlFunHomeNotExist(PathBuf),
55
56    /// Syntax highlighter theme file related errors
57    #[error(transparent)]
58    Highlighter(#[from] HighlighterThemeError),
59
60    /// Argument or environment valiable parse error
61    #[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    /// `std::env::home_dir` returns None
75    ///
76    /// # Errors
77    ///
78    /// Always returns [`SqlFunArgsError::UserHomeNotDefined`].
79    #[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    /// `sql-fun` home not exists
86    ///
87    /// # Errors
88    ///
89    /// Always returns [`SqlFunArgsError::SqlFunHomeNotExist`].
90    #[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    /// raize `FilePathNotFound`
96    ///
97    /// # Errors
98    ///
99    /// Always returns [`SqlFunArgsError::FilePathNotFound`] outside of tests.
100    #[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    /// not supported value used
110    ///
111    /// # Errors
112    ///
113    /// Always returns [`SqlFunArgsError::NotSupported`].
114    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    /// invalid postgres engine version specified
120    ///
121    /// # Errors
122    ///
123    /// Always returns [`SqlFunArgsError::InvalidPostgresEngineVersion`].
124    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    /// postgres engine version not specified
130    ///
131    /// # Errors
132    ///
133    /// Always returns [`SqlFunArgsError::InsufficientConfiguration`].
134    pub fn no_postgres_engine_version_specified<T>() -> Result<T, Self> {
135        Err(Self::InsufficientConfiguration(
136            ConfigurationKind::EngineVersion,
137        ))
138    }
139
140    /// extension list parse error
141    ///
142    /// # Errors
143    ///
144    /// Always returns [`SqlFunArgsError::PgExtenstionsParse`].
145    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}