1use std::path::{Path, PathBuf};
2
3use crate::{CodingError, CodingErrorCode};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7#[allow(clippy::struct_field_names)] pub struct AppPaths {
9 config_dir: PathBuf,
10 state_dir: PathBuf,
11 data_dir: PathBuf,
12}
13
14impl AppPaths {
15 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 #[must_use]
45 pub fn config_dir(&self) -> &Path {
46 &self.config_dir
47 }
48
49 #[must_use]
51 pub fn state_dir(&self) -> &Path {
52 &self.state_dir
53 }
54
55 #[must_use]
57 pub fn data_dir(&self) -> &Path {
58 &self.data_dir
59 }
60
61 #[must_use]
63 pub fn settings_file(&self) -> PathBuf {
64 self.config_dir.join("settings.json")
65 }
66
67 #[must_use]
69 pub fn providers_file(&self) -> PathBuf {
70 self.config_dir.join("providers.json")
71 }
72
73 #[must_use]
75 pub fn trust_file(&self) -> PathBuf {
76 self.state_dir.join("project-trust.json")
77 }
78
79 #[must_use]
81 pub fn session_database(&self) -> PathBuf {
82 self.state_dir.join("sessions.sqlite3")
83 }
84}