#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ArgumentValue<'a> {
String(&'a str),
Integer(i32),
Boolean(bool),
Null,
}
impl<'a> From<Option<&'a str>> for ArgumentValue<'a> {
#[inline]
fn from(value: Option<&'a str>) -> Self {
match value {
Some(value) => ArgumentValue::from(value),
None => ArgumentValue::Null,
}
}
}
impl<'a> From<&'a str> for ArgumentValue<'a> {
fn from(value: &'a str) -> Self {
const SPECIAL_VALUES: [(&str, ArgumentValue); 5] = [
("", ArgumentValue::Null),
("t", ArgumentValue::Boolean(true)),
("f", ArgumentValue::Boolean(false)),
("true", ArgumentValue::Boolean(true)),
("false", ArgumentValue::Boolean(false)),
];
for (name, result) in &SPECIAL_VALUES {
if name.eq_ignore_ascii_case(value) {
return *result;
}
}
match value.parse::<i32>() {
Ok(int) => ArgumentValue::Integer(int),
Err(_) => ArgumentValue::String(value),
}
}
}
impl From<bool> for ArgumentValue<'_> {
#[inline]
fn from(value: bool) -> Self {
ArgumentValue::Boolean(value)
}
}
impl From<i32> for ArgumentValue<'_> {
#[inline]
fn from(value: i32) -> Self {
ArgumentValue::Integer(value)
}
}
impl From<()> for ArgumentValue<'_> {
#[inline]
fn from(_: ()) -> Self {
ArgumentValue::Null
}
}