use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ExperimentalFeature {}
impl ExperimentalFeature {
pub const ALL: [Self; 0] = [];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {}
}
}
impl fmt::Display for ExperimentalFeature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for ExperimentalFeature {
type Err = UnknownExperimentalFeature;
fn from_str(raw: &str) -> Result<Self, Self::Err> {
Self::ALL
.into_iter()
.find(|feature| feature.as_str() == raw)
.ok_or_else(|| UnknownExperimentalFeature {
value: raw.to_owned(),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownExperimentalFeature {
pub value: String,
}
impl fmt::Display for UnknownExperimentalFeature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let recognized = ExperimentalFeature::ALL
.map(ExperimentalFeature::as_str)
.join(", ");
if recognized.is_empty() {
write!(
f,
"unknown experimental feature '{}'; no experimental features are currently \
recognized",
self.value,
)
} else {
write!(
f,
"unknown experimental feature '{}'; expected one of: {recognized}",
self.value,
)
}
}
}
impl std::error::Error for UnknownExperimentalFeature {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unknown_value_reports_no_recognized_features() {
for value in ["frobnicate", "standard-compat"] {
let err = value.parse::<ExperimentalFeature>().unwrap_err();
assert_eq!(
err.to_string(),
format!(
"unknown experimental feature '{value}'; no experimental features are \
currently recognized"
),
);
}
}
}