Skip to main content

apimock_config/config/
file_tree_config.rs

1use serde::Deserialize;
2
3/// Persistent filter preferences for [`FileTreeView`], loaded from the
4/// optional `[file_tree_view]` section of `apimock.toml`.
5///
6/// When the section is absent, [`FileTreeViewConfig::default()`] is used,
7/// which mirrors [`apimock_routing::view::build::FileTreeFilter::default()`]:
8/// dotfiles hidden, built-in excludes on, no extra filters, gitignore off.
9///
10/// [`FileTreeView`]: apimock_routing::view::FileTreeView
11#[derive(Clone, Debug, Deserialize)]
12pub struct FileTreeViewConfig {
13    /// Show dotfiles and dot-directories (default: `false`).
14    #[serde(default)]
15    pub show_hidden: bool,
16
17    /// Apply the built-in exclude list (`target`, `node_modules`, etc.)
18    /// (default: `true`).
19    #[serde(default = "default_true")]
20    pub builtin_excludes: bool,
21
22    /// Glob patterns for additional exclusions (RFC 019).
23    ///
24    /// Each entry is matched against the entry's `file_name()` only.
25    /// Supports standard glob syntax (`*`, `?`, `[…]`). A trailing `/`
26    /// restricts the pattern to directories. Pre-5.11 exact-name entries
27    /// continue to work because a bare name is a valid glob.
28    #[serde(default)]
29    pub extra_excludes: Vec<String>,
30
31    /// If non-empty, only files whose name matches at least one of these
32    /// glob patterns are shown. Directories always pass the include filter
33    /// so the user can drill into them. (default: `[]` — show everything)
34    #[serde(default)]
35    pub include: Vec<String>,
36
37    /// Parse `.gitignore` files in the tree root and its ancestors,
38    /// applying Git-compatible ignore rules (RFC 019). (default: `false`)
39    #[serde(default)]
40    pub respect_gitignore: bool,
41}
42
43fn default_true() -> bool {
44    true
45}
46
47impl Default for FileTreeViewConfig {
48    fn default() -> Self {
49        Self {
50            show_hidden: false,
51            builtin_excludes: true,
52            extra_excludes: Vec::new(),
53            include: Vec::new(),
54            respect_gitignore: false,
55        }
56    }
57}
58
59impl FileTreeViewConfig {
60    /// Convert to the routing crate's [`FileTreeFilter`].
61    ///
62    /// [`FileTreeFilter`]: apimock_routing::view::build::FileTreeFilter
63    pub fn to_filter(&self) -> apimock_routing::view::build::FileTreeFilter {
64        apimock_routing::view::build::FileTreeFilter {
65            show_hidden: self.show_hidden,
66            builtin_excludes: self.builtin_excludes,
67            extra_excludes: self.extra_excludes.clone(),
68            include: self.include.clone(),
69            respect_gitignore: self.respect_gitignore,
70        }
71    }
72}