use shep_core::config::{DaemonConfig, DaemonConfigError};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Control {
ReadOnly,
Allowed,
}
impl Control {
#[must_use]
pub const fn how_to_open(self) -> &'static str {
"control tools are off; add `[whistle]` with `allow_control = true` to \
$SHEP_HOME/shep.toml and restart whistle"
}
}
#[must_use]
pub fn resolve_control(loaded: &Result<DaemonConfig, DaemonConfigError>) -> Control {
match loaded {
Ok(config) if config.whistle.allow_control => Control::Allowed,
_ => Control::ReadOnly,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn control_of(text: Option<&str>) -> Control {
resolve_control(&DaemonConfig::load(text, &|_| None))
}
#[test]
fn the_file_is_the_only_source_and_it_defaults_to_read_only() {
assert_eq!(control_of(None), Control::ReadOnly);
assert_eq!(control_of(Some("")), Control::ReadOnly);
assert_eq!(
control_of(Some("[daemon]\nlog_level = \"info\"\n")),
Control::ReadOnly
);
assert_eq!(
control_of(Some("[whistle]\nallow_control = true\n")),
Control::Allowed
);
assert_eq!(
control_of(Some("[whistle]\nallow_control = false\n")),
Control::ReadOnly
);
}
#[test]
fn a_file_that_will_not_parse_is_read_as_no() {
assert_eq!(control_of(Some("[whistle")), Control::ReadOnly);
assert_eq!(
control_of(Some("[whistle]\nallow_control = \"yes\"\n")),
Control::ReadOnly,
"a string where a bool belongs is a broken file, not a true"
);
}
#[test]
fn the_refusal_names_the_file_and_the_key() {
let notice = Control::ReadOnly.how_to_open();
assert!(notice.contains("[whistle]"));
assert!(notice.contains("allow_control = true"));
assert!(notice.contains("shep.toml"));
assert!(
!notice.contains("--allow-control"),
"whistle has no such flag; pointing at one would send an operator in a circle"
);
}
}