crate_plugin_kit/config/
paths.rs1use std::path::PathBuf;
9
10use crate::error::{KitError, KitResult};
11
12use super::KitConfig;
13
14impl KitPaths {
15 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
31fn 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#[derive(Debug, Clone)]
41pub struct KitPaths {
42 pub root: PathBuf,
44 pub plugins: PathBuf,
46 pub build: PathBuf,
48 pub lock_file: PathBuf,
50 pub index_file: PathBuf,
52}
53
54impl KitPaths {
55 pub fn plugin_dir(&self, crate_name: &str) -> PathBuf {
57 self.plugins.join(crate_name)
58 }
59
60 pub fn manifest_path(&self, crate_name: &str, manifest_name: &str) -> PathBuf {
62 self.plugin_dir(crate_name).join(manifest_name)
63 }
64
65 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}