Skip to main content

worktree_io/config/
ser.rs

1use std::fmt::Write as _;
2
3use super::Config;
4
5/// Wrap a string value in TOML basic-string quotes, escaping special characters.
6///
7/// When the value contains newlines, a TOML multiline basic string (`"""..."""`) is
8/// used so that hook scripts remain human-readable in the config file.
9fn toml_quoted(s: &str) -> String {
10    if s.contains('\n') {
11        let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
12        return format!("\"\"\"\n{escaped}\"\"\"");
13    }
14    let escaped = s
15        .replace('\\', "\\\\")
16        .replace('"', "\\\"")
17        .replace('\r', "\\r")
18        .replace('\t', "\\t");
19    format!("\"{escaped}\"")
20}
21
22impl Config {
23    /// Serialize the config to a TOML string with inline documentation comments.
24    ///
25    /// Each section header and field is preceded by a `#` comment that matches
26    /// the doc-comment on the corresponding struct field.  The resulting string
27    /// round-trips cleanly through [`toml::from_str`].
28    #[must_use]
29    pub fn to_toml_with_comments(&self) -> String {
30        let mut out = String::new();
31
32        // Website header comment
33        out.push_str("# runner \u{2014} https://worktree.io\n\n");
34
35        // [editor] ------------------------------------------------------------
36        out.push_str("# Editor configuration.\n");
37        out.push_str("[editor]\n");
38        if let Some(cmd) = &self.editor.command {
39            out.push_str("# Command to launch the editor, e.g. \"code .\" or \"nvim .\"\n");
40            writeln!(out, "command = {}", toml_quoted(cmd)).unwrap();
41        }
42        if self.editor.background {
43            out.push_str("# When true, the editor opens in the background (fire-and-forget).\n");
44            writeln!(out, "background = {}", self.editor.background).unwrap();
45        }
46        out.push('\n');
47
48        // [open] --------------------------------------------------------------
49        out.push_str("# Workspace open behavior.\n");
50        out.push_str("[open]\n");
51        out.push_str("# Whether to launch the configured editor when opening a workspace.\n");
52        writeln!(out, "editor = {}", self.open.editor).unwrap();
53        out.push('\n');
54
55        // [hooks] -------------------------------------------------------------
56        out.push_str("# Hook scripts run around the open command.\n");
57        out.push_str("[hooks]\n");
58        if let Some(pre) = &self.hooks.pre_open {
59            out.push_str("# Script run before opening the workspace.\n");
60            writeln!(out, "\"pre:open\" = {}", toml_quoted(pre)).unwrap();
61        }
62        if let Some(post) = &self.hooks.post_open {
63            out.push_str("# Script run after opening the workspace.\n");
64            writeln!(out, "\"post:open\" = {}", toml_quoted(post)).unwrap();
65        }
66        out.push('\n');
67
68        // [workspace] ---------------------------------------------------------
69        out.push_str("# Workspace lifecycle configuration.\n");
70        out.push_str("[workspace]\n");
71        if let Some(ttl) = &self.workspace.ttl {
72            out.push_str("# Maximum age of a workspace before it is considered expired.\n");
73            writeln!(out, "ttl = {}", toml_quoted(&ttl.to_string())).unwrap();
74        }
75        if self.workspace.auto_prune {
76            out.push_str(
77                "# When true, expired worktrees are pruned each time `open` is invoked.\n",
78            );
79            writeln!(out, "auto_prune = {}", self.workspace.auto_prune).unwrap();
80        }
81        if self.workspace.temp {
82            out.push_str("# When true, worktrees are stored under the OS temp directory.\n");
83            writeln!(out, "temp = {}", self.workspace.temp).unwrap();
84        }
85
86        out
87    }
88}
89
90#[cfg(test)]
91#[path = "ser_tests.rs"]
92mod ser_tests;
93
94#[cfg(test)]
95#[path = "ser_multiline_tests.rs"]
96mod ser_multiline_tests;
97
98#[cfg(test)]
99#[path = "ser_workspace_tests.rs"]
100mod ser_workspace_tests;