sokf_core/config/
excludes_d.rs1use std::fs;
8
9use serde::Deserialize;
10
11use crate::{Error, RepoPath, RepoRoot, Result};
12
13pub const EXCLUDES_DIR: &str = ".sokf/excludes.d";
16
17#[derive(Debug, Clone, Default, Deserialize)]
19#[serde(deny_unknown_fields)]
20struct ExcludesFile {
21 #[serde(default)]
22 exclude: Vec<String>,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct AddedExclude {
29 pub file: RepoPath,
31 pub glob: String,
33}
34
35pub(crate) fn read_added(root: &RepoRoot) -> Result<Vec<AddedExclude>> {
44 let mut found = Vec::new();
45 for file in excludes_files(root)? {
46 let text =
47 fs::read_to_string(file.to_fs(root)).map_err(|e| Error::io(file.to_fs(root), e))?;
48 let parsed: ExcludesFile = toml::from_str(&text)
49 .map_err(|e| Error::config(file.clone(), e.message().to_string()))?;
50 for glob in parsed.exclude {
51 if let Some(m) = super::file::glob_fault("exclude", &glob) {
53 return Err(Error::config(file, m));
54 }
55 found.push(AddedExclude {
56 file: file.clone(),
57 glob,
58 });
59 }
60 }
61 Ok(found)
62}
63
64fn excludes_files(root: &RepoRoot) -> Result<Vec<RepoPath>> {
68 super::d_folder::d_files(root, EXCLUDES_DIR, "toml", false)
69}
70
71pub fn excludes_files_lenient(root: &RepoRoot) -> Vec<RepoPath> {
75 super::d_folder::d_files(root, EXCLUDES_DIR, "toml", true).unwrap_or_default()
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
86 fn reads_each_file_in_path_order_and_refuses_a_bad_one() {
87 let dir = crate::test_support::fresh_dir("excludes-d");
88 let root = RepoRoot::new(dir.clone());
89 assert_eq!(read_added(&root).unwrap(), vec![]);
90 crate::test_support::write(
91 &dir,
92 ".sokf/excludes.d/b.toml",
93 "exclude = [\"/b1\", \"/b2/**\"]\n",
94 );
95 crate::test_support::write(&dir, ".sokf/excludes.d/a.toml", "exclude = [\"/a/**\"]\n");
96 crate::test_support::write(
97 &dir,
98 ".sokf/excludes.d/a/deep.toml",
99 "exclude = [\"/deep/*.rs\"]\n",
100 );
101 crate::test_support::write(&dir, ".sokf/excludes.d/empty.toml", "");
102 crate::test_support::write(&dir, ".sokf/excludes.d/notes.txt", "exclude = 1\n");
103 let globs: Vec<(String, String)> = read_added(&root)
104 .unwrap()
105 .into_iter()
106 .map(|e| (e.file.to_string(), e.glob))
107 .collect();
108 assert_eq!(
109 globs,
110 vec![
111 ("/.sokf/excludes.d/a.toml".into(), "/a/**".into()),
112 ("/.sokf/excludes.d/a/deep.toml".into(), "/deep/*.rs".into()),
113 ("/.sokf/excludes.d/b.toml".into(), "/b1".into()),
114 ("/.sokf/excludes.d/b.toml".into(), "/b2/**".into()),
115 ]
116 );
117 for (bad, says) in [
118 ("exclude = [", "unclosed array"),
119 ("exclude = []\nroots = []\n", "unknown field `roots`"),
120 ("exclude = \"/x\"\n", "invalid type: string"),
121 (
122 "exclude = [\"x/**\"]\n",
123 "exclude glob `x/**` is not /-rooted",
124 ),
125 ("exclude = [\"/[\"]\n", "exclude glob `/[` does not parse"),
126 ] {
127 crate::test_support::write(&dir, ".sokf/excludes.d/c.toml", bad);
128 let e = read_added(&root).unwrap_err();
129 assert!(matches!(e, Error::Config { .. }), "{bad:?} -> {e}");
130 let e = e.to_string();
131 assert!(
132 e.starts_with("/.sokf/excludes.d/c.toml: "),
133 "{bad:?} -> {e}"
134 );
135 assert!(e.contains(says), "{bad:?} -> {e}");
136 }
137 fs::remove_dir_all(&dir).unwrap();
138 }
139}