Skip to content

Commit 0e32289

Browse files
committed
refactor: config codegen
1 parent 9952744 commit 0e32289

File tree

11 files changed

+886
-646
lines changed

11 files changed

+886
-646
lines changed

crates/pgls_configuration/src/generated.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/pgls_configuration/src/generated/linter.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

crates/pgls_configuration/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@
22
//!
33
//! The configuration is divided by "tool".
44
5-
pub mod analyser;
65
pub mod database;
76
pub mod diagnostics;
87
pub mod files;
9-
pub mod generated;
8+
pub mod linter;
109
pub mod migrations;
1110
pub mod plpgsql_check;
11+
pub mod rules;
1212
pub mod typecheck;
1313
pub mod vcs;
1414

1515
pub use crate::diagnostics::ConfigurationDiagnostic;
1616

1717
use std::path::PathBuf;
1818

19-
pub use crate::generated::push_to_analyser_rules;
2019
use crate::vcs::{PartialVcsConfiguration, VcsConfiguration, partial_vcs_configuration};
21-
pub use analyser::{
22-
LinterConfiguration, PartialLinterConfiguration, RuleConfiguration, RuleFixConfiguration,
23-
RulePlainConfiguration, RuleSelector, RuleWithFixOptions, RuleWithOptions, Rules,
24-
partial_linter_configuration,
25-
};
2620
use biome_deserialize::StringSet;
2721
use biome_deserialize_macros::{Merge, Partial};
2822
use bpaf::Bpaf;
2923
use database::{
3024
DatabaseConfiguration, PartialDatabaseConfiguration, partial_database_configuration,
3125
};
3226
use files::{FilesConfiguration, PartialFilesConfiguration, partial_files_configuration};
27+
pub use linter::{
28+
LinterConfiguration, PartialLinterConfiguration, Rules, partial_linter_configuration,
29+
push_to_analyser_rules,
30+
};
3331
use migrations::{
3432
MigrationsConfiguration, PartialMigrationsConfiguration, partial_migrations_configuration,
3533
};
@@ -38,6 +36,10 @@ use plpgsql_check::{
3836
PartialPlPgSqlCheckConfiguration, PlPgSqlCheckConfiguration,
3937
partial_pl_pg_sql_check_configuration,
4038
};
39+
pub use rules::{
40+
RuleConfiguration, RuleFixConfiguration, RulePlainConfiguration, RuleSelector,
41+
RuleWithFixOptions, RuleWithOptions,
42+
};
4143
use serde::{Deserialize, Serialize};
4244
pub use typecheck::{
4345
PartialTypecheckConfiguration, TypecheckConfiguration, partial_typecheck_configuration,

crates/pgls_configuration/src/analyser/linter/mod.rs renamed to crates/pgls_configuration/src/linter/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
mod rules;
1+
//! Generated file, do not edit by hand, see `xtask/codegen`
22
3+
#![doc = r" Generated file, do not edit by hand, see `xtask/codegen`"]
4+
mod rules;
35
use biome_deserialize::StringSet;
46
use biome_deserialize_macros::{Merge, Partial};
57
use bpaf::Bpaf;
68
pub use rules::*;
79
use serde::{Deserialize, Serialize};
8-
910
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
1011
#[partial(derive(Bpaf, Clone, Eq, Merge, PartialEq))]
1112
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
1213
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
1314
pub struct LinterConfiguration {
14-
/// if `false`, it disables the feature and the linter won't be executed. `true` by default
15+
#[doc = r" if `false`, it disables the feature and the linter won't be executed. `true` by default"]
1516
#[partial(bpaf(hide))]
1617
pub enabled: bool,
17-
18-
/// List of rules
18+
#[doc = r" List of rules"]
1919
#[partial(bpaf(pure(Default::default()), optional, hide))]
2020
pub rules: Rules,
21-
22-
/// A list of Unix shell style patterns. The formatter will ignore files/folders that will
23-
/// match these patterns.
21+
#[doc = r" A list of Unix shell style patterns. The formatter will ignore files/folders that will"]
22+
#[doc = r" match these patterns."]
2423
#[partial(bpaf(hide))]
2524
pub ignore: StringSet,
26-
27-
/// A list of Unix shell style patterns. The formatter will include files/folders that will
28-
/// match these patterns.
25+
#[doc = r" A list of Unix shell style patterns. The formatter will include files/folders that will"]
26+
#[doc = r" match these patterns."]
2927
#[partial(bpaf(hide))]
3028
pub include: StringSet,
3129
}
32-
3330
impl LinterConfiguration {
3431
pub const fn is_disabled(&self) -> bool {
3532
!self.enabled
3633
}
3734
}
38-
3935
impl Default for LinterConfiguration {
4036
fn default() -> Self {
4137
Self {
@@ -46,12 +42,10 @@ impl Default for LinterConfiguration {
4642
}
4743
}
4844
}
49-
5045
impl PartialLinterConfiguration {
5146
pub const fn is_disabled(&self) -> bool {
5247
matches!(self.enabled, Some(false))
5348
}
54-
5549
pub fn get_rules(&self) -> Rules {
5650
self.rules.clone().unwrap_or_default()
5751
}

crates/pgls_configuration/src/analyser/linter/rules.rs renamed to crates/pgls_configuration/src/linter/rules.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Generated file, do not edit by hand, see `xtask/codegen`
22
3-
use crate::analyser::{RuleConfiguration, RulePlainConfiguration};
3+
#![doc = r" Generated file, do not edit by hand, see `xtask/codegen`"]
4+
use crate::rules::{RuleConfiguration, RulePlainConfiguration};
45
use biome_deserialize_macros::Merge;
56
use pgls_analyse::{RuleFilter, options::RuleOptions};
67
use pgls_diagnostics::{Category, Severity};
@@ -902,6 +903,22 @@ impl Safety {
902903
}
903904
}
904905
}
906+
#[doc = r" Push the configured rules to the analyser"]
907+
pub fn push_to_analyser_rules(
908+
rules: &Rules,
909+
metadata: &pgls_analyse::MetadataRegistry,
910+
analyser_rules: &mut pgls_analyse::AnalyserRules,
911+
) {
912+
if let Some(rules) = rules.safety.as_ref() {
913+
for rule_name in Safety::GROUP_RULES {
914+
if let Some((_, Some(rule_options))) = rules.get_rule_configuration(rule_name) {
915+
if let Some(rule_key) = metadata.find_rule("safety", rule_name) {
916+
analyser_rules.push_rule(rule_key, rule_options);
917+
}
918+
}
919+
}
920+
}
921+
}
905922
#[test]
906923
fn test_order() {
907924
for items in Safety::GROUP_RULES.windows(2) {

crates/pgls_configuration/src/analyser/mod.rs renamed to crates/pgls_configuration/src/rules/configuration.rs

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
pub mod linter;
2-
3-
pub use crate::analyser::linter::*;
41
use biome_deserialize::Merge;
52
use biome_deserialize_macros::Deserializable;
6-
use pgls_analyse::RuleFilter;
73
use pgls_analyse::options::RuleOptions;
84
use pgls_diagnostics::Severity;
95
#[cfg(feature = "schema")]
106
use schemars::JsonSchema;
117
use serde::{Deserialize, Serialize};
12-
use std::str::FromStr;
138

149
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
1510
#[cfg_attr(feature = "schema", derive(JsonSchema))]
@@ -300,90 +295,3 @@ impl<T: Default> Merge for RuleWithFixOptions<T> {
300295
self.options = other.options;
301296
}
302297
}
303-
304-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
305-
pub enum RuleSelector {
306-
Group(linter::RuleGroup),
307-
Rule(linter::RuleGroup, &'static str),
308-
}
309-
310-
impl From<RuleSelector> for RuleFilter<'static> {
311-
fn from(value: RuleSelector) -> Self {
312-
match value {
313-
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
314-
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
315-
}
316-
}
317-
}
318-
319-
impl<'a> From<&'a RuleSelector> for RuleFilter<'static> {
320-
fn from(value: &'a RuleSelector) -> Self {
321-
match value {
322-
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
323-
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
324-
}
325-
}
326-
}
327-
328-
impl FromStr for RuleSelector {
329-
type Err = &'static str;
330-
fn from_str(selector: &str) -> Result<Self, Self::Err> {
331-
let selector = selector.strip_prefix("lint/").unwrap_or(selector);
332-
if let Some((group_name, rule_name)) = selector.split_once('/') {
333-
let group = linter::RuleGroup::from_str(group_name)?;
334-
if let Some(rule_name) = Rules::has_rule(group, rule_name) {
335-
Ok(RuleSelector::Rule(group, rule_name))
336-
} else {
337-
Err("This rule doesn't exist.")
338-
}
339-
} else {
340-
match linter::RuleGroup::from_str(selector) {
341-
Ok(group) => Ok(RuleSelector::Group(group)),
342-
Err(_) => Err(
343-
"This group doesn't exist. Use the syntax `<group>/<rule>` to specify a rule.",
344-
),
345-
}
346-
}
347-
}
348-
}
349-
350-
impl serde::Serialize for RuleSelector {
351-
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
352-
match self {
353-
RuleSelector::Group(group) => serializer.serialize_str(group.as_str()),
354-
RuleSelector::Rule(group, rule_name) => {
355-
let group_name = group.as_str();
356-
serializer.serialize_str(&format!("{group_name}/{rule_name}"))
357-
}
358-
}
359-
}
360-
}
361-
362-
impl<'de> serde::Deserialize<'de> for RuleSelector {
363-
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
364-
struct Visitor;
365-
impl serde::de::Visitor<'_> for Visitor {
366-
type Value = RuleSelector;
367-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
368-
formatter.write_str("<group>/<ruyle_name>")
369-
}
370-
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
371-
match RuleSelector::from_str(v) {
372-
Ok(result) => Ok(result),
373-
Err(error) => Err(serde::de::Error::custom(error)),
374-
}
375-
}
376-
}
377-
deserializer.deserialize_str(Visitor)
378-
}
379-
}
380-
381-
#[cfg(feature = "schema")]
382-
impl schemars::JsonSchema for RuleSelector {
383-
fn schema_name() -> String {
384-
"RuleCode".to_string()
385-
}
386-
fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
387-
String::json_schema(r#gen)
388-
}
389-
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub(crate) mod configuration;
2+
pub(crate) mod selector;
3+
4+
pub use configuration::{
5+
RuleAssistConfiguration, RuleAssistPlainConfiguration, RuleAssistWithOptions,
6+
RuleConfiguration, RuleFixConfiguration, RulePlainConfiguration, RuleWithFixOptions,
7+
RuleWithOptions,
8+
};
9+
pub use selector::RuleSelector;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use pgls_analyse::RuleFilter;
2+
3+
use std::str::FromStr;
4+
5+
use crate::{Rules, linter::RuleGroup};
6+
7+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
8+
pub enum RuleSelector {
9+
Group(RuleGroup),
10+
Rule(RuleGroup, &'static str),
11+
}
12+
13+
impl From<RuleSelector> for RuleFilter<'static> {
14+
fn from(value: RuleSelector) -> Self {
15+
match value {
16+
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
17+
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
18+
}
19+
}
20+
}
21+
22+
impl<'a> From<&'a RuleSelector> for RuleFilter<'static> {
23+
fn from(value: &'a RuleSelector) -> Self {
24+
match value {
25+
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
26+
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
27+
}
28+
}
29+
}
30+
31+
impl FromStr for RuleSelector {
32+
type Err = &'static str;
33+
fn from_str(selector: &str) -> Result<Self, Self::Err> {
34+
let selector = selector.strip_prefix("lint/").unwrap_or(selector);
35+
if let Some((group_name, rule_name)) = selector.split_once('/') {
36+
let group = RuleGroup::from_str(group_name)?;
37+
if let Some(rule_name) = Rules::has_rule(group, rule_name) {
38+
Ok(RuleSelector::Rule(group, rule_name))
39+
} else {
40+
Err("This rule doesn't exist.")
41+
}
42+
} else {
43+
match RuleGroup::from_str(selector) {
44+
Ok(group) => Ok(RuleSelector::Group(group)),
45+
Err(_) => Err(
46+
"This group doesn't exist. Use the syntax `<group>/<rule>` to specify a rule.",
47+
),
48+
}
49+
}
50+
}
51+
}
52+
53+
impl serde::Serialize for RuleSelector {
54+
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
55+
match self {
56+
RuleSelector::Group(group) => serializer.serialize_str(group.as_str()),
57+
RuleSelector::Rule(group, rule_name) => {
58+
let group_name = group.as_str();
59+
serializer.serialize_str(&format!("{group_name}/{rule_name}"))
60+
}
61+
}
62+
}
63+
}
64+
65+
impl<'de> serde::Deserialize<'de> for RuleSelector {
66+
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
67+
struct Visitor;
68+
impl serde::de::Visitor<'_> for Visitor {
69+
type Value = RuleSelector;
70+
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
71+
formatter.write_str("<group>/<ruyle_name>")
72+
}
73+
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
74+
match RuleSelector::from_str(v) {
75+
Ok(result) => Ok(result),
76+
Err(error) => Err(serde::de::Error::custom(error)),
77+
}
78+
}
79+
}
80+
deserializer.deserialize_str(Visitor)
81+
}
82+
}
83+
84+
#[cfg(feature = "schema")]
85+
impl schemars::JsonSchema for RuleSelector {
86+
fn schema_name() -> String {
87+
"RuleCode".to_string()
88+
}
89+
fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
90+
String::json_schema(r#gen)
91+
}
92+
}

0 commit comments

Comments
 (0)