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