| 1 | //! To run this example, execute the following command from the top level of |
| 2 | //! this repository: |
| 3 | //! |
| 4 | //! ```sh |
| 5 | //! cargo run --example term |
| 6 | //! ``` |
| 7 | |
| 8 | 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#}; |
| 9 | use codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::filesrust-analyzer cargo codespan-reporting 0.12.0 files/::SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#; |
| 10 | use codespan_reportingrust-analyzer cargo codespan-reporting 0.12.0 crate/::termrust-analyzer cargo codespan-reporting 0.12.0 term/; |
| 11 | 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#}; |
| 12 | |
| 13 | #[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:)] |
| 14 | pub struct Optsrust-analyzer cargo codespan-reporting 0.12.0 Opts# { |
| 15 | /// Configure coloring of output |
| 16 | pub colorrust-analyzer cargo codespan-reporting 0.12.0 Opts#color.: ColorChoicerust-analyzer cargo termcolor 1.4.1 ColorChoice#, |
| 17 | } |
| 18 | |
| 19 | 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#> { |
| 20 | 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().(); |
| 21 | let color = pargs |
| 22 | .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(). |
| 23 | .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#); |
| 24 | 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. }) |
| 25 | } |
| 26 | |
| 27 | 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#<()> { |
| 28 | 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().; |
| 29 | |
| 30 | let mut files = SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#::newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]new().(); |
| 31 | |
| 32 | let file_id1 = files.addrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]add().( |
| 33 | "Data/Nat.fun", |
| 34 | unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().( |
| 35 | " |
| 36 | module Data.Nat where |
| 37 | |
| 38 | data Nat : Type where |
| 39 | zero : Nat |
| 40 | succ : Nat → Nat |
| 41 | |
| 42 | {-# BUILTIN NATRAL Nat #-} |
| 43 | |
| 44 | infixl 6 _+_ _-_ |
| 45 | |
| 46 | _+_ : Nat → Nat → Nat |
| 47 | zero + n₂ = n₂ |
| 48 | succ n₁ + n₂ = succ (n₁ + n₂) |
| 49 | |
| 50 | _-_ : Nat → Nat → Nat |
| 51 | n₁ - zero = n₁ |
| 52 | zero - succ n₂ = zero |
| 53 | succ n₁ - succ n₂ = n₁ - n₂ |
| 54 | ", |
| 55 | ), |
| 56 | ); |
| 57 | |
| 58 | let file_id2 = files.addrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]add().( |
| 59 | "Test.fun", |
| 60 | unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().( |
| 61 | r#" |
| 62 | module Test where |
| 63 | |
| 64 | _ : Nat |
| 65 | _ = 123 + "hello" |
| 66 | "#, |
| 67 | ), |
| 68 | ); |
| 69 | |
| 70 | let file_id3 = files.addrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]add().( |
| 71 | "FizzBuzz.fun", |
| 72 | unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().( |
| 73 | r#" |
| 74 | module FizzBuzz where |
| 75 | |
| 76 | fizz₁ : Nat → String |
| 77 | fizz₁ num = case (mod num 5) (mod num 3) of |
| 78 | 0 0 => "FizzBuzz" |
| 79 | 0 _ => "Fizz" |
| 80 | _ 0 => "Buzz" |
| 81 | _ _ => num |
| 82 | |
| 83 | fizz₂ : Nat → String |
| 84 | fizz₂ num = |
| 85 | case (mod num 5) (mod num 3) of |
| 86 | 0 0 => "FizzBuzz" |
| 87 | 0 _ => "Fizz" |
| 88 | _ 0 => "Buzz" |
| 89 | _ _ => num |
| 90 | "#, |
| 91 | ), |
| 92 | ); |
| 93 | |
| 94 | let diagnostics = [ |
| 95 | // Unknown builtin error |
| 96 | Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::errorrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]error().() |
| 97 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().("unknown builtin: `NATRAL`") |
| 98 | .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!![ |
| 99 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().(file_id1, 96..102).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("unknown builtin") |
| 100 | ]) |
| 101 | .with_notesrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_notes().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 102 | "there is a builtin with a similar name: `NATURAL`".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().() |
| 103 | ]), |
| 104 | // Unused parameter warning |
| 105 | Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::warningrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]warning().() |
| 106 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().("unused parameter pattern: `n₂`") |
| 107 | .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!![ |
| 108 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().(file_id1, 285..289).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("unused parameter") |
| 109 | ]) |
| 110 | .with_notesrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_notes().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["consider using a wildcard pattern: `_`".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()]), |
| 111 | // Unexpected type error |
| 112 | Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::errorrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]error().() |
| 113 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().("unexpected type in application of `_+_`") |
| 114 | .with_coderust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_code().("E0001") |
| 115 | .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!![ |
| 116 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().(file_id2, 37..44).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("expected `Nat`, found `String`"), |
| 117 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id1, 130..155) |
| 118 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("based on the definition of `_+_`"), |
| 119 | ]) |
| 120 | .with_notesrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_notes().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().( |
| 121 | " |
| 122 | expected type `Nat` |
| 123 | found type `String` |
| 124 | ", |
| 125 | )]), |
| 126 | // Incompatible match clause error |
| 127 | Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::errorrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]error().() |
| 128 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().("`case` clauses have incompatible types") |
| 129 | .with_coderust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_code().("E0308") |
| 130 | .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!![ |
| 131 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().(file_id3, 163..166).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("expected `String`, found `Nat`"), |
| 132 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 62..166) |
| 133 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("`case` clauses have incompatible types"), |
| 134 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 41..47) |
| 135 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("expected type `String` found here"), |
| 136 | ]) |
| 137 | .with_notesrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_notes().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().( |
| 138 | " |
| 139 | expected type `String` |
| 140 | found type `Nat` |
| 141 | ", |
| 142 | )]), |
| 143 | // Incompatible match clause error |
| 144 | Diagnosticrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Diagnostic#::errorrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]error().() |
| 145 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_message().("`case` clauses have incompatible types") |
| 146 | .with_coderust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_code().("E0308") |
| 147 | .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!![ |
| 148 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::primaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]primary().(file_id3, 328..331).with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("expected `String`, found `Nat`"), |
| 149 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 211..331) |
| 150 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("`case` clauses have incompatible types"), |
| 151 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 258..268) |
| 152 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("this is found to be of type `String`"), |
| 153 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 284..290) |
| 154 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("this is found to be of type `String`"), |
| 155 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 306..312) |
| 156 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("this is found to be of type `String`"), |
| 157 | Labelrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/Label#::secondaryrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]secondary().(file_id3, 186..192) |
| 158 | .with_messagerust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Label<FileId>`]with_message().("expected type `String` found here"), |
| 159 | ]) |
| 160 | .with_notesrust-analyzer cargo codespan-reporting 0.12.0 diagnostic/impl#[`Diagnostic<FileId>`]with_notes().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![unindentrust-analyzer cargo unindent 0.2.4 crate/::unindentrust-analyzer cargo unindent 0.2.4 unindent/unindent().( |
| 161 | " |
| 162 | expected type `String` |
| 163 | found type `Nat` |
| 164 | ", |
| 165 | )]), |
| 166 | ]; |
| 167 | |
| 168 | let writer = StandardStreamrust-analyzer cargo termcolor 1.4.1 StandardStream#::stderrrust-analyzer cargo termcolor 1.4.1 impl#[StandardStream]stderr().(color); |
| 169 | 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().(); |
| 170 | for diagnostic in &diagnostics { |
| 171 | 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, &files, diagnostic)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 172 | } |
| 173 | |
| 174 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 175 | } |