1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::decor::InternalString;
use crate::parser;
use combine::stream::state::State;
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::Parser;
let result = parser::key_parser().easy_parse(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,
}
}
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
}
}