Skip to main content

crate_plugin_kit/config/
paths.rs

1//! The directories a kit instance works in, and the files inside them.
2//!
3//! [`KitPaths`] is resolved once, when the kit is built, from [`KitConfig::data_dir`] or
4//! the default `~/.{id}`. Every path the crate touches afterwards hangs off it: the
5//! install dirs under `plugins`, the build-host scaffolding under `build`, the install
6//! lock and the record cache.
7
8use std::path::PathBuf;
9
10use crate::error::{KitError, KitResult};
11
12use super::KitConfig;
13
14impl KitPaths {
15    /// Resolves the various paths. Creates directories as needed.
16    pub(super) fn resolve(cfg: &KitConfig) -> KitResult<Self> {
17        let root = match &cfg.data_dir {
18            Some(d) => d.clone(),
19            None => default_data_dir(&cfg.id)?,
20        };
21        Ok(Self {
22            plugins: root.join("plugins"),
23            build: root.join("build"),
24            lock_file: root.join(".lock"),
25            index_file: root.join("plugins").join(".plugins.json"),
26            root,
27        })
28    }
29}
30
31/// Default data dir `~/.{id}`, as determined by `directories`.
32fn default_data_dir(id: &str) -> KitResult<PathBuf> {
33    let home = directories::UserDirs::new()
34        .map(|d| d.home_dir().to_path_buf())
35        .ok_or(KitError::NoDataDir)?;
36    Ok(home.join(format!(".{id}")))
37}
38
39/// All paths the kit uses.
40#[derive(Debug, Clone)]
41pub struct KitPaths {
42    /// `<data-dir>` itself.
43    pub root: PathBuf,
44    /// Install dir of installed plugins, `<root>/plugins`.
45    pub plugins: PathBuf,
46    /// build-host scaffolding directory, `<root>/build`.
47    pub build: PathBuf,
48    /// Install exclusive lock file, `<root>/.lock`.
49    pub lock_file: PathBuf,
50    /// Install record cache, `<root>/plugins/.plugins.json`.
51    pub index_file: PathBuf,
52}
53
54impl KitPaths {
55    /// Install dir of one plugin, `<plugins>/<crate_name>`.
56    pub fn plugin_dir(&self, crate_name: &str) -> PathBuf {
57        self.plugins.join(crate_name)
58    }
59
60    /// Manifest path of one plugin.
61    pub fn manifest_path(&self, crate_name: &str, manifest_name: &str) -> PathBuf {
62        self.plugin_dir(crate_name).join(manifest_name)
63    }
64
65    /// build-host scaffolding directory of one plugin.
66    pub fn build_dir(&self, crate_name: &str) -> PathBuf {
67        self.build.join(crate_name)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    fn cfg() -> KitConfig {
76        KitConfig::new("myapp")
77    }
78
79    #[test]
80    fn paths_honour_the_data_dir_override() {
81        let root = PathBuf::from("some").join("dir");
82        let c = cfg().with_data_dir(&root);
83        let p = c.paths().expect("must not touch HOME when overridden");
84
85        assert_eq!(p.root, root);
86        assert_eq!(p.plugins, root.join("plugins"));
87        assert_eq!(p.build, root.join("build"));
88        assert_eq!(p.lock_file, root.join(".lock"));
89        assert_eq!(p.index_file, root.join("plugins").join(".plugins.json"));
90    }
91
92    #[test]
93    fn plugin_paths_sit_under_the_plugins_dir() {
94        let root = PathBuf::from("some").join("dir");
95        let c = cfg().with_data_dir(&root);
96        let p = c.paths().unwrap();
97
98        assert_eq!(
99            p.plugin_dir("myapp-plugin-foo"),
100            root.join("plugins").join("myapp-plugin-foo")
101        );
102        assert_eq!(
103            p.build_dir("myapp-plugin-foo"),
104            root.join("build").join("myapp-plugin-foo")
105        );
106        assert_eq!(
107            p.manifest_path("myapp-plugin-foo", "myapp-plugin.toml"),
108            root.join("plugins")
109                .join("myapp-plugin-foo")
110                .join("myapp-plugin.toml")
111        );
112    }
113}