use std::env;
use std::error::Error;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetKind {
Local,
Remote,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TargetDetectionError {
NotFound,
InvalidValue(String),
}
impl fmt::Display for TargetDetectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TargetDetectionError::NotFound => {
write!(f, "environment variable \"target\" was not found")
}
TargetDetectionError::InvalidValue(v) => {
write!(
f,
"environment variable \"target\" had invalid value \"{}\"; expected \"local\" or \"remote\"",
v
)
}
}
}
}
impl Error for TargetDetectionError {}
pub fn detect_target_from_env() -> Result<TargetKind, TargetDetectionError> {
let raw = env::var("target").map_err(|_| TargetDetectionError::NotFound)?;
let value = raw.trim().to_lowercase();
match value.as_str() {
"local" => Ok(TargetKind::Local),
"remote" => Ok(TargetKind::Remote),
_ => Err(TargetDetectionError::InvalidValue(raw)),
}
}