Yet another refactor to let us implement the many-to-many mapping
between codes and rules in a prefix-agnostic way.
We want to break up the RuleCodePrefix[1] enum into smaller enums.
To facilitate that this commit introduces a new wrapping type around
RuleCodePrefix so that we can start breaking it apart.
[1]: Actually `RuleCodePrefix` is the previous name of the autogenerated
enum ... I renamed it in b19258a243 to
RuleSelector since `ALL` isn't a prefix. This commit now renames it back
but only because the new `RuleSelector` wrapper type, introduced in this
commit, will let us move the `ALL` variant from `RuleCodePrefix` to
`RuleSelector` in the next commit.
69 lines
1.7 KiB
Rust
69 lines
1.7 KiB
Rust
use std::str::FromStr;
|
|
|
|
use schemars::JsonSchema;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::registry::{Rule, RuleCodePrefix};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
pub struct RuleSelector(RuleCodePrefix);
|
|
|
|
impl FromStr for RuleSelector {
|
|
type Err = strum::ParseError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
Ok(Self(RuleCodePrefix::from_str(s)?))
|
|
}
|
|
}
|
|
|
|
impl From<RuleCodePrefix> for RuleSelector {
|
|
fn from(prefix: RuleCodePrefix) -> Self {
|
|
Self(prefix)
|
|
}
|
|
}
|
|
|
|
impl IntoIterator for &RuleSelector {
|
|
type IntoIter = ::std::vec::IntoIter<Self::Item>;
|
|
type Item = Rule;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
self.0.into_iter()
|
|
}
|
|
}
|
|
|
|
/// A const alternative to the `impl From<RuleCodePrefix> for RuleSelector`
|
|
// to let us keep the fields of RuleSelector private.
|
|
// Note that Rust doesn't yet support `impl const From<RuleCodePrefix> for
|
|
// RuleSelector` (see https://github.com/rust-lang/rust/issues/67792).
|
|
// TODO(martin): Remove once RuleSelector is an enum with Linter & Rule variants
|
|
pub(crate) const fn prefix_to_selector(prefix: RuleCodePrefix) -> RuleSelector {
|
|
RuleSelector(prefix)
|
|
}
|
|
|
|
impl JsonSchema for RuleSelector {
|
|
fn schema_name() -> String {
|
|
"RuleSelector".to_string()
|
|
}
|
|
|
|
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
|
<RuleCodePrefix as JsonSchema>::json_schema(gen)
|
|
}
|
|
}
|
|
|
|
impl RuleSelector {
|
|
pub(crate) fn specificity(&self) -> Specificity {
|
|
self.0.specificity()
|
|
}
|
|
}
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
pub(crate) enum Specificity {
|
|
All,
|
|
Linter,
|
|
Code1Char,
|
|
Code2Chars,
|
|
Code3Chars,
|
|
Code4Chars,
|
|
Code5Chars,
|
|
}
|