Skip to content

Commit

Permalink
feat: add Fixer enum
Browse files Browse the repository at this point in the history
part of #656
  • Loading branch information
HerringtonDarkholme committed Dec 4, 2023
1 parent a7a1bd9 commit cf06465
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
31 changes: 31 additions & 0 deletions crates/config/src/fixer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// A pattern string or fix object to auto fix the issue.
/// It can reference metavariables appeared in rule.
#[derive(Serialize, Deserialize, Clone, JsonSchema)]
#[serde(untagged)]
pub enum Fixer {
Str(String),
// Config(FixConfig),
}

// #[derive(Serialize, Deserialize, Clone, JsonSchema)]
// pub struct FixConfig {
// template: String,
// forward_expand: String,
// backward_expand: String,
// prepend: String,
// }

#[cfg(test)]
mod test {
use super::*;
use crate::from_str;

#[test]
fn test_parse() {
let fixer: Fixer = from_str("test").expect("should parse");
assert!(matches!(fixer, Fixer::Str(_)));
}
}
1 change: 1 addition & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod combined;
mod constraints;
mod fixer;
mod maybe;
mod rule;
mod rule_collection;
Expand Down
5 changes: 3 additions & 2 deletions crates/config/src/rule_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::fixer::Fixer;
use crate::rule::{RuleSerializeError, SerializableRule};
use crate::transform::Transformation;
use crate::DeserializeEnv;
Expand Down Expand Up @@ -114,7 +115,7 @@ pub struct SerializableRuleConfig<L: Language> {
#[serde(default)]
pub severity: Severity,
/// A pattern to auto fix the issue. It can reference metavariables appeared in rule.
pub fix: Option<String>,
pub fix: Option<Fixer>,
/// Glob patterns to specify that the rule only applies to matching files
pub files: Option<Vec<String>>,
/// Glob patterns that exclude rules from applying to files
Expand All @@ -129,7 +130,7 @@ type RResult<T> = std::result::Result<T, RuleConfigError>;

impl<L: Language> SerializableRuleConfig<L> {
fn get_fixer(&self) -> RResult<Option<TemplateFix<String>>> {
if let Some(fix) = &self.fix {
if let Some(Fixer::Str(fix)) = &self.fix {
if let Some(trans) = &self.transform {
let keys: Vec<_> = trans.keys().cloned().collect();
Ok(Some(TemplateFix::with_transform(
Expand Down

0 comments on commit cf06465

Please sign in to comment.