Skip to main content

dodot_lib/commands/
mod.rs

1//! Public command API — the entry points for all dodot operations.
2//!
3//! Each function returns a `Result<T>` where `T: Serialize`. These
4//! types are the contract with standout's rendering layer — they
5//! carry everything needed to produce both human-readable (template)
6//! and machine-readable (JSON) output.
7
8pub mod addignore;
9pub mod adopt;
10pub mod down;
11pub mod fill;
12pub mod init;
13pub mod list;
14pub mod status;
15pub mod up;
16
17#[cfg(test)]
18mod tests;
19
20use serde::Serialize;
21
22// ── Shared display types ────────────────────────────────────────
23
24/// Handler symbols matching the Go implementation.
25pub fn handler_symbol(handler: &str) -> &'static str {
26    match handler {
27        "symlink" => "➞",
28        "shell" => "⚙",
29        "path" => "+",
30        "homebrew" => "⚙",
31        "install" => "×",
32        _ => "?",
33    }
34}
35
36/// Status string for standout template tag matching (maps to theme style names).
37pub fn status_style(deployed: bool) -> &'static str {
38    if deployed {
39        "deployed"
40    } else {
41        "pending"
42    }
43}
44
45/// Human-readable handler description for a file.
46pub fn handler_description(handler: &str, rel_path: &str, user_target: Option<&str>) -> String {
47    match handler {
48        "symlink" => {
49            if let Some(target) = user_target {
50                target.to_string()
51            } else {
52                // Apply dot. prefix convention: dot.bashrc → ~/.bashrc
53                let display_path = if !rel_path.contains('/') && rel_path.starts_with("dot.") {
54                    format!(".{}", &rel_path[4..])
55                } else {
56                    format!(".{rel_path}")
57                };
58                format!("~/{display_path}")
59            }
60        }
61        "shell" => "shell profile".into(),
62        "path" => format!("$PATH/{rel_path}"),
63        "install" => "run script".into(),
64        "homebrew" => "brew install".into(),
65        _ => String::new(),
66    }
67}
68
69/// A file entry for pack status display.
70#[derive(Debug, Clone, Serialize)]
71pub struct DisplayFile {
72    pub name: String,
73    pub symbol: String,
74    pub description: String,
75    pub status: String,
76    pub status_label: String,
77    pub handler: String,
78}
79
80/// A pack entry for status display.
81#[derive(Debug, Clone, Serialize)]
82pub struct DisplayPack {
83    pub name: String,
84    pub files: Vec<DisplayFile>,
85    /// Per-pack footnotes referenced by `(N)` markers in `status_label`.
86    /// Each entry is the body text only (no leading `(N)`); the renderer
87    /// pairs them with the marker number assigned at assembly time.
88    #[serde(skip_serializing_if = "Vec::is_empty", default)]
89    pub footnotes: Vec<String>,
90}
91
92/// One claimant of a cross-pack conflict, formatted for display.
93#[derive(Debug, Clone, Serialize)]
94pub struct DisplayClaimant {
95    /// Pack name.
96    pub pack: String,
97    /// Short, pack-relative source description (e.g. `git/env.sh`).
98    pub source: String,
99}
100
101/// A single cross-pack conflict, flattened for template rendering.
102#[derive(Debug, Clone, Serialize)]
103pub struct DisplayConflict {
104    /// Conflict kind. Serializes as `"symlink"` or `"path"` so the
105    /// template can branch on it.
106    pub kind: String,
107    /// Human-readable target (path for symlink, executable name for path).
108    pub target: String,
109    pub claimants: Vec<DisplayClaimant>,
110}
111
112impl DisplayConflict {
113    /// Convert a detection-layer conflict into its display form,
114    /// shortening paths relative to `home` when possible.
115    pub fn from_conflict(c: &crate::conflicts::Conflict, home: &std::path::Path) -> Self {
116        let kind = match c.kind {
117            crate::conflicts::ConflictKind::SymlinkTarget => "symlink",
118            crate::conflicts::ConflictKind::PathExecutable => "path",
119        };
120        let target = match c.kind {
121            crate::conflicts::ConflictKind::SymlinkTarget => shorten_path(&c.target, home),
122            crate::conflicts::ConflictKind::PathExecutable => c
123                .target
124                .file_name()
125                .map(|n| n.to_string_lossy().into_owned())
126                .unwrap_or_else(|| c.target.display().to_string()),
127        };
128        let claimants = c
129            .claimants
130            .iter()
131            .map(|cl| DisplayClaimant {
132                pack: cl.pack.clone(),
133                source: pack_relative_source(&cl.source, &cl.pack),
134            })
135            .collect();
136        DisplayConflict {
137            kind: kind.into(),
138            target,
139            claimants,
140        }
141    }
142}
143
144fn shorten_path(p: &std::path::Path, home: &std::path::Path) -> String {
145    if let Ok(rel) = p.strip_prefix(home) {
146        format!("~/{}", rel.display())
147    } else {
148        p.display().to_string()
149    }
150}
151
152/// Render a claimant source as `<pack>/<relative-path>` when possible,
153/// falling back to just the filename.
154fn pack_relative_source(source: &std::path::Path, pack: &str) -> String {
155    let s = source.to_string_lossy();
156    let marker = format!("/{pack}/");
157    if let Some(idx) = s.rfind(&marker) {
158        let rel = &s[idx + 1..];
159        return rel.to_string();
160    }
161    // Fallback: pack/filename
162    let fname = source
163        .file_name()
164        .map(|n| n.to_string_lossy().into_owned())
165        .unwrap_or_default();
166    format!("{pack}/{fname}")
167}
168
169/// Result type for commands that display pack status
170/// (status, up, down).
171#[derive(Debug, Clone, Serialize)]
172pub struct PackStatusResult {
173    #[serde(skip_serializing_if = "Option::is_none")]
174    pub message: Option<String>,
175    pub dry_run: bool,
176    pub packs: Vec<DisplayPack>,
177    #[serde(skip_serializing_if = "Vec::is_empty")]
178    pub warnings: Vec<String>,
179    /// Cross-pack conflicts to display at the end of the output.
180    #[serde(skip_serializing_if = "Vec::is_empty")]
181    pub conflicts: Vec<DisplayConflict>,
182    /// Names of pack-shaped directories skipped because they carry a
183    /// `.dodotignore` marker. Surfaced by `status` so users aren't
184    /// baffled when a directory they expected doesn't appear.
185    #[serde(skip_serializing_if = "Vec::is_empty")]
186    pub ignored_packs: Vec<String>,
187}