1
- use crate :: CargoResult ;
2
-
3
1
type Span = std:: ops:: Range < usize > ;
4
2
5
3
#[ derive( Debug ) ]
@@ -22,7 +20,7 @@ pub struct ScriptSource<'s> {
22
20
}
23
21
24
22
impl < ' s > ScriptSource < ' s > {
25
- pub fn parse ( raw : & ' s str ) -> CargoResult < Self > {
23
+ pub fn parse ( raw : & ' s str ) -> Result < Self , FrontmatterError > {
26
24
use winnow:: stream:: FindSlice as _;
27
25
use winnow:: stream:: Location as _;
28
26
use winnow:: stream:: Offset as _;
@@ -67,9 +65,9 @@ impl<'s> ScriptSource<'s> {
67
65
}
68
66
1 | 2 => {
69
67
// either not a frontmatter or invalid frontmatter opening
70
- anyhow :: bail !(
68
+ return Err ( FrontmatterError :: new ( format ! (
71
69
"found {fence_length} `{FENCE_CHAR}` in rust frontmatter, expected at least 3"
72
- )
70
+ ) ) ) ;
73
71
}
74
72
_ => { }
75
73
}
@@ -78,7 +76,9 @@ impl<'s> ScriptSource<'s> {
78
76
let open_end = input. current_token_start ( ) ;
79
77
source. open = Some ( open_start..open_end) ;
80
78
let Some ( info_nl) = input. find_slice ( "\n " ) else {
81
- anyhow:: bail!( "no closing `{fence_pattern}` found for frontmatter" ) ;
79
+ return Err ( FrontmatterError :: new ( format ! (
80
+ "no closing `{fence_pattern}` found for frontmatter"
81
+ ) ) ) ;
82
82
} ;
83
83
let info = input. next_slice ( info_nl. start ) ;
84
84
let info = info. trim_matches ( is_whitespace) ;
@@ -91,7 +91,9 @@ impl<'s> ScriptSource<'s> {
91
91
// Ends with a line that starts with a matching number of `-` only followed by whitespace
92
92
let nl_fence_pattern = format ! ( "\n {fence_pattern}" ) ;
93
93
let Some ( frontmatter_nl) = input. find_slice ( nl_fence_pattern. as_str ( ) ) else {
94
- anyhow:: bail!( "no closing `{fence_pattern}` found for frontmatter" ) ;
94
+ return Err ( FrontmatterError :: new ( format ! (
95
+ "no closing `{fence_pattern}` found for frontmatter"
96
+ ) ) ) ;
95
97
} ;
96
98
let frontmatter_start = input. current_token_start ( ) + 1 ; // skip nl from infostring
97
99
let _ = input. next_slice ( frontmatter_nl. start + 1 ) ;
@@ -111,14 +113,18 @@ impl<'s> ScriptSource<'s> {
111
113
let after_closing_fence = after_closing_fence. trim_matches ( is_whitespace) ;
112
114
if !after_closing_fence. is_empty ( ) {
113
115
// extra characters beyond the original fence pattern, even if they are extra `-`
114
- anyhow:: bail!( "trailing characters found after frontmatter close" ) ;
116
+ return Err ( FrontmatterError :: new ( format ! (
117
+ "trailing characters found after frontmatter close"
118
+ ) ) ) ;
115
119
}
116
120
117
121
source. content = content_start..content_end;
118
122
119
123
let repeat = Self :: parse ( source. content ( ) ) ?;
120
124
if repeat. frontmatter . is_some ( ) {
121
- anyhow:: bail!( "only one frontmatter is supported" ) ;
125
+ return Err ( FrontmatterError :: new ( format ! (
126
+ "only one frontmatter is supported"
127
+ ) ) ) ;
122
128
}
123
129
124
130
Ok ( source)
@@ -232,6 +238,27 @@ fn is_whitespace(c: char) -> bool {
232
238
)
233
239
}
234
240
241
+ #[ derive( Debug ) ]
242
+ pub struct FrontmatterError {
243
+ message : String ,
244
+ }
245
+
246
+ impl FrontmatterError {
247
+ pub fn new ( message : impl Into < String > ) -> Self {
248
+ Self {
249
+ message : message. into ( ) ,
250
+ }
251
+ }
252
+ }
253
+
254
+ impl std:: fmt:: Display for FrontmatterError {
255
+ fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
256
+ self . message . fmt ( fmt)
257
+ }
258
+ }
259
+
260
+ impl std:: error:: Error for FrontmatterError { }
261
+
235
262
#[ cfg( test) ]
236
263
mod test {
237
264
use snapbox:: assert_data_eq;
0 commit comments