use std::str::FromStr;
use decor::InternalString;
use parser;
use combine;
use combine::Parser;
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)]
pub struct Key {
key: InternalString,
raw: InternalString,
}
impl FromStr for Key {
type Err = parser::TomlError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let basic = format!("\"{}\"", s);
let literal = format!("'{}'", s);
Key::try_parse(s)
.or_else(|_| Key::try_parse(&basic))
.or_else(|_| Key::try_parse(&literal))
}
}
impl Key {
fn try_parse(s: &str) -> Result<Key, parser::TomlError> {
let result = parser::key().parse(combine::State::new(s));
match result {
Ok((_, ref rest)) if !rest.input.is_empty() => {
Err(parser::TomlError::from_unparsed(rest.positioner, s))
}
Ok(((raw, key), _)) => Ok(Key::new(raw, key)),
Err(e) => Err(parser::TomlError::new(e, s)),
}
}
pub(crate) fn new(raw: &str, key: InternalString) -> Self {
Self {
raw: raw.into(),
key: key,
}
}
pub fn get(&self) -> &str {
&self.key
}
pub fn raw(&self) -> &str {
&self.raw
}
}
#[doc(hidden)]
impl Into<InternalString> for Key {
fn into(self) -> InternalString {
self.key
}
}