1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::{io, process::Output};

/// Describes all errors variants from this crate.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// A tmux invocation returned some output where none was expected (actions such as
    /// some `tmux display-message` invocations).
    #[error(
        "unexpected process output: intent: `{intent}`, stdout: `{stdout}`, stderr: `{stderr}`"
    )]
    UnexpectedTmuxOutput {
        intent: &'static str,
        stdout: String,
        stderr: String,
    },

    /// Indicates Tmux has a weird config, like missing the `"default-shell"`.
    #[error("unexpected tmux config: `{0}`")]
    TmuxConfig(&'static str),

    /// Some parsing error.
    #[error("failed parsing: `{intent}`")]
    ParseError {
        desc: &'static str,
        intent: &'static str,
        err: nom::Err<nom::error::Error<String>>,
    },

    /// Failed parsing the output of a process invocation as utf-8.
    #[error("failed parsing utf-8 string: `{source}`")]
    Utf8 {
        #[from]
        /// Source error.
        source: std::string::FromUtf8Error,
    },

    /// Some IO error.
    #[error("failed with io: `{source}`")]
    Io {
        #[from]
        /// Source error.
        source: io::Error,
    },
}

/// Convert a nom error into an owned error and add the parsing intent.
///
/// # Errors
///
/// This maps to a `Error::ParseError`.
#[must_use]
pub fn map_add_intent(
    desc: &'static str,
    intent: &'static str,
    nom_err: nom::Err<nom::error::Error<&str>>,
) -> Error {
    Error::ParseError {
        desc,
        intent,
        err: nom_err.to_owned(),
    }
}

/// Ensure that the output's stdout and stderr are empty, indicating
/// the command had succeeded.
///
/// # Errors
///
/// Returns a `Error::UnexpectedTmuxOutput` in case .
pub fn check_empty_process_output(
    output: &Output,
    intent: &'static str,
) -> std::result::Result<(), Error> {
    if !output.stdout.is_empty() || !output.stderr.is_empty() {
        let stdout = String::from_utf8_lossy(&output.stdout[..]).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr[..]).to_string();
        return Err(Error::UnexpectedTmuxOutput {
            intent,
            stdout,
            stderr,
        });
    }
    Ok(())
}