Skip to main content

sql_fun_core/
builtin.rs

1use std::path::PathBuf;
2
3use crate::{SqlFunArgs, SqlFunArgsError, SqlFunMetadata};
4
5/// builtin item data path resolver
6pub struct PostgresBuiltinPaths {
7    builtin_base_path: PathBuf,
8}
9
10impl PostgresBuiltinPaths {
11    /// create instance
12    ///
13    /// # Errors
14    ///
15    /// Returns [`SqlFunArgsError`] when builtin directories cannot be resolved.
16    pub fn try_from_args(
17        args: &SqlFunArgs,
18        metadata: &SqlFunMetadata,
19    ) -> Result<Self, SqlFunArgsError> {
20        let base_path = args.builtin_info_dir(metadata)?;
21        Ok(Self {
22            builtin_base_path: base_path,
23        })
24    }
25
26    fn path_exists_and_file(path: PathBuf) -> Result<PathBuf, SqlFunArgsError> {
27        if !path.exists() || !path.is_file() {
28            SqlFunArgsError::file_path_not_found(&path)?;
29        }
30        Ok(path)
31    }
32
33    /// returns type data file path
34    ///
35    /// # Errors
36    ///
37    /// Returns [`SqlFunArgsError`] when the expected file is missing.
38    pub fn types(&self) -> Result<PathBuf, SqlFunArgsError> {
39        Self::path_exists_and_file(self.builtin_base_path.join("pg_type.json"))
40    }
41
42    /// returns type data file path
43    ///
44    /// # Errors
45    ///
46    /// Returns [`SqlFunArgsError`] when the expected file is missing.
47    pub fn functions(&self) -> Result<PathBuf, SqlFunArgsError> {
48        Self::path_exists_and_file(self.builtin_base_path.join("functions.json"))
49    }
50
51    /// function details for table functions
52    ///
53    /// # Errors
54    ///
55    /// Returns [`SqlFunArgsError`] when the expected file is missing.
56    pub fn function_details(&self) -> Result<PathBuf, SqlFunArgsError> {
57        Self::path_exists_and_file(self.builtin_base_path.join("func_detail.json"))
58    }
59
60    /// binary operators
61    ///
62    /// # Errors
63    ///
64    /// Returns [`SqlFunArgsError`] when the expected file is missing.
65    pub fn binary_operators(&self) -> Result<PathBuf, SqlFunArgsError> {
66        Self::path_exists_and_file(self.builtin_base_path.join("binary_op.json"))
67    }
68
69    /// left unary operators
70    ///
71    /// # Errors
72    ///
73    /// Returns [`SqlFunArgsError`] when the expected file is missing.
74    pub fn left_unary_operators(&self) -> Result<PathBuf, SqlFunArgsError> {
75        Self::path_exists_and_file(self.builtin_base_path.join("left_unary_op.json"))
76    }
77
78    /// right unary operators
79    ///
80    /// # Errors
81    ///
82    /// Returns [`SqlFunArgsError`] when the expected file is missing.
83    pub fn right_unary_operators(&self) -> Result<PathBuf, SqlFunArgsError> {
84        Self::path_exists_and_file(self.builtin_base_path.join("right_unary_op.json"))
85    }
86
87    /// casts
88    ///
89    /// # Errors
90    ///
91    /// Returns [`SqlFunArgsError`] when the expected file is missing.
92    pub fn casts(&self) -> Result<PathBuf, SqlFunArgsError> {
93        Self::path_exists_and_file(self.builtin_base_path.join("casts.json"))
94    }
95}