#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Key {
Name(String),
Occurrence(String, usize),
}
impl Key {
pub fn name(&self) -> &str {
match self {
Key::Name(n) | Key::Occurrence(n, _) => n,
}
}
pub fn occurrence(&self) -> Option<usize> {
match self {
Key::Name(_) => None,
Key::Occurrence(_, n) => Some(*n),
}
}
}
impl From<&str> for Key {
fn from(name: &str) -> Self {
Key::Name(name.to_string())
}
}
impl From<String> for Key {
fn from(name: String) -> Self {
Key::Name(name)
}
}
impl From<&String> for Key {
fn from(name: &String) -> Self {
Key::Name(name.clone())
}
}
impl From<(&str, usize)> for Key {
fn from((name, n): (&str, usize)) -> Self {
Key::Occurrence(name.to_string(), n)
}
}
impl From<(String, usize)> for Key {
fn from((name, n): (String, usize)) -> Self {
Key::Occurrence(name, n)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conversions_and_accessors() {
let k: Key = "GAIN".into();
assert_eq!(k, Key::Name("GAIN".to_string()));
assert_eq!(k.name(), "GAIN");
assert_eq!(k.occurrence(), None);
let k: Key = ("GAIN".to_string()).into();
assert_eq!(k.name(), "GAIN");
let k: Key = (&"GAIN".to_string()).into();
assert_eq!(k.name(), "GAIN");
let k: Key = ("GAIN", 1).into();
assert_eq!(k, Key::Occurrence("GAIN".to_string(), 1));
assert_eq!(k.name(), "GAIN");
assert_eq!(k.occurrence(), Some(1));
let k: Key = ("GAIN".to_string(), 2).into();
assert_eq!(k.occurrence(), Some(2));
}
}