use core::fmt;
use std::ffi::OsString;
use shep_core::dogs::{SCHEMA_FLAG, VERSION_FLAG};
use shep_core::paths::{self, HOME_DIR_VAR, SHEP_HOME_VAR, ShepPaths};
pub const PRINT_CONFIG_FLAG: &str = "--print-config";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DogAction {
Run,
PrintConfig,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UsageError {
dog: String,
argument: String,
}
impl UsageError {
#[must_use]
pub fn argument(&self) -> &str {
&self.argument
}
#[must_use]
pub const fn exit_code(&self) -> u8 {
shep_core::exit::USAGE
}
}
impl fmt::Display for UsageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { dog, argument } = self;
match argument.as_str() {
"--help" | "-h" => write!(f, "{dog} takes one option, {PRINT_CONFIG_FLAG}."),
VERSION_FLAG | SCHEMA_FLAG => write!(
f,
"{dog} answers {argument} as its first argument only, which is where the \
shepherd asks it."
),
_ => write!(f, "{dog} does not understand {argument}."),
}
}
}
impl core::error::Error for UsageError {}
pub fn parse_args<'a>(
dog: &str,
args: impl IntoIterator<Item = &'a str>,
) -> Result<DogAction, UsageError> {
let mut action = DogAction::Run;
for argument in args {
if argument != PRINT_CONFIG_FLAG {
return Err(UsageError {
dog: dog.to_owned(),
argument: argument.to_owned(),
});
}
action = DogAction::PrintConfig;
}
Ok(action)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum HomeError {
Unset,
Empty,
NotUtf8,
}
impl HomeError {
#[must_use]
pub const fn exit_code(self) -> u8 {
shep_core::exit::USAGE
}
}
impl fmt::Display for HomeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unset => write!(
f,
"neither {SHEP_HOME_VAR} nor {HOME_DIR_VAR} is set, so there is no shep home \
to find the shepherd in"
),
Self::Empty => write!(f, "{SHEP_HOME_VAR} is set but empty"),
Self::NotUtf8 => write!(f, "{SHEP_HOME_VAR} is not valid UTF-8"),
}
}
}
impl core::error::Error for HomeError {}
pub fn resolve_paths(var: &dyn Fn(&str) -> Option<OsString>) -> Result<ShepPaths, HomeError> {
let named = match var("SHEP_HOME") {
None => None,
Some(value) if value.is_empty() => return Err(HomeError::Empty),
Some(value) => Some(value.into_string().map_err(|_| HomeError::NotUtf8)?),
};
let env = |key: &str| (key == "SHEP_HOME").then(|| named.clone()).flatten();
let home = paths::shep_home(&env, paths::user_home(var).as_deref()).ok_or(HomeError::Unset)?;
Ok(ShepPaths::at(home))
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[test]
fn no_arguments_runs_and_print_config_prints() {
assert_eq!(parse_args("shep-discord", []), Ok(DogAction::Run));
assert_eq!(
parse_args("shep-discord", ["--print-config", "--print-config"]),
Ok(DogAction::PrintConfig)
);
}
#[test]
fn every_other_argument_is_refused_by_name() {
let refused = |argument| parse_args("shep-log-rotate", [argument]).unwrap_err();
assert_eq!(
refused("-h").to_string(),
"shep-log-rotate takes one option, --print-config."
);
assert_eq!(
refused("--dry-run").to_string(),
"shep-log-rotate does not understand --dry-run."
);
assert_eq!(refused("--dry-run").exit_code(), shep_core::exit::USAGE);
}
#[test]
fn a_probe_flag_after_the_first_argument_says_where_it_belongs() {
let err = parse_args("shep-discord", ["--print-config", VERSION_FLAG]).unwrap_err();
assert_eq!(err.argument(), VERSION_FLAG);
assert!(
err.to_string().contains("as its first argument only"),
"{err}"
);
}
fn vars(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<OsString> {
let pairs: Vec<(String, OsString)> = pairs
.iter()
.map(|(key, value)| ((*key).to_owned(), OsString::from(value)))
.collect();
move |key| {
pairs
.iter()
.find(|(named, _)| named == key)
.map(|(_, value)| value.clone())
}
}
#[test]
fn shep_home_wins_and_needs_no_home_directory() {
let paths = resolve_paths(&vars(&[("SHEP_HOME", "/srv/shep")])).unwrap();
assert_eq!(paths, ShepPaths::at(PathBuf::from("/srv/shep")));
}
#[cfg(unix)]
#[test]
fn the_default_sits_under_the_users_home() {
let paths = resolve_paths(&vars(&[("HOME", "/home/ada")])).unwrap();
assert_eq!(paths.home, PathBuf::from("/home/ada/.shep"));
}
#[test]
fn no_home_at_all_is_refused_rather_than_the_working_directory() {
assert_eq!(resolve_paths(&vars(&[])), Err(HomeError::Unset));
assert_eq!(
resolve_paths(&vars(&[("SHEP_HOME", ""), ("HOME", "/home/ada")])),
Err(HomeError::Empty)
);
}
#[cfg(unix)]
#[test]
fn a_shep_home_that_is_not_utf8_is_refused() {
use std::os::unix::ffi::OsStringExt as _;
let lossy = OsString::from_vec(vec![b'/', 0xff]);
let var = move |key: &str| (key == "SHEP_HOME").then(|| lossy.clone());
assert_eq!(resolve_paths(&var), Err(HomeError::NotUtf8));
}
}