credence_lib/configuration/
files.rs

1use super::{
2    super::{render::*, util::*},
3    coordinate::*,
4    error::*,
5};
6
7use {compris::resolve::*, kutil_cli::debug::*, std::path::*};
8
9//
10// FilesConfiguration
11//
12
13/// Files configuration.
14#[derive(Clone, Debug, Debuggable, Resolve)]
15pub struct FilesConfiguration {
16    /// Assets path.
17    #[resolve]
18    #[debuggable(style(string))]
19    pub assets: PathBuf,
20
21    /// Status path.
22    #[resolve]
23    #[debuggable(style(string))]
24    pub status: PathBuf,
25
26    /// Templates path.
27    #[resolve]
28    #[debuggable(style(string))]
29    pub templates: PathBuf,
30
31    /// Coordinate.
32    #[resolve]
33    #[debuggable(as(debuggable))]
34    pub coordinate: CoordinateConfiguration,
35}
36
37impl FilesConfiguration {
38    /// Set assets paths if not already set.
39    pub fn set_assets_path<PathT>(&mut self, assets_path: PathT)
40    where
41        PathT: AsRef<Path>,
42    {
43        if self.assets.as_os_str().is_empty() {
44            self.assets = assets_path.as_ref().into();
45        }
46    }
47
48    /// Validate.
49    pub fn validate<PathT>(&mut self, base_path: PathT) -> Result<(), ConfigurationError>
50    where
51        PathT: AsRef<Path>,
52    {
53        let base_path = base_path.as_ref();
54
55        if !self.status.is_absolute() {
56            self.status = base_path.join(&self.status);
57        }
58
59        if !self.templates.is_absolute() {
60            self.templates = base_path.join(&self.templates);
61        }
62
63        self.coordinate.validate(base_path, vec![self.templates.clone()])?;
64
65        Ok(())
66    }
67
68    /// Asset path.
69    pub fn asset(&self, uri_path: &str) -> PathBuf {
70        self.assets.join(uri_path.trim_start_matches(PATH_SEPARATOR))
71    }
72
73    /// Templates.
74    pub fn templates(&self) -> Templates {
75        Templates::new(self)
76    }
77}
78
79impl Default for FilesConfiguration {
80    fn default() -> Self {
81        Self {
82            assets: PathBuf::default(),
83            status: PathBuf::from("status"),
84            templates: PathBuf::from("templates"),
85            coordinate: CoordinateConfiguration::default(),
86        }
87    }
88}