#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum UnusedScope {
Public,
Private,
Function,
Struct,
All,
}
impl UnusedScope {
#[must_use]
pub fn try_parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"public" => Some(Self::Public),
"private" => Some(Self::Private),
"function" => Some(Self::Function),
"struct" => Some(Self::Struct),
"all" | "" => Some(Self::All),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_all_variants() {
assert_eq!(UnusedScope::try_parse("public"), Some(UnusedScope::Public));
assert_eq!(
UnusedScope::try_parse("private"),
Some(UnusedScope::Private)
);
assert_eq!(
UnusedScope::try_parse("function"),
Some(UnusedScope::Function)
);
assert_eq!(UnusedScope::try_parse("struct"), Some(UnusedScope::Struct));
assert_eq!(UnusedScope::try_parse("all"), Some(UnusedScope::All));
}
#[test]
fn empty_string_is_all() {
assert_eq!(UnusedScope::try_parse(""), Some(UnusedScope::All));
}
#[test]
fn is_case_insensitive() {
assert_eq!(UnusedScope::try_parse("PUBLIC"), Some(UnusedScope::Public));
assert_eq!(
UnusedScope::try_parse("Function"),
Some(UnusedScope::Function)
);
}
#[test]
fn rejects_unknown_scopes() {
assert_eq!(UnusedScope::try_parse("unknown"), None);
assert_eq!(UnusedScope::try_parse("class"), None);
}
}