use std::str::FromStr;
use crate::error;
use super::{Key, Keys};
pub(crate) mod parser;
impl FromStr for Key {
type Err = error::KeyParse;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parser = parser::Parser::new(s);
let first = parser.next_key()?;
if parser.is_empty() {
Ok(first)
} else {
Err(error::KeyParse::TooManyKeys(s.to_owned()))
}
}
}
impl Key {
pub fn parse_multiple(input: &str) -> Result<Vec<Self>, <Self as FromStr>::Err> {
let mut parser = parser::Parser::new(input);
let mut output = vec![];
while !parser.is_empty() {
output.push(parser.next_key()?);
}
Ok(output)
}
}
impl FromStr for Keys {
type Err = error::KeyParse;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parser = parser::Parser::new(s);
let mut output = vec![];
while !parser.is_empty() {
output.push(parser.next_key()?);
}
Ok(Keys(output))
}
}