#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Refusal {
Refused,
NoAgent,
}
impl Refusal {
#[must_use]
pub(crate) fn of_line(line: &str) -> Option<Refusal> {
let line = line.trim_end();
if line.starts_with("Error creating textual authentication agent") {
return Some(Refusal::NoAgent);
}
line.strip_prefix("Error executing command as another user").map(|why| {
if why.contains("No authentication agent") {
Refusal::NoAgent
} else {
Refusal::Refused
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pkexecs_own_lines_are_read_as_polkit_prints_them() {
assert_eq!(
Refusal::of_line("Error executing command as another user: Not authorized\n"),
Some(Refusal::Refused)
);
assert_eq!(
Refusal::of_line("Error executing command as another user: No authentication agent found."),
Some(Refusal::NoAgent)
);
assert_eq!(
Refusal::of_line(
"Error creating textual authentication agent: Error opening current controlling terminal for the process (`/dev/tty'): No such device or address"
),
Some(Refusal::NoAgent)
);
assert_eq!(Refusal::of_line("sh: 1: gpg: not found"), None, "the script's own output");
}
}