use std::cmp::Ordering;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ParameterSource {
CommandLine,
Environment,
Default,
DefaultMap,
Prompt,
}
impl ParameterSource {
#[inline]
pub fn is_from_user(&self) -> bool {
matches!(self, Self::CommandLine | Self::Prompt)
}
#[inline]
fn precedence(&self) -> u8 {
match self {
Self::CommandLine | Self::Prompt => 4,
Self::Environment => 3,
Self::DefaultMap => 2,
Self::Default => 1,
}
}
}
impl fmt::Display for ParameterSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CommandLine => write!(f, "command line"),
Self::Environment => write!(f, "environment variable"),
Self::Default => write!(f, "default value"),
Self::DefaultMap => write!(f, "default map"),
Self::Prompt => write!(f, "prompt"),
}
}
}
impl PartialOrd for ParameterSource {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ParameterSource {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.precedence().cmp(&other.precedence())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_from_user() {
assert!(ParameterSource::CommandLine.is_from_user());
assert!(ParameterSource::Prompt.is_from_user());
assert!(!ParameterSource::Environment.is_from_user());
assert!(!ParameterSource::Default.is_from_user());
assert!(!ParameterSource::DefaultMap.is_from_user());
}
#[test]
fn test_display() {
assert_eq!(ParameterSource::CommandLine.to_string(), "command line");
assert_eq!(
ParameterSource::Environment.to_string(),
"environment variable"
);
assert_eq!(ParameterSource::Default.to_string(), "default value");
assert_eq!(ParameterSource::DefaultMap.to_string(), "default map");
assert_eq!(ParameterSource::Prompt.to_string(), "prompt");
}
#[test]
fn test_precedence_ordering() {
assert!(ParameterSource::CommandLine > ParameterSource::Environment);
assert!(ParameterSource::Environment > ParameterSource::DefaultMap);
assert!(ParameterSource::DefaultMap > ParameterSource::Default);
assert_eq!(
ParameterSource::CommandLine.cmp(&ParameterSource::Prompt),
Ordering::Equal
);
assert!(ParameterSource::Prompt > ParameterSource::Environment);
}
#[test]
fn test_equality() {
assert_eq!(ParameterSource::CommandLine, ParameterSource::CommandLine);
assert_ne!(ParameterSource::CommandLine, ParameterSource::Environment);
}
#[test]
fn test_copy_clone() {
let source = ParameterSource::CommandLine;
let copied = source; let cloned = source.clone(); assert_eq!(source, copied);
assert_eq!(source, cloned);
}
#[test]
fn test_hash() {
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(ParameterSource::CommandLine);
set.insert(ParameterSource::Environment);
set.insert(ParameterSource::CommandLine);
assert_eq!(set.len(), 2);
assert!(set.contains(&ParameterSource::CommandLine));
assert!(set.contains(&ParameterSource::Environment));
}
#[test]
fn test_debug() {
let debug_str = format!("{:?}", ParameterSource::CommandLine);
assert_eq!(debug_str, "CommandLine");
}
}