@@ -7,7 +7,9 @@ use miette::{Severity, SourceSpan};
7
7
8
8
use winnow:: {
9
9
ascii:: { digit1, hex_digit1, oct_digit1, Caseless } ,
10
- combinator:: { alt, cut_err, eof, not, opt, peek, preceded, repeat, repeat_till, terminated} ,
10
+ combinator:: {
11
+ alt, cut_err, eof, fail, not, opt, peek, preceded, repeat, repeat_till, terminated,
12
+ } ,
11
13
error:: {
12
14
AddContext , ContextError , ErrorKind , FromExternalError , FromRecoverableError , ParserError ,
13
15
StrContext , StrContextValue ,
@@ -26,25 +28,16 @@ use crate::{
26
28
type Input < ' a > = Recoverable < Located < & ' a str > , KdlParseError > ;
27
29
type PResult < T > = winnow:: PResult < T , KdlParseError > ;
28
30
29
- impl std:: str:: FromStr for KdlDocument {
30
- type Err = KdlParseFailure ;
31
-
32
- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
33
- let ( maybe_val, errs) = try_parse ( document, s) ;
34
- if let ( Some ( v) , true ) = ( maybe_val, errs. is_empty ( ) ) {
35
- Ok ( v)
36
- } else {
37
- Err ( failure_from_errs ( errs, s) )
38
- }
39
- }
40
- }
41
-
42
31
pub ( crate ) fn try_parse < ' a , P : Parser < Input < ' a > , T , KdlParseError > , T > (
43
32
mut parser : P ,
44
33
input : & ' a str ,
45
- ) -> ( Option < T > , Vec < KdlParseError > ) {
34
+ ) -> Result < T , KdlParseFailure > {
46
35
let ( _, maybe_val, errs) = parser. recoverable_parse ( Located :: new ( input) ) ;
47
- ( maybe_val, errs)
36
+ if let ( Some ( v) , true ) = ( maybe_val, errs. is_empty ( ) ) {
37
+ Ok ( v)
38
+ } else {
39
+ Err ( failure_from_errs ( errs, input) )
40
+ }
48
41
}
49
42
50
43
pub ( crate ) fn failure_from_errs ( errs : Vec < KdlParseError > , input : & str ) -> KdlParseFailure {
@@ -201,7 +194,7 @@ fn new_input(s: &str) -> Input<'_> {
201
194
}
202
195
203
196
/// `document := bom? nodes`
204
- fn document ( input : & mut Input < ' _ > ) -> PResult < KdlDocument > {
197
+ pub ( crate ) fn document ( input : & mut Input < ' _ > ) -> PResult < KdlDocument > {
205
198
let bom = opt ( bom. take ( ) ) . parse_next ( input) ?;
206
199
let mut doc = nodes. parse_next ( input) ?;
207
200
if let Some ( bom) = bom {
@@ -373,7 +366,7 @@ fn final_node(input: &mut Input<'_>) -> PResult<KdlNode> {
373
366
Ok ( node)
374
367
}
375
368
376
- pub ( crate ) fn padded_node_entry ( input : & mut Input < ' _ > ) -> PResult < Option < KdlEntry > > {
369
+ pub ( crate ) fn padded_node_entry ( input : & mut Input < ' _ > ) -> PResult < KdlEntry > {
377
370
let ( ( leading, entry, trailing) , _span) = (
378
371
repeat ( 0 .., line_space) . map ( |_: ( ) | ( ) ) . take ( ) ,
379
372
node_entry,
@@ -383,7 +376,7 @@ pub(crate) fn padded_node_entry(input: &mut Input<'_>) -> PResult<Option<KdlEntr
383
376
)
384
377
. with_span ( )
385
378
. parse_next ( input) ?;
386
- Ok ( entry. map ( |mut val| {
379
+ if let Some ( entry ) = entry. map ( |mut val| {
387
380
if let Some ( fmt) = val. format_mut ( ) {
388
381
fmt. leading = format ! ( "{leading}{}" , fmt. leading) ;
389
382
fmt. trailing = format ! ( "{}{trailing}" , fmt. trailing) ;
@@ -393,7 +386,11 @@ pub(crate) fn padded_node_entry(input: &mut Input<'_>) -> PResult<Option<KdlEntr
393
386
val. span = _span. into ( ) ;
394
387
}
395
388
val
396
- } ) )
389
+ } ) {
390
+ Ok ( entry)
391
+ } else {
392
+ fail. parse_next ( input) ?
393
+ }
397
394
}
398
395
399
396
/// `node-prop-or-arg := prop | value`
0 commit comments