Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions crates/pgls_configuration/src/generated.rs

This file was deleted.

19 changes: 0 additions & 19 deletions crates/pgls_configuration/src/generated/linter.rs

This file was deleted.

18 changes: 10 additions & 8 deletions crates/pgls_configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@
//!
//! The configuration is divided by "tool".

pub mod analyser;
pub mod database;
pub mod diagnostics;
pub mod files;
pub mod generated;
pub mod linter;
pub mod migrations;
pub mod plpgsql_check;
pub mod rules;
pub mod typecheck;
pub mod vcs;

pub use crate::diagnostics::ConfigurationDiagnostic;

use std::path::PathBuf;

pub use crate::generated::push_to_analyser_rules;
use crate::vcs::{PartialVcsConfiguration, VcsConfiguration, partial_vcs_configuration};
pub use analyser::{
LinterConfiguration, PartialLinterConfiguration, RuleConfiguration, RuleFixConfiguration,
RulePlainConfiguration, RuleSelector, RuleWithFixOptions, RuleWithOptions, Rules,
partial_linter_configuration,
};
use biome_deserialize::StringSet;
use biome_deserialize_macros::{Merge, Partial};
use bpaf::Bpaf;
use database::{
DatabaseConfiguration, PartialDatabaseConfiguration, partial_database_configuration,
};
use files::{FilesConfiguration, PartialFilesConfiguration, partial_files_configuration};
pub use linter::{
LinterConfiguration, PartialLinterConfiguration, Rules, partial_linter_configuration,
push_to_analyser_rules,
};
use migrations::{
MigrationsConfiguration, PartialMigrationsConfiguration, partial_migrations_configuration,
};
Expand All @@ -38,6 +36,10 @@ use plpgsql_check::{
PartialPlPgSqlCheckConfiguration, PlPgSqlCheckConfiguration,
partial_pl_pg_sql_check_configuration,
};
pub use rules::{
RuleConfiguration, RuleFixConfiguration, RulePlainConfiguration, RuleSelector,
RuleWithFixOptions, RuleWithOptions,
};
use serde::{Deserialize, Serialize};
pub use typecheck::{
PartialTypecheckConfiguration, TypecheckConfiguration, partial_typecheck_configuration,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
mod rules;
//! Generated file, do not edit by hand, see `xtask/codegen`

#![doc = r" Generated file, do not edit by hand, see `xtask/codegen`"]
mod rules;
use biome_deserialize::StringSet;
use biome_deserialize_macros::{Merge, Partial};
use bpaf::Bpaf;
pub use rules::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct LinterConfiguration {
/// if `false`, it disables the feature and the linter won't be executed. `true` by default
#[doc = r" if `false`, it disables the feature and the linter won't be executed. `true` by default"]
#[partial(bpaf(hide))]
pub enabled: bool,

/// List of rules
#[doc = r" List of rules"]
#[partial(bpaf(pure(Default::default()), optional, hide))]
pub rules: Rules,

/// A list of Unix shell style patterns. The formatter will ignore files/folders that will
/// match these patterns.
#[doc = r" A list of Unix shell style patterns. The formatter will ignore files/folders that will"]
#[doc = r" match these patterns."]
#[partial(bpaf(hide))]
pub ignore: StringSet,

/// A list of Unix shell style patterns. The formatter will include files/folders that will
/// match these patterns.
#[doc = r" A list of Unix shell style patterns. The formatter will include files/folders that will"]
#[doc = r" match these patterns."]
#[partial(bpaf(hide))]
pub include: StringSet,
}

impl LinterConfiguration {
pub const fn is_disabled(&self) -> bool {
!self.enabled
}
}

impl Default for LinterConfiguration {
fn default() -> Self {
Self {
Expand All @@ -46,12 +42,10 @@ impl Default for LinterConfiguration {
}
}
}

impl PartialLinterConfiguration {
pub const fn is_disabled(&self) -> bool {
matches!(self.enabled, Some(false))
}

pub fn get_rules(&self) -> Rules {
self.rules.clone().unwrap_or_default()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Generated file, do not edit by hand, see `xtask/codegen`

use crate::analyser::{RuleConfiguration, RulePlainConfiguration};
#![doc = r" Generated file, do not edit by hand, see `xtask/codegen`"]
use crate::rules::{RuleConfiguration, RulePlainConfiguration};
use biome_deserialize_macros::Merge;
use pgls_analyse::{RuleFilter, options::RuleOptions};
use pgls_diagnostics::{Category, Severity};
Expand Down Expand Up @@ -902,6 +903,22 @@ impl Safety {
}
}
}
#[doc = r" Push the configured rules to the analyser"]
pub fn push_to_analyser_rules(
rules: &Rules,
metadata: &pgls_analyse::MetadataRegistry,
analyser_rules: &mut pgls_analyse::AnalyserRules,
) {
if let Some(rules) = rules.safety.as_ref() {
for rule_name in Safety::GROUP_RULES {
if let Some((_, Some(rule_options))) = rules.get_rule_configuration(rule_name) {
if let Some(rule_key) = metadata.find_rule("safety", rule_name) {
analyser_rules.push_rule(rule_key, rule_options);
}
}
}
}
}
#[test]
fn test_order() {
for items in Safety::GROUP_RULES.windows(2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
pub mod linter;

pub use crate::analyser::linter::*;
use biome_deserialize::Merge;
use biome_deserialize_macros::Deserializable;
use pgls_analyse::RuleFilter;
use pgls_analyse::options::RuleOptions;
use pgls_diagnostics::Severity;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
Expand Down Expand Up @@ -300,90 +295,3 @@ impl<T: Default> Merge for RuleWithFixOptions<T> {
self.options = other.options;
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum RuleSelector {
Group(linter::RuleGroup),
Rule(linter::RuleGroup, &'static str),
}

impl From<RuleSelector> for RuleFilter<'static> {
fn from(value: RuleSelector) -> Self {
match value {
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
}
}
}

impl<'a> From<&'a RuleSelector> for RuleFilter<'static> {
fn from(value: &'a RuleSelector) -> Self {
match value {
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
}
}
}

impl FromStr for RuleSelector {
type Err = &'static str;
fn from_str(selector: &str) -> Result<Self, Self::Err> {
let selector = selector.strip_prefix("lint/").unwrap_or(selector);
if let Some((group_name, rule_name)) = selector.split_once('/') {
let group = linter::RuleGroup::from_str(group_name)?;
if let Some(rule_name) = Rules::has_rule(group, rule_name) {
Ok(RuleSelector::Rule(group, rule_name))
} else {
Err("This rule doesn't exist.")
}
} else {
match linter::RuleGroup::from_str(selector) {
Ok(group) => Ok(RuleSelector::Group(group)),
Err(_) => Err(
"This group doesn't exist. Use the syntax `<group>/<rule>` to specify a rule.",
),
}
}
}
}

impl serde::Serialize for RuleSelector {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
RuleSelector::Group(group) => serializer.serialize_str(group.as_str()),
RuleSelector::Rule(group, rule_name) => {
let group_name = group.as_str();
serializer.serialize_str(&format!("{group_name}/{rule_name}"))
}
}
}
}

impl<'de> serde::Deserialize<'de> for RuleSelector {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl serde::de::Visitor<'_> for Visitor {
type Value = RuleSelector;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("<group>/<ruyle_name>")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
match RuleSelector::from_str(v) {
Ok(result) => Ok(result),
Err(error) => Err(serde::de::Error::custom(error)),
}
}
}
deserializer.deserialize_str(Visitor)
}
}

#[cfg(feature = "schema")]
impl schemars::JsonSchema for RuleSelector {
fn schema_name() -> String {
"RuleCode".to_string()
}
fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
String::json_schema(r#gen)
}
}
9 changes: 9 additions & 0 deletions crates/pgls_configuration/src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub(crate) mod configuration;
pub(crate) mod selector;

pub use configuration::{
RuleAssistConfiguration, RuleAssistPlainConfiguration, RuleAssistWithOptions,
RuleConfiguration, RuleFixConfiguration, RulePlainConfiguration, RuleWithFixOptions,
RuleWithOptions,
};
pub use selector::RuleSelector;
92 changes: 92 additions & 0 deletions crates/pgls_configuration/src/rules/selector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use pgls_analyse::RuleFilter;

use std::str::FromStr;

use crate::{Rules, linter::RuleGroup};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum RuleSelector {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be made more generic in a future pr

Group(RuleGroup),
Rule(RuleGroup, &'static str),
}

impl From<RuleSelector> for RuleFilter<'static> {
fn from(value: RuleSelector) -> Self {
match value {
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
}
}
}

impl<'a> From<&'a RuleSelector> for RuleFilter<'static> {
fn from(value: &'a RuleSelector) -> Self {
match value {
RuleSelector::Group(group) => RuleFilter::Group(group.as_str()),
RuleSelector::Rule(group, name) => RuleFilter::Rule(group.as_str(), name),
}
}
}

impl FromStr for RuleSelector {
type Err = &'static str;
fn from_str(selector: &str) -> Result<Self, Self::Err> {
let selector = selector.strip_prefix("lint/").unwrap_or(selector);
if let Some((group_name, rule_name)) = selector.split_once('/') {
let group = RuleGroup::from_str(group_name)?;
if let Some(rule_name) = Rules::has_rule(group, rule_name) {
Ok(RuleSelector::Rule(group, rule_name))
} else {
Err("This rule doesn't exist.")
}
} else {
match RuleGroup::from_str(selector) {
Ok(group) => Ok(RuleSelector::Group(group)),
Err(_) => Err(
"This group doesn't exist. Use the syntax `<group>/<rule>` to specify a rule.",
),
}
}
}
}

impl serde::Serialize for RuleSelector {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
RuleSelector::Group(group) => serializer.serialize_str(group.as_str()),
RuleSelector::Rule(group, rule_name) => {
let group_name = group.as_str();
serializer.serialize_str(&format!("{group_name}/{rule_name}"))
}
}
}
}

impl<'de> serde::Deserialize<'de> for RuleSelector {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl serde::de::Visitor<'_> for Visitor {
type Value = RuleSelector;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("<group>/<ruyle_name>")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
match RuleSelector::from_str(v) {
Ok(result) => Ok(result),
Err(error) => Err(serde::de::Error::custom(error)),
}
}
}
deserializer.deserialize_str(Visitor)
}
}

#[cfg(feature = "schema")]
impl schemars::JsonSchema for RuleSelector {
fn schema_name() -> String {
"RuleCode".to_string()
}
fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
String::json_schema(r#gen)
}
}
Loading
Loading