use crate::parsers::ParseError;
pub(crate) trait UnwrapValue<V> {
fn unwrap_value(self) -> V;
fn is_value(&self, value: V) -> bool;
}
impl<V: PartialEq> UnwrapValue<V> for Result<V, ParseError> {
fn unwrap_value(self) -> V {
self.unwrap_or_else(|e| panic!("expected a value: {e}"))
}
fn is_value(&self, value: V) -> bool {
self.as_ref().is_ok_and(|v| *v == value)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{Name, Parser};
use assert2::check;
#[test]
fn test_is_value() {
check!(Name::from_str("abcde").is_value("abcde".into()));
}
#[test]
fn test_is_value_fails() {
check!(!Name::from_str("abcde").is_value("xyz".into()));
check!(!Name::from_str("-").is_value("abcde".into()));
}
#[test]
fn test_unwrap_value() {
check!(Name::from_str("abcde").unwrap_value() == "abcde");
}
}