Skip to main content

tea_coding/
paths.rs

1use std::path::{Path, PathBuf};
2
3use crate::{CodingError, CodingErrorCode};
4
5/// Fully injected filesystem roots used by the coding product.
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[allow(clippy::struct_field_names)] // `_dir` distinguishes injected directory roots.
8pub struct AppPaths {
9    config_dir: PathBuf,
10    state_dir: PathBuf,
11    data_dir: PathBuf,
12}
13
14impl AppPaths {
15    /// Creates paths without consulting process environment or a real home.
16    ///
17    /// # Errors
18    ///
19    /// Rejects non-absolute roots or roots containing no final component.
20    pub fn new(
21        config_dir: impl Into<PathBuf>,
22        state_dir: impl Into<PathBuf>,
23        data_dir: impl Into<PathBuf>,
24    ) -> Result<Self, CodingError> {
25        let paths = [config_dir.into(), state_dir.into(), data_dir.into()];
26        if paths
27            .iter()
28            .any(|path| !path.is_absolute() || path.file_name().is_none())
29        {
30            return Err(CodingError::new(
31                CodingErrorCode::InvalidInput,
32                "application paths must be absolute directory paths",
33            ));
34        }
35        let [config_dir, state_dir, data_dir] = paths;
36        Ok(Self {
37            config_dir,
38            state_dir,
39            data_dir,
40        })
41    }
42
43    /// Returns the configuration directory.
44    #[must_use]
45    pub fn config_dir(&self) -> &Path {
46        &self.config_dir
47    }
48
49    /// Returns the durable state directory.
50    #[must_use]
51    pub fn state_dir(&self) -> &Path {
52        &self.state_dir
53    }
54
55    /// Returns the declarative resource data directory.
56    #[must_use]
57    pub fn data_dir(&self) -> &Path {
58        &self.data_dir
59    }
60
61    /// Returns the global settings file.
62    #[must_use]
63    pub fn settings_file(&self) -> PathBuf {
64        self.config_dir.join("settings.json")
65    }
66
67    /// Returns the global provider configuration file.
68    #[must_use]
69    pub fn providers_file(&self) -> PathBuf {
70        self.config_dir.join("providers.json")
71    }
72
73    /// Returns the trust decision store.
74    #[must_use]
75    pub fn trust_file(&self) -> PathBuf {
76        self.state_dir.join("project-trust.json")
77    }
78
79    /// Returns the `SQLite` session database path.
80    #[must_use]
81    pub fn session_database(&self) -> PathBuf {
82        self.state_dir.join("sessions.sqlite3")
83    }
84}