Debian Codegraph @

examples/reusable_diagnostic.rs

raw symbols SCIP
1 use codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/::{Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#, Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#};
2 use codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::filesrust-analyzer cargo codespan-reporting 0.12.0 files/::SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#;
3 use codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::termrust-analyzer cargo codespan-reporting 0.12.0 term/::termcolorrust-analyzer cargo termcolor 1.4.1 crate/::{ColorChoicerust-analyzer cargo termcolor 1.4.1 ColorChoice#, StandardStreamrust-analyzer cargo termcolor 1.4.1 StandardStream#};
4 use codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::termrust-analyzer cargo codespan-reporting 0.12.0 term/::{selfrust-analyzer cargo codespan-reporting 0.12.0 term/};
5 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#;
6
7 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
8 pub struct Optsrust-analyzer cargo codespan-reporting 0.12.0 Opts# {
9 colorrust-analyzer cargo codespan-reporting 0.12.0 Opts#color.: ColorChoicerust-analyzer cargo termcolor 1.4.1 ColorChoice#,
10 }
11
12 fn parse_argsrust-analyzer cargo codespan-reporting 0.12.0 parse_args().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optsrust-analyzer cargo codespan-reporting 0.12.0 Opts#, pico_argsrust-analyzer cargo pico-args 0.5.0 crate/::Errorrust-analyzer cargo pico-args 0.5.0 Error#> {
13 let mut pargs = pico_argsrust-analyzer cargo pico-args 0.5.0 crate/::Argumentsrust-analyzer cargo pico-args 0.5.0 Arguments#::from_envrust-analyzer cargo pico-args 0.5.0 impl#[Arguments]from_env().();
14 let color = pargs
15 .opt_value_from_strrust-analyzer cargo pico-args 0.5.0 impl#[Arguments]opt_value_from_str().("--color")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
16 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(ColorChoicerust-analyzer cargo termcolor 1.4.1 ColorChoice#::Autorust-analyzer cargo termcolor 1.4.1 ColorChoice#Auto#);
17 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Optsrust-analyzer cargo codespan-reporting 0.12.0 Opts# { colorrust-analyzer cargo codespan-reporting 0.12.0 Opts#color. })
18 }
19
20 fn mainrust-analyzer cargo codespan-reporting 0.12.0 main().() -> anyhowrust-analyzer cargo anyhow 1.0.0 crate/::Resultrust-analyzer cargo anyhow 1.0.0 Result#<()> {
21 let file = SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#::newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]new().(
22 "main.rs",
23 unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().(
24 r#"
25 fn main() {
26 let foo: i32 = "hello, world";
27 foo += 1;
28 }
29 "#,
30 ),
31 );
32
33 let errors = [
34 Errorrust-analyzer cargo codespan-reporting 0.12.0 Error#::MismatchTyperust-analyzer cargo codespan-reporting 0.12.0 Error#MismatchType#(
35 Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#::newrust-analyzer cargo codespan-reporting 0.12.0 impl#[Item]new().(20..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#23, "i32"),
36 Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#::newrust-analyzer cargo codespan-reporting 0.12.0 impl#[Item]new().(31..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#45, "\"hello, world\""),
37 ),
38 Errorrust-analyzer cargo codespan-reporting 0.12.0 Error#::MutatingImmutablerust-analyzer cargo codespan-reporting 0.12.0 Error#MutatingImmutable#(Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#::newrust-analyzer cargo codespan-reporting 0.12.0 impl#[Item]new().(20..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#23, "foo"), Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#::newrust-analyzer cargo codespan-reporting 0.12.0 impl#[Item]new().(51..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#59, "foo += 1")),
39 ];
40
41 let Optsrust-analyzer cargo codespan-reporting 0.12.0 Opts# { colorrust-analyzer cargo codespan-reporting 0.12.0 Opts#color. } = parse_argsrust-analyzer cargo codespan-reporting 0.12.0 parse_args().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
42 let writer = StandardStreamrust-analyzer cargo termcolor 1.4.1 StandardStream#::stderrrust-analyzer cargo termcolor 1.4.1 impl#[StandardStream]stderr().(color);
43 let config = codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::termrust-analyzer cargo codespan-reporting 0.12.0 term/::Configrust-analyzer cargo codespan-reporting 0.12.0 term/config/Config#::defaultrust-analyzer cargo codespan-reporting 0.12.0 term/config/impl#[Config][Default]default().();
44 for diagnostic in errors.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Errorrust-analyzer cargo codespan-reporting 0.12.0 Error#::reportrust-analyzer cargo codespan-reporting 0.12.0 impl#[Error]report().) {
45 termrust-analyzer cargo codespan-reporting 0.12.0 term/::emitrust-analyzer cargo codespan-reporting 0.12.0 term/emit().(&mut writer.lockrust-analyzer cargo termcolor 1.4.1 impl#[StandardStream]lock().(), &config, &file, &diagnostic)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
46 }
47
48 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
49 }
50
51 /// An error enum that represent all possible errors within your program
52 enum Errorrust-analyzer cargo codespan-reporting 0.12.0 Error# {
53 MismatchTyperust-analyzer cargo codespan-reporting 0.12.0 Error#MismatchType#(Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#, Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#),
54 MutatingImmutablerust-analyzer cargo codespan-reporting 0.12.0 Error#MutatingImmutable#(Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#, Itemrust-analyzer cargo codespan-reporting 0.12.0 Item#),
55 }
56
57 impl Errorrust-analyzer cargo codespan-reporting 0.12.0 Error# {
58 fn reportrust-analyzer cargo codespan-reporting 0.12.0 impl#[Error]report().(&self) -> Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#<()> {
59 match self {
60 Errorrust-analyzer cargo codespan-reporting 0.12.0 Error#::MismatchTyperust-analyzer cargo codespan-reporting 0.12.0 Error#MismatchType#(left, right) => Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::errorrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]error().()
61 .with_coderust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_code().("E0308")
62 .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().("mismatch types")
63 .with_labelsrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_labels().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
64 Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().((), right.rangerust-analyzer cargo codespan-reporting 0.12.0 Item#range..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`][Clone]clone().()).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
65 "Expected `{}`, found: `{}`",
66 left.contentrust-analyzer cargo codespan-reporting 0.12.0 Item#content., right.contentrust-analyzer cargo codespan-reporting 0.12.0 Item#content.,
67 )),
68 Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().((), left.rangerust-analyzer cargo codespan-reporting 0.12.0 Item#range..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`][Clone]clone().()).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("expected due to this"),
69 ]),
70 Errorrust-analyzer cargo codespan-reporting 0.12.0 Error#::MutatingImmutablerust-analyzer cargo codespan-reporting 0.12.0 Error#MutatingImmutable#(original, mutating) => Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::errorrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]error().()
71 .with_coderust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_code().("E0384")
72 .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
73 "cannot mutate immutable variable `{}`",
74 original.contentrust-analyzer cargo codespan-reporting 0.12.0 Item#content.,
75 ))
76 .with_labelsrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_labels().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
77 Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().((), original.rangerust-analyzer cargo codespan-reporting 0.12.0 Item#range..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`][Clone]clone().()).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().(unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().(
78 &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
79 r#"
80 first assignment to `{0}`
81 help: make this binding mutable: `mut {0}`
82 "#,
83 original.contentrust-analyzer cargo codespan-reporting 0.12.0 Item#content.,
84 ),
85 )),
86 Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().((), mutating.rangerust-analyzer cargo codespan-reporting 0.12.0 Item#range..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`][Clone]clone().())
87 .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("cannot assign twice to immutable variable"),
88 ]),
89 }
90 }
91 }
92
93 /// An item in the source code to be used in the `Error` enum.
94 /// In a more complex program it could also contain a `files::FileId` to handle errors that occur inside multiple files.
95 struct Itemrust-analyzer cargo codespan-reporting 0.12.0 Item# {
96 rangerust-analyzer cargo codespan-reporting 0.12.0 Item#range.: Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>,
97 contentrust-analyzer cargo codespan-reporting 0.12.0 Item#content.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
98 }
99
100 impl Itemrust-analyzer cargo codespan-reporting 0.12.0 Item# {
101 fn newrust-analyzer cargo codespan-reporting 0.12.0 impl#[Item]new().(range: Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, content: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Itemrust-analyzer cargo codespan-reporting 0.12.0 Item# {
102 let content = content.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
103 Itemrust-analyzer cargo codespan-reporting 0.12.0 Item# { rangerust-analyzer cargo codespan-reporting 0.12.0 Item#range., contentrust-analyzer cargo codespan-reporting 0.12.0 Item#content. }
104 }
105 }