Skip to main content

worktree_io/config/
mod.rs

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