-
Notifications
You must be signed in to change notification settings - Fork 902
/
violation.rs
85 lines (68 loc) · 2.72 KB
/
violation.rs
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
use std::fmt::{Debug, Display};
#[derive(Debug, Copy, Clone)]
pub enum FixAvailability {
Sometimes,
Always,
None,
}
impl Display for FixAvailability {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FixAvailability::Sometimes => write!(f, "Fix is sometimes available."),
FixAvailability::Always => write!(f, "Fix is always available."),
FixAvailability::None => write!(f, "Fix is not available."),
}
}
}
pub trait Violation: Debug + PartialEq + Eq {
/// `None` in the case an fix is never available or otherwise Some
/// [`FixAvailability`] describing the available fix.
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;
/// The message used to describe the violation.
fn message(&self) -> String;
/// The explanation used in documentation and elsewhere.
fn explanation() -> Option<&'static str> {
None
}
// TODO(micha): Move `fix_title` to `Fix`, add new `advice` method that is shown as an advice.
// Change the `Diagnostic` renderer to show the advice, and render the fix message after the `Suggested fix: <here>`
/// Returns the title for the fix. The message is also shown as an advice as part of the diagnostics.
///
/// Required for rules that have fixes.
fn fix_title(&self) -> Option<String> {
None
}
/// Returns the format strings used by [`message`](Violation::message).
fn message_formats() -> &'static [&'static str];
}
/// This trait exists just to make implementing the [`Violation`] trait more
/// convenient for violations that can always be fixed.
pub trait AlwaysFixableViolation: Debug + PartialEq + Eq {
/// The message used to describe the violation.
fn message(&self) -> String;
/// The explanation used in documentation and elsewhere.
fn explanation() -> Option<&'static str> {
None
}
/// The title displayed for the available fix.
fn fix_title(&self) -> String;
/// Returns the format strings used by
/// [`message`](AlwaysFixableViolation::message).
fn message_formats() -> &'static [&'static str];
}
/// A blanket implementation.
impl<V: AlwaysFixableViolation> Violation for V {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Always;
fn message(&self) -> String {
<Self as AlwaysFixableViolation>::message(self)
}
fn explanation() -> Option<&'static str> {
<Self as AlwaysFixableViolation>::explanation()
}
fn fix_title(&self) -> Option<String> {
Some(<Self as AlwaysFixableViolation>::fix_title(self))
}
fn message_formats() -> &'static [&'static str] {
<Self as AlwaysFixableViolation>::message_formats()
}
}