Skip to main content

worktree_io/config/
mod.rs

1mod ops;
2mod ops_get_set;
3mod ser;
4
5#[cfg(test)]
6#[path = "ops_tests.rs"]
7mod ops_tests;
8
9#[cfg(test)]
10#[path = "ops_auto_prune_tests.rs"]
11mod ops_auto_prune_tests;
12
13use serde::{Deserialize, Serialize};
14
15use crate::ttl::Ttl;
16
17/// Top-level configuration for the worktree CLI.
18#[derive(Debug, Clone, Serialize, Deserialize, Default)]
19#[serde(default)]
20pub struct Config {
21    /// Editor configuration.
22    pub editor: EditorConfig,
23    /// Workspace open behavior.
24    pub open: OpenConfig,
25    /// Hook scripts run around the open command.
26    pub hooks: HooksConfig,
27    /// Workspace lifecycle configuration.
28    pub workspace: WorkspaceConfig,
29}
30
31/// Workspace lifecycle configuration.
32#[derive(Debug, Clone, Serialize, Deserialize, Default)]
33#[serde(default)]
34pub struct WorkspaceConfig {
35    /// Maximum age of a workspace before it is considered expired.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub ttl: Option<Ttl>,
38    /// When true, expired worktrees are pruned each time `open` is invoked.
39    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
40    pub auto_prune: bool,
41}
42
43/// Shell scripts executed before and after opening a workspace.
44#[derive(Debug, Clone, Serialize, Deserialize, Default)]
45pub struct HooksConfig {
46    /// Script run before opening the workspace.
47    #[serde(rename = "pre:open", skip_serializing_if = "Option::is_none", default)]
48    pub pre_open: Option<String>,
49    /// Script run after opening the workspace.
50    #[serde(rename = "post:open", skip_serializing_if = "Option::is_none", default)]
51    pub post_open: Option<String>,
52}
53
54/// Editor-related configuration.
55#[derive(Debug, Clone, Serialize, Deserialize, Default)]
56#[serde(default)]
57pub struct EditorConfig {
58    /// Command to launch the editor, e.g. "code ." or "nvim ."
59    pub command: Option<String>,
60}
61
62/// Controls how the workspace is opened.
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[serde(default)]
65pub struct OpenConfig {
66    /// Whether to launch the configured editor when opening a workspace.
67    pub editor: bool,
68}
69
70impl Default for OpenConfig {
71    fn default() -> Self {
72        Self { editor: true }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    #[test]
80    fn test_config_default() {
81        let c = Config::default();
82        assert!(c.editor.command.is_none());
83        assert!(c.open.editor);
84        assert!(c.hooks.pre_open.is_none());
85    }
86    #[test]
87    fn test_editor_config_default() {
88        assert!(EditorConfig::default().command.is_none());
89    }
90    #[test]
91    fn test_open_config_default() {
92        assert!(OpenConfig::default().editor);
93    }
94}