Skip to content

Commit 3a303c1

Browse files
committedJan 15, 2024
refactor: move SerializableRuleCore to rule_core mod
1 parent 367a93a commit 3a303c1

File tree

2 files changed

+80
-75
lines changed

2 files changed

+80
-75
lines changed
 

‎crates/config/src/rule_config.rs

+5-73
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
use crate::fixer::{Fixer, FixerError, SerializableFixer};
2-
use crate::rule::{RuleSerializeError, SerializableRule};
3-
use crate::transform::Transformation;
4-
use crate::DeserializeEnv;
1+
use crate::fixer::Fixer;
52
use crate::GlobalRules;
63

74
pub use crate::rule_core::{
8-
try_deserialize_matchers, RuleWithConstraint, SerializableMetaVarMatcher,
9-
SerializeConstraintsError,
5+
try_deserialize_matchers, RuleConfigError, RuleWithConstraint, SerializableMetaVarMatcher,
6+
SerializableRuleCore, SerializeConstraintsError,
107
};
118
use ast_grep_core::language::Language;
12-
use ast_grep_core::meta_var::MetaVarMatchers;
139
use ast_grep_core::replacer::{IndentSensitive, Replacer};
1410
use ast_grep_core::{NodeMatch, StrDoc};
1511
use schemars::JsonSchema;
1612
use serde::{Deserialize, Serialize};
17-
use serde_yaml::{with::singleton_map_recursive::deserialize, Deserializer, Error as YamlError};
18-
use thiserror::Error;
13+
use serde_yaml::{with::singleton_map_recursive::deserialize, Deserializer};
1914

2015
use std::collections::HashMap;
2116
use std::ops::{Deref, DerefMut};
@@ -36,58 +31,6 @@ pub enum Severity {
3631
Off,
3732
}
3833

39-
#[derive(Serialize, Deserialize, Clone, JsonSchema)]
40-
pub struct SerializableRuleCore<L: Language> {
41-
/// Specify the language to parse and the file extension to include in matching.
42-
pub language: L,
43-
/// A rule object to find matching AST nodes
44-
pub rule: SerializableRule,
45-
/// Additional meta variables pattern to filter matching
46-
pub constraints: Option<HashMap<String, SerializableMetaVarMatcher>>,
47-
/// Utility rules that can be used in `matches`
48-
pub utils: Option<HashMap<String, SerializableRule>>,
49-
/// A dictionary for metavariable manipulation. Dict key is the new variable name.
50-
/// Dict value is a [transformation] that specifies how meta var is processed.
51-
/// See [transformation doc](https://ast-grep.github.io/reference/yaml/transformation.html).
52-
pub transform: Option<HashMap<String, Transformation>>,
53-
/// A pattern string or a FixConfig object to auto fix the issue.
54-
/// It can reference metavariables appeared in rule.
55-
/// See details in fix [object reference](https://ast-grep.github.io/reference/yaml/fix.html#fixconfig).
56-
pub fix: Option<SerializableFixer>,
57-
}
58-
59-
impl<L: Language> SerializableRuleCore<L> {
60-
fn get_deserialize_env(&self, globals: &GlobalRules<L>) -> RResult<DeserializeEnv<L>> {
61-
let env = DeserializeEnv::new(self.language.clone()).with_globals(globals);
62-
if let Some(utils) = &self.utils {
63-
let env = env.register_local_utils(utils)?;
64-
Ok(env)
65-
} else {
66-
Ok(env)
67-
}
68-
}
69-
70-
fn get_meta_var_matchers(&self) -> RResult<MetaVarMatchers<StrDoc<L>>> {
71-
Ok(if let Some(constraints) = self.constraints.clone() {
72-
try_deserialize_matchers(constraints, self.language.clone())?
73-
} else {
74-
MetaVarMatchers::default()
75-
})
76-
}
77-
78-
pub fn get_matcher(&self, globals: &GlobalRules<L>) -> RResult<RuleWithConstraint<L>> {
79-
let env = self.get_deserialize_env(globals)?;
80-
let rule = env.deserialize_rule(self.rule.clone())?;
81-
let matchers = self.get_meta_var_matchers()?;
82-
let transform = self.transform.clone();
83-
Ok(
84-
RuleWithConstraint::new(rule)
85-
.with_matchers(matchers)
86-
.with_utils(env.registration)
87-
.with_transform(transform),
88-
)
89-
}
90-
}
9134
#[derive(Serialize, Deserialize, Clone, JsonSchema)]
9235
pub struct SerializableRuleCoreWithId<L: Language> {
9336
#[serde(flatten)]
@@ -160,18 +103,6 @@ impl<L: Language> DerefMut for SerializableRuleConfig<L> {
160103
}
161104
}
162105

163-
#[derive(Debug, Error)]
164-
pub enum RuleConfigError {
165-
#[error("Fail to parse yaml as RuleConfig")]
166-
Yaml(#[from] YamlError),
167-
#[error("Rule is not configured correctly.")]
168-
Rule(#[from] RuleSerializeError),
169-
#[error("fix pattern is invalid.")]
170-
Fixer(#[from] FixerError),
171-
#[error("constraints is not configured correctly.")]
172-
Constraints(#[from] SerializeConstraintsError),
173-
}
174-
175106
pub struct RuleConfig<L: Language> {
176107
inner: SerializableRuleConfig<L>,
177108
pub matcher: RuleWithConstraint<L>,
@@ -218,6 +149,7 @@ impl<L: Language> Deref for RuleConfig<L> {
218149
mod test {
219150
use super::*;
220151
use crate::from_str;
152+
use crate::rule::SerializableRule;
221153
use crate::test::TypeScript;
222154

223155
fn ts_rule_config(rule: SerializableRule) -> SerializableRuleConfig<TypeScript> {

‎crates/config/src/rule_core.rs

+75-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
use serde::{Deserialize, Serialize};
2-
1+
use crate::fixer::{FixerError, SerializableFixer};
32
use crate::rule::referent_rule::RuleRegistration;
43
use crate::rule::Rule;
4+
use crate::rule::{RuleSerializeError, SerializableRule};
55
use crate::transform::{apply_env_transform, Transformation};
6+
use crate::DeserializeEnv;
7+
use crate::GlobalRules;
8+
69
use ast_grep_core::language::Language;
710
use ast_grep_core::matcher::{KindMatcher, KindMatcherError, RegexMatcher, RegexMatcherError};
811
use ast_grep_core::meta_var::{MetaVarEnv, MetaVarMatcher, MetaVarMatchers};
912
use ast_grep_core::{Doc, Matcher, Node, Pattern, PatternError, StrDoc};
13+
use serde::{Deserialize, Serialize};
14+
use serde_yaml::Error as YamlError;
1015

1116
use bit_set::BitSet;
1217
use schemars::JsonSchema;
@@ -60,6 +65,74 @@ pub fn try_deserialize_matchers<L: Language>(
6065
Ok(map)
6166
}
6267

68+
#[derive(Debug, Error)]
69+
pub enum RuleConfigError {
70+
#[error("Fail to parse yaml as RuleConfig")]
71+
Yaml(#[from] YamlError),
72+
#[error("Rule is not configured correctly.")]
73+
Rule(#[from] RuleSerializeError),
74+
#[error("fix pattern is invalid.")]
75+
Fixer(#[from] FixerError),
76+
#[error("constraints is not configured correctly.")]
77+
Constraints(#[from] SerializeConstraintsError),
78+
}
79+
80+
type RResult<T> = std::result::Result<T, RuleConfigError>;
81+
82+
/// Used for global rules, rewriters, and pyo3/napi
83+
#[derive(Serialize, Deserialize, Clone, JsonSchema)]
84+
pub struct SerializableRuleCore<L: Language> {
85+
/// Specify the language to parse and the file extension to include in matching.
86+
pub language: L,
87+
/// A rule object to find matching AST nodes
88+
pub rule: SerializableRule,
89+
/// Additional meta variables pattern to filter matching
90+
pub constraints: Option<HashMap<String, SerializableMetaVarMatcher>>,
91+
/// Utility rules that can be used in `matches`
92+
pub utils: Option<HashMap<String, SerializableRule>>,
93+
/// A dictionary for metavariable manipulation. Dict key is the new variable name.
94+
/// Dict value is a [transformation] that specifies how meta var is processed.
95+
/// See [transformation doc](https://ast-grep.github.io/reference/yaml/transformation.html).
96+
pub transform: Option<HashMap<String, Transformation>>,
97+
/// A pattern string or a FixConfig object to auto fix the issue.
98+
/// It can reference metavariables appeared in rule.
99+
/// See details in fix [object reference](https://ast-grep.github.io/reference/yaml/fix.html#fixconfig).
100+
pub fix: Option<SerializableFixer>,
101+
}
102+
103+
impl<L: Language> SerializableRuleCore<L> {
104+
pub fn get_deserialize_env(&self, globals: &GlobalRules<L>) -> RResult<DeserializeEnv<L>> {
105+
let env = DeserializeEnv::new(self.language.clone()).with_globals(globals);
106+
if let Some(utils) = &self.utils {
107+
let env = env.register_local_utils(utils)?;
108+
Ok(env)
109+
} else {
110+
Ok(env)
111+
}
112+
}
113+
114+
fn get_meta_var_matchers(&self) -> RResult<MetaVarMatchers<StrDoc<L>>> {
115+
Ok(if let Some(constraints) = self.constraints.clone() {
116+
try_deserialize_matchers(constraints, self.language.clone())?
117+
} else {
118+
MetaVarMatchers::default()
119+
})
120+
}
121+
122+
pub fn get_matcher(&self, globals: &GlobalRules<L>) -> RResult<RuleWithConstraint<L>> {
123+
let env = self.get_deserialize_env(globals)?;
124+
let rule = env.deserialize_rule(self.rule.clone())?;
125+
let matchers = self.get_meta_var_matchers()?;
126+
let transform = self.transform.clone();
127+
Ok(
128+
RuleWithConstraint::new(rule)
129+
.with_matchers(matchers)
130+
.with_utils(env.registration)
131+
.with_transform(transform),
132+
)
133+
}
134+
}
135+
63136
pub struct RuleWithConstraint<L: Language> {
64137
rule: Rule<L>,
65138
matchers: MetaVarMatchers<StrDoc<L>>,

0 commit comments

Comments
 (0)
Please sign in to comment.