@@ -48,6 +48,13 @@ impl Key {
48
48
}
49
49
}
50
50
51
+ /// Parse a TOML key expression
52
+ ///
53
+ /// Unlike `"".parse<Key>()`, this supports dotted keys.
54
+ pub fn parse ( repr : & str ) -> Result < Vec < Self > , parser:: TomlError > {
55
+ Self :: try_parse_path ( repr)
56
+ }
57
+
51
58
pub ( crate ) fn with_repr_unchecked ( mut self , repr : Repr ) -> Self {
52
59
self . repr = Some ( repr) ;
53
60
self
@@ -97,12 +104,12 @@ impl Key {
97
104
self . decor . clear ( ) ;
98
105
}
99
106
100
- fn try_parse ( s : & str ) -> Result < Key , parser:: TomlError > {
107
+ fn try_parse_simple ( s : & str ) -> Result < Key , parser:: TomlError > {
101
108
use combine:: stream:: position:: { IndexPositioner , Positioner } ;
102
109
use combine:: EasyParser ;
103
110
104
111
let b = s. as_bytes ( ) ;
105
- let result = parser:: key_parser ( ) . easy_parse ( Stream :: new ( b) ) ;
112
+ let result = parser:: simple_key ( ) . easy_parse ( Stream :: new ( b) ) ;
106
113
match result {
107
114
Ok ( ( _, ref rest) ) if !rest. input . is_empty ( ) => Err ( parser:: TomlError :: from_unparsed (
108
115
( & rest. positioner
@@ -114,6 +121,24 @@ impl Key {
114
121
Err ( e) => Err ( parser:: TomlError :: new ( e, b) ) ,
115
122
}
116
123
}
124
+
125
+ fn try_parse_path ( s : & str ) -> Result < Vec < Key > , parser:: TomlError > {
126
+ use combine:: stream:: position:: { IndexPositioner , Positioner } ;
127
+ use combine:: EasyParser ;
128
+
129
+ let b = s. as_bytes ( ) ;
130
+ let result = parser:: key_path ( ) . easy_parse ( Stream :: new ( b) ) ;
131
+ match result {
132
+ Ok ( ( _, ref rest) ) if !rest. input . is_empty ( ) => Err ( parser:: TomlError :: from_unparsed (
133
+ ( & rest. positioner
134
+ as & dyn Positioner < usize , Position = usize , Checkpoint = IndexPositioner > )
135
+ . position ( ) ,
136
+ b,
137
+ ) ) ,
138
+ Ok ( ( keys, _) ) => Ok ( keys) ,
139
+ Err ( e) => Err ( parser:: TomlError :: new ( e, b) ) ,
140
+ }
141
+ }
117
142
}
118
143
119
144
impl std:: ops:: Deref for Key {
@@ -158,7 +183,7 @@ impl FromStr for Key {
158
183
/// if fails, tries as basic quoted key (surrounds with "")
159
184
/// and then literal quoted key (surrounds with '')
160
185
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
161
- Key :: try_parse ( s)
186
+ Key :: try_parse_simple ( s)
162
187
}
163
188
}
164
189
0 commit comments