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