Skip to main content

isothrum_machine/install/
brew.rs

1//! What Homebrew prints when it, not the formula, is what is broken, read from one line of its
2//! error output: the user's environment to repair, which an install can't mend itself.
3
4/// A broken Homebrew.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Trouble {
7    /// A lock it can't take: a root-owned update lock (after a `sudo brew`), or a stale one it
8    /// thinks another update holds.
9    Locked,
10    /// Too old to know this macOS (`unknown or unsupported macOS version: :dunno`).
11    Outdated,
12    /// A source build refused because the installed Xcode is older than it needs.
13    XcodeTooOld,
14}
15
16impl Trouble {
17    /// The trouble `line` says, if it says one.
18    #[must_use]
19    pub fn of_line(line: &str) -> Option<Trouble> {
20        if (line.contains("homebrew/locks") && line.contains("Permission denied")) || (line.contains("brew update") && line.contains("already running")) {
21            Some(Trouble::Locked)
22        } else if line.contains("unknown or unsupported macOS version") {
23            Some(Trouble::Outdated)
24        } else if line.contains("is too outdated") || line.contains("Please update to Xcode") {
25            Some(Trouble::XcodeTooOld)
26        } else {
27            None
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn homebrews_lines_are_read_as_it_prints_them() {
38        assert_eq!(
39            Trouble::of_line("Error: Another `brew update` process is already running."),
40            Some(Trouble::Locked)
41        );
42        assert_eq!(Trouble::of_line("Error: unknown or unsupported macOS version: :dunno"), Some(Trouble::Outdated));
43        assert_eq!(
44            Trouble::of_line("Error: Your Xcode (26.6) at /Applications/Xcode.app is too outdated."),
45            Some(Trouble::XcodeTooOld)
46        );
47        assert_eq!(Trouble::of_line("==> Fetching qemu"), None);
48    }
49}