Skip to main content

systemprompt_models/paths/
mod.rs

1mod build;
2pub mod constants;
3mod error;
4mod system;
5mod web;
6
7pub use build::BuildPaths;
8pub use constants::{cloud_container, dir_names, file_names};
9pub use error::PathError;
10pub use system::SystemPaths;
11pub use web::WebPaths;
12
13use std::path::{Path, PathBuf};
14use std::sync::OnceLock;
15
16use crate::profile::PathsConfig;
17
18static APP_PATHS: OnceLock<AppPaths> = OnceLock::new();
19
20#[derive(Debug, Clone)]
21pub struct AppPaths {
22    system: SystemPaths,
23    web: WebPaths,
24    build: BuildPaths,
25    storage: Option<PathBuf>,
26    ai_config: Option<PathBuf>,
27}
28
29impl AppPaths {
30    pub fn init(profile_paths: &PathsConfig) -> Result<(), PathError> {
31        if APP_PATHS.get().is_some() {
32            return Ok(());
33        }
34        let paths = Self::from_profile(profile_paths)?;
35        APP_PATHS
36            .set(paths)
37            .map_err(|_| PathError::AlreadyInitialized)?;
38        Ok(())
39    }
40
41    pub fn get() -> Result<&'static Self, PathError> {
42        APP_PATHS.get().ok_or(PathError::NotInitialized)
43    }
44
45    fn from_profile(paths: &PathsConfig) -> Result<Self, PathError> {
46        Ok(Self {
47            system: SystemPaths::from_profile(paths)?,
48            web: WebPaths::from_profile(paths),
49            build: BuildPaths::from_profile(paths),
50            storage: paths.storage.as_ref().map(PathBuf::from),
51            ai_config: Some(PathBuf::from(paths.ai_config())),
52        })
53    }
54
55    pub const fn system(&self) -> &SystemPaths {
56        &self.system
57    }
58
59    pub const fn web(&self) -> &WebPaths {
60        &self.web
61    }
62
63    pub const fn build(&self) -> &BuildPaths {
64        &self.build
65    }
66
67    pub fn storage(&self) -> Option<&Path> {
68        self.storage.as_deref()
69    }
70
71    pub fn ai_config(&self) -> Option<&Path> {
72        self.ai_config.as_deref()
73    }
74}