I have an enum, which contains values and stores in data in custom format.
I can convert it from the string manually, even without regex
, but I'm interested, if it is possible to do the same with the match
expression and, hopefully, with full enum variants completion check?
#[derive(Debug)]
pub enum NoteNotations {
Voice(u8),
}
impl ToString for NoteNotations {
fn to_string(&self) -> String {
match self {
Self::Voice(idx) => format!("voice:{}", idx),
}
}
}
impl FromStr for NoteNotations {
type Err = Box<dyn Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("voice") {
let idx: u8 = s.split(":").collect::<Vec<&str>>()[1].parse()?;
Ok(Self::Voice(idx))
} else {
Err(format!("Can not parse {}", s).into())
}
}
}
P.S. Also, I'm not sure in Err
type for FromStr
implementation