-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathdiagnostics.rs
More file actions
462 lines (415 loc) · 13.8 KB
/
diagnostics.rs
File metadata and controls
462 lines (415 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use pgt_console::markup;
use pgt_diagnostics::adapters::{BpafError, IoError, SerdeJsonError};
use pgt_diagnostics::{
Advices, Category, Diagnostic, Error, LogCategory, MessageAndDescription, Severity, Visit,
};
use pgt_workspace::WorkspaceError;
use std::process::{ExitCode, Termination};
use std::{env::current_exe, fmt::Debug};
fn command_name() -> String {
current_exe()
.ok()
.and_then(|path| Some(path.file_name()?.to_str()?.to_string()))
.unwrap_or_else(|| String::from("postgrestools"))
}
/// A diagnostic that is emitted when running Postgres Tools via CLI.
///
/// When displaying the diagnostic,
#[derive(Debug, Diagnostic)]
pub enum CliDiagnostic {
/// Returned when it is called with a subcommand it doesn't know
UnknownCommand(UnknownCommand),
/// Return by the help command when it is called with a subcommand it doesn't know
UnknownCommandHelp(UnknownCommandHelp),
/// Returned when the value of a command line argument could not be parsed
ParseError(ParseDiagnostic),
/// Returned when the CLI doesn't recognize a command line argument
UnexpectedArgument(UnexpectedArgument),
/// Returned when a required argument is not present in the command line
MissingArgument(MissingArgument),
/// Returned when a subcommand is called without any arguments
EmptyArguments(EmptyArguments),
/// Returned when a subcommand is called with an unsupported combination of arguments
IncompatibleArguments(IncompatibleArguments),
/// Returned by a traversal command when error diagnostics were emitted
CheckError(CheckError),
/// Emitted when a file is fixed, but it still contains diagnostics.
///
/// This happens when these diagnostics come from rules that don't have a code action.
FileCheck(FileCheck),
/// When an argument is higher than the expected maximum
OverflowNumberArgument(OverflowNumberArgument),
/// Wrapper for an underlying pglt-service error
WorkspaceError(WorkspaceError),
/// Wrapper for an underlying `std::io` error
IoError(IoDiagnostic),
/// The daemon is not running
ServerNotRunning(ServerNotRunning),
/// The end configuration (`postgrestools.jsonc` + other options) is incompatible with the command
IncompatibleEndConfiguration(IncompatibleEndConfiguration),
/// No files processed during the file system traversal
NoFilesWereProcessed(NoFilesWereProcessed),
/// Emitted during the reporting phase
Report(ReportDiagnostic),
/// Emitted when there's an error emitted when using stdin mode
Stdin(StdinDiagnostic),
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message(
description = "Unknown command {command_name}",
message("Unknown command "<Emphasis>{self.command_name}</Emphasis>)
),
)]
pub struct UnknownCommand {
command_name: String,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message(
description = "Cannot print help for unknown command {command_name}",
message("Cannot print help for unknown command "<Emphasis>{self.command_name}</Emphasis>)
),
)]
pub struct UnknownCommandHelp {
command_name: String,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
)]
pub struct ParseDiagnostic {
#[message]
#[description]
message: MessageAndDescription,
#[source]
source: Option<Error>,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message(
description = "Unrecognized option {argument}",
message("Unrecognized option "<Emphasis>{self.argument}</Emphasis>".")
),
)]
pub struct UnexpectedArgument {
argument: String,
#[advice]
help: CliAdvice,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message(
description = "Unrecognized option {argument}",
message("Missing argument "<Emphasis>{self.argument}</Emphasis>)
),
)]
pub struct MissingArgument {
argument: String,
#[advice]
advice: CliAdvice,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message = "Empty arguments"
)]
pub struct EmptyArguments;
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message(
description = "Incompatible arguments {first_argument} and {second_argument}",
message("Incompatible arguments "<Emphasis>{self.first_argument}</Emphasis>" and "<Emphasis>{self.second_argument}</Emphasis>)
)
)]
pub struct IncompatibleArguments {
first_argument: String,
second_argument: String,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
severity = Error,
)]
pub struct CheckError {
#[category]
category: &'static Category,
#[message]
message: MessageAndDescription,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
severity = Error,
)]
pub struct FileCheck {
#[message]
#[description]
pub message: MessageAndDescription,
#[location(resource)]
pub file_path: String,
#[category]
pub category: &'static Category,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "flags/invalid",
severity = Error,
message(
description = "The value of the argument {argument} is too high, maximum accepted {maximum}",
message("The value of the argument "<Emphasis>{self.argument}</Emphasis>" is too high, maximum accepted "{{self.maximum}})
)
)]
pub struct OverflowNumberArgument {
argument: String,
maximum: u16,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "internalError/io",
severity = Error,
message = "Errors occurred while executing I/O operations."
)]
pub struct IoDiagnostic {
#[source]
source: Option<Error>,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "internalError/io",
severity = Error,
message = "No running instance of the daemon server was found."
)]
pub struct ServerNotRunning;
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "internalError/io",
severity = Error,
message(
description = "The combination of configuration and arguments is invalid: \n{reason}",
message("The combination of configuration and arguments is invalid: \n"{{&self.reason}})
)
)]
pub struct IncompatibleEndConfiguration {
reason: String,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "internalError/io",
severity = Error,
message = "No files were processed in the specified paths."
)]
pub struct NoFilesWereProcessed;
#[derive(Debug, Diagnostic)]
pub enum ReportDiagnostic {
/// Emitted when trying to serialise the report
Serialization(SerdeJsonError),
}
/// Advices for the [CliDiagnostic]
#[derive(Debug, Default)]
struct CliAdvice {
/// Used to print the help command
sub_command: String,
}
impl CliAdvice {
fn new_with_help(sub_command: impl Into<String>) -> Self {
Self {
sub_command: sub_command.into(),
}
}
}
impl Advices for CliAdvice {
fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
let command_name = command_name();
let help_sub_command = format!("{} {} --help", command_name, &self.sub_command);
visitor.record_log(
LogCategory::Info,
&markup! { "Type the following command for more information" },
)?;
visitor.record_command(&help_sub_command)?;
Ok(())
}
}
impl CliDiagnostic {
/// Returned when a subcommand is called with an unsupported combination of arguments
pub fn incompatible_arguments(
first_argument: impl Into<String>,
second_argument: impl Into<String>,
) -> Self {
Self::IncompatibleArguments(IncompatibleArguments {
first_argument: first_argument.into(),
second_argument: second_argument.into(),
})
}
/// To throw when there's been an error while parsing an argument
pub fn parse_error_bpaf(source: bpaf::ParseFailure) -> Self {
Self::ParseError(ParseDiagnostic {
source: Some(Error::from(BpafError::from(source))),
message: MessageAndDescription::from("Failed to parse CLI arguments.".to_string()),
})
}
/// Returned when it is called with a subcommand it doesn't know
pub fn unknown_command(command: impl Into<String>) -> Self {
Self::UnknownCommand(UnknownCommand {
command_name: command.into(),
})
}
/// Returned when a subcommand is called without any arguments
pub fn empty_arguments() -> Self {
Self::EmptyArguments(EmptyArguments)
}
/// Returned when a required argument is not present in the command line
pub fn missing_argument(argument: impl Into<String>, subcommand: impl Into<String>) -> Self {
Self::MissingArgument(MissingArgument {
argument: argument.into(),
advice: CliAdvice::new_with_help(subcommand),
})
}
/// When no files were processed while traversing the file system
pub fn no_files_processed() -> Self {
Self::NoFilesWereProcessed(NoFilesWereProcessed)
}
/// Returned when the CLI doesn't recognize a command line argument
pub fn unexpected_argument(argument: impl Into<String>, subcommand: impl Into<String>) -> Self {
Self::UnexpectedArgument(UnexpectedArgument {
argument: argument.into(),
help: CliAdvice::new_with_help(subcommand),
})
}
/// When there's been error inside the workspace
pub fn workspace_error(error: WorkspaceError) -> Self {
Self::WorkspaceError(error)
}
/// An I/O error
pub fn io_error(error: std::io::Error) -> Self {
Self::IoError(IoDiagnostic {
source: Some(Error::from(IoError::from(error))),
})
}
/// Emitted when errors were emitted while running `check` command
pub fn check_error(category: &'static Category) -> Self {
Self::CheckError(CheckError {
category,
message: MessageAndDescription::from(
markup! {
"Some "<Emphasis>"errors"</Emphasis>" were emitted while "<Emphasis>"running checks"</Emphasis>"."
}
.to_owned(),
),
})
}
/// Emitted when warnings were emitted while running `check` command
pub fn check_warnings(category: &'static Category) -> Self {
Self::CheckError(CheckError {
category,
message: MessageAndDescription::from(
markup! {
"Some "<Emphasis>"warnings"</Emphasis>" were emitted while "<Emphasis>"running checks"</Emphasis>"."
}
.to_owned(),
),
})
}
/// Emitted when errors were emitted while apply code fixes
pub fn apply_error(category: &'static Category) -> Self {
Self::CheckError(CheckError {
category,
message: MessageAndDescription::from(
markup! {
"Some "<Emphasis>"errors"</Emphasis>" were emitted while "<Emphasis>"applying fixes"</Emphasis>"."
}
.to_owned(),
),
})
}
/// Emitted when warnings were emitted while apply code fixes
pub fn apply_warnings(category: &'static Category) -> Self {
Self::CheckError(CheckError {
category,
message: MessageAndDescription::from(
markup! {
"Some "<Emphasis>"warnings"</Emphasis>" were emitted while "<Emphasis>"running checks"</Emphasis>"."
}
.to_owned(),
),
})
}
pub fn stdin() -> Self {
Self::Stdin(StdinDiagnostic::default())
}
/// Emitted when the server is not running
pub fn server_not_running() -> Self {
Self::ServerNotRunning(ServerNotRunning)
}
/// Emitted when the end configuration (`postgrestools.jsonc` file + CLI arguments + LSP configuration)
/// results in a combination of options that doesn't allow to run the command correctly.
///
/// A reason needs to be provided
pub fn incompatible_end_configuration(reason: impl Into<String>) -> Self {
Self::IncompatibleEndConfiguration(IncompatibleEndConfiguration {
reason: reason.into(),
})
}
/// Emitted when an argument value is greater than the allowed value
pub fn overflown_argument(argument: impl Into<String>, maximum: u16) -> Self {
Self::OverflowNumberArgument(OverflowNumberArgument {
argument: argument.into(),
maximum,
})
}
/// Return by the help command when it is called with a subcommand it doesn't know
pub fn new_unknown_help(command: impl Into<String>) -> Self {
Self::UnknownCommandHelp(UnknownCommandHelp {
command_name: command.into(),
})
}
}
impl From<WorkspaceError> for CliDiagnostic {
fn from(error: WorkspaceError) -> Self {
CliDiagnostic::workspace_error(error)
}
}
impl From<std::io::Error> for CliDiagnostic {
fn from(error: std::io::Error) -> Self {
CliDiagnostic::io_error(error)
}
}
impl Termination for CliDiagnostic {
fn report(self) -> ExitCode {
let severity = self.severity();
if severity >= Severity::Error {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}
}
#[derive(Debug, Default, Diagnostic)]
#[diagnostic(
severity = Error,
category = "stdin",
message = "The contents aren't fixed. Use the `--fix` flag to fix them."
)]
pub struct StdinDiagnostic {}
#[cfg(test)]
mod test {
use crate::CliDiagnostic;
#[test]
fn termination_diagnostic_size() {
assert_eq!(
std::mem::size_of::<CliDiagnostic>(),
96,
"you successfully decreased the size of the diagnostic!"
)
}
}