use crate::decor::InternalString;
use crate::parser;
use combine::stream::position::Stream;
use std::str::FromStr;
#[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> {
use combine::EasyParser;
let result = parser::key_parser().easy_parse(Stream::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,
}
}
pub fn get(&self) -> &str {
&self.key
}
pub fn raw(&self) -> &str {
&self.raw
}
}
#[doc(hidden)]
impl From<Key> for InternalString {
fn from(key: Key) -> InternalString {
key.key
}
}