|
1 |
| -/// Show how to build your own diagnostics, without having to use the |
2 |
| -/// `fancy` feature or having `main()` return `miette::Result` |
3 |
| -use kdl::KdlDocument; |
4 |
| -use miette::Diagnostic; |
5 |
| -use miette::SourceSpan; |
| 1 | +// TODO(@zkat): Error stuff has changed, so this needs updating. |
6 | 2 |
|
7 |
| -#[derive(Debug)] |
8 |
| -pub struct MyError { |
9 |
| - pub message: String, |
10 |
| -} |
| 3 | +// /// Show how to build your own diagnostics, without having to use the |
| 4 | +// /// `fancy` feature or having `main()` return `miette::Result` |
| 5 | +// /// |
| 6 | +// use kdl::KdlDocument; |
| 7 | +// use miette::Diagnostic; |
| 8 | +// use miette::SourceSpan; |
11 | 9 |
|
12 |
| -fn parse(input: &str) -> Result<KdlDocument, MyError> { |
13 |
| - let doc = input.parse::<KdlDocument>(); |
14 |
| - doc.map_err(|error| { |
15 |
| - let source = error |
16 |
| - .source_code() |
17 |
| - .expect("parse errors should have source code"); |
18 |
| - let help = error.help.unwrap_or_default(); |
19 |
| - let span: SourceSpan = error.span; |
20 |
| - let contents = source |
21 |
| - .read_span(&span, 0, 0) |
22 |
| - .expect("source should have span contents"); |
23 |
| - // miette uses 0 based indexes, but humans prefer 1-based |
24 |
| - let line = contents.line() + 1; |
25 |
| - let column = contents.column() + 1; |
26 |
| - let message = format!( |
27 |
| - "line {}, column {}: {}\n help: {}", |
28 |
| - line, column, error, help |
29 |
| - ); |
30 |
| - MyError { message } |
31 |
| - }) |
32 |
| -} |
| 10 | +// #[derive(Debug)] |
| 11 | +// pub struct MyError { |
| 12 | +// pub message: String, |
| 13 | +// } |
| 14 | + |
| 15 | +// fn parse(input: &str) -> Result<KdlDocument, MyError> { |
| 16 | +// let doc = input.parse::<KdlDocument>(); |
| 17 | +// doc.map_err(|error| { |
| 18 | +// let source = error |
| 19 | +// .source_code() |
| 20 | +// .expect("parse errors should have source code"); |
| 21 | +// let help = error.help.unwrap_or_default(); |
| 22 | +// let span: SourceSpan = error.span; |
| 23 | +// let contents = source |
| 24 | +// .read_span(&span, 0, 0) |
| 25 | +// .expect("source should have span contents"); |
| 26 | +// // miette uses 0 based indexes, but humans prefer 1-based |
| 27 | +// let line = contents.line() + 1; |
| 28 | +// let column = contents.column() + 1; |
| 29 | +// let message = format!( |
| 30 | +// "line {}, column {}: {}\n help: {}", |
| 31 | +// line, column, error, help |
| 32 | +// ); |
| 33 | +// MyError { message } |
| 34 | +// }) |
| 35 | +// } |
33 | 36 |
|
34 | 37 | fn main() {
|
35 |
| - let input = r#" |
36 |
| - foo { |
37 |
| - bar { |
38 |
| - baz 1. |
39 |
| - } |
40 |
| - } |
41 |
| - "#; |
42 |
| - let err = parse(input).unwrap_err(); |
43 |
| - eprintln!("{}", err.message); |
44 |
| - // Output: |
45 |
| - // line 4, column 14: Expected valid value. |
46 |
| - // help: Floating point numbers must be base 10, and have numbers after the decimal point. |
| 38 | + // let input = r#" |
| 39 | + // foo { |
| 40 | + // bar { |
| 41 | + // baz 1. |
| 42 | + // } |
| 43 | + // } |
| 44 | + // "#; |
| 45 | + // let err = parse(input).unwrap_err(); |
| 46 | + // eprintln!("{}", err.message); |
| 47 | + // // Output: |
| 48 | + // // line 4, column 14: Expected valid value. |
| 49 | + // // help: Floating point numbers must be base 10, and have numbers after the decimal point. |
47 | 50 | }
|
0 commit comments