|
1 | | -use std::fmt::{Display, Formatter, Debug}; |
| 1 | +use std::fmt::{Debug, Display, Formatter}; |
2 | 2 |
|
3 | 3 | use text_size::TextRange; |
4 | 4 |
|
5 | | -use crate::{categories::RuleCategories, rule::{GroupCategory, Rule, RuleGroup}, RuleFilter}; |
| 5 | +use crate::{ |
| 6 | + categories::RuleCategories, |
| 7 | + rule::{GroupCategory, Rule, RuleGroup}, |
| 8 | +}; |
| 9 | + |
| 10 | +/// Allow filtering a single rule or group of rules by their names |
| 11 | +#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 12 | +pub enum RuleFilter<'a> { |
| 13 | + Group(&'a str), |
| 14 | + Rule(&'a str, &'a str), |
| 15 | +} |
6 | 16 |
|
7 | 17 | /// Allows filtering the list of rules that will be executed in a run of the analyzer, |
8 | 18 | /// and at what source code range signals (diagnostics or actions) may be raised |
@@ -58,8 +68,6 @@ impl<'analysis> AnalysisFilter<'analysis> { |
58 | 68 | } |
59 | 69 | } |
60 | 70 |
|
61 | | - |
62 | | - |
63 | 71 | impl<'a> RuleFilter<'a> { |
64 | 72 | // Returns the group name of this filter. |
65 | 73 | pub fn group(self) -> &'a str { |
@@ -122,3 +130,64 @@ impl<'a> pg_console::fmt::Display for RuleFilter<'a> { |
122 | 130 | } |
123 | 131 | } |
124 | 132 |
|
| 133 | +/// Opaque identifier for a group of rule |
| 134 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 135 | +pub struct GroupKey { |
| 136 | + group: &'static str, |
| 137 | +} |
| 138 | + |
| 139 | +impl GroupKey { |
| 140 | + pub(crate) fn new(group: &'static str) -> Self { |
| 141 | + Self { group } |
| 142 | + } |
| 143 | + |
| 144 | + pub fn group<G: RuleGroup>() -> Self { |
| 145 | + Self::new(G::NAME) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl From<GroupKey> for RuleFilter<'static> { |
| 150 | + fn from(key: GroupKey) -> Self { |
| 151 | + RuleFilter::Group(key.group) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/// Opaque identifier for a single rule |
| 156 | +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 157 | +pub struct RuleKey { |
| 158 | + group: &'static str, |
| 159 | + rule: &'static str, |
| 160 | +} |
| 161 | + |
| 162 | +impl RuleKey { |
| 163 | + pub fn new(group: &'static str, rule: &'static str) -> Self { |
| 164 | + Self { group, rule } |
| 165 | + } |
| 166 | + |
| 167 | + pub fn rule<R: Rule>() -> Self { |
| 168 | + Self::new(<R::Group as RuleGroup>::NAME, R::METADATA.name) |
| 169 | + } |
| 170 | + |
| 171 | + pub fn group(&self) -> &'static str { |
| 172 | + self.group |
| 173 | + } |
| 174 | + |
| 175 | + pub fn rule_name(&self) -> &'static str { |
| 176 | + self.rule |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +impl From<RuleKey> for RuleFilter<'static> { |
| 181 | + fn from(key: RuleKey) -> Self { |
| 182 | + RuleFilter::Rule(key.group, key.rule) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +impl PartialEq<RuleKey> for RuleFilter<'static> { |
| 187 | + fn eq(&self, other: &RuleKey) -> bool { |
| 188 | + match *self { |
| 189 | + RuleFilter::Group(group) => group == other.group, |
| 190 | + RuleFilter::Rule(group, rule) => group == other.group && rule == other.rule, |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments