Skip to content

Commit

Permalink
chore: make config not optional in rule context
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Oct 11, 2024
1 parent 74d57e8 commit 20e71be
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion crates/lib/src/core/rules/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub trait Rule: CloneRule + dyn_clone::DynClone + Debug + 'static + Send + Sync
tables,
dialect,
fix,
config: Some(config),
config,
segment: tree.clone(),
templated_file: <_>::default(),
path: <_>::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/core/rules/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct RuleContext<'a> {
pub fix: bool,
pub templated_file: Option<TemplatedFile>,
pub path: Option<String>,
pub config: Option<&'a FluffConfig>,
pub config: &'a FluffConfig,

// These change within a file.
/// segment: The segment in question
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/rules/aliasing/al01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ FROM foo AS voo
as_keyword,
rule_cx.parent_stack[0].clone(),
TargetSide::Both,
rule_cx.config.unwrap(),
rule_cx.config,
)
.without(as_keyword)
.respace(rule_cx.tables, false, Filter::All)
Expand All @@ -145,7 +145,7 @@ FROM foo AS voo
&identifier,
rule_cx.parent_stack[0].clone(),
TargetSide::Before,
rule_cx.config.unwrap(),
rule_cx.config,
)
.insert(
SegmentBuilder::keyword(rule_cx.tables.next_id(), "AS"),
Expand Down
1 change: 0 additions & 1 deletion crates/lib/src/rules/aliasing/al03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ FROM foo

if context
.config
.unwrap()
.get("allow_scalar", "rules")
.as_bool()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/capitalisation/cp02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ from foo
.unquoted_identifiers_policy
.as_deref()
.unwrap_or_else(|| {
context.config.unwrap().raw["rules"]["unquoted_identifiers_policy"]
context.config.raw["rules"]["unquoted_identifiers_policy"]
.as_string()
.unwrap()
});
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/convention/cv05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ WHERE a IS NULL
&context.segment,
context.parent_stack[0].clone(),
TargetSide::Both,
context.config.unwrap(),
context.config,
)
.replace(context.segment.clone(), &seg)
.respace(context.tables, false, Filter::All)
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ JOIN bar USING (a)
}

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let sequence = ReflowSequence::from_root(context.segment, context.config.unwrap());
let sequence = ReflowSequence::from_root(context.segment, context.config);
sequence
.respace(context.tables, false, Filter::All)
.results()
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ FROM foo
}

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
ReflowSequence::from_root(context.segment, context.config.unwrap())
ReflowSequence::from_root(context.segment, context.config)
.reindent(context.tables)
.results()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/lib/src/rules/layout/lt03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ FROM foo

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
if context.segment.is_type(SyntaxKind::ComparisonOperator) {
let comparison_positioning = context.config.unwrap().raw["layout"]["type"]
let comparison_positioning = context.config.raw["layout"]["type"]
["comparison_operator"]["line_position"]
.as_string()
.unwrap();
Expand All @@ -76,8 +76,8 @@ FROM foo
return vec![LintResult::new(None, Vec::new(), None, None, None)];
}
} else if context.segment.is_type(SyntaxKind::BinaryOperator) {
let binary_positioning = context.config.unwrap().raw["layout"]["type"]
["binary_operator"]["line_position"]
let binary_positioning = context.config.raw["layout"]["type"]["binary_operator"]
["line_position"]
.as_string()
.unwrap();

Expand All @@ -94,7 +94,7 @@ FROM foo
&context.segment,
context.parent_stack.first().unwrap().clone(),
TargetSide::Both,
context.config.unwrap(),
context.config,
)
.rebreak(context.tables)
.results()
Expand Down
5 changes: 2 additions & 3 deletions crates/lib/src/rules/layout/lt04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ FROM foo
}

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let comma_positioning = context.config.unwrap().raw["layout"]["type"]["comma"]
["line_position"]
let comma_positioning = context.config.raw["layout"]["type"]["comma"]["line_position"]
.as_string()
.unwrap();

Expand All @@ -85,7 +84,7 @@ FROM foo
&context.segment,
context.parent_stack.first().unwrap().clone(),
TargetSide::Both,
context.config.unwrap(),
context.config,
)
.rebreak(context.tables)
.results()
Expand Down
15 changes: 6 additions & 9 deletions crates/lib/src/rules/layout/lt05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub struct RuleLT05 {
}

impl Rule for RuleLT05 {
fn load_from_config(&self, _config: &AHashMap<String, Value>) -> Result<ErasedRule, String> {
fn load_from_config(&self, config: &AHashMap<String, Value>) -> Result<ErasedRule, String> {
Ok(RuleLT05 {
ignore_comment_lines: _config["ignore_comment_lines"].as_bool().unwrap(),
ignore_comment_clauses: _config["ignore_comment_clauses"].as_bool().unwrap(),
ignore_comment_lines: config["ignore_comment_lines"].as_bool().unwrap(),
ignore_comment_clauses: config["ignore_comment_clauses"].as_bool().unwrap(),
}
.erased())
}
Expand Down Expand Up @@ -67,10 +67,9 @@ FROM my_table"#
&[RuleGroups::All, RuleGroups::Core, RuleGroups::Layout]
}
fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let mut results =
ReflowSequence::from_root(context.segment.clone(), context.config.unwrap())
.break_long_lines(context.tables)
.results();
let mut results = ReflowSequence::from_root(context.segment.clone(), context.config)
.break_long_lines(context.tables)
.results();

let mut to_remove = AHashSet::new();

Expand Down Expand Up @@ -143,8 +142,6 @@ FROM my_table"#
if (line_pos as i32)
< context
.config
.as_ref()
.unwrap()
.get("max_line_length", "core")
.as_int()
.unwrap()
Expand Down
3 changes: 1 addition & 2 deletions crates/lib/src/rules/layout/lt08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ SELECT a FROM plop
}
fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let mut error_buffer = Vec::new();
let global_comma_style = context.config.unwrap().raw["layout"]["type"]["comma"]
["line_position"]
let global_comma_style = context.config.raw["layout"]["type"]["comma"]["line_position"]
.as_string()
.unwrap();
let expanded_segments = context.segment.iter_segments(
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ SELECT 'b' AS col
&context.segment,
context.parent_stack.first().unwrap().clone(),
TargetSide::Both,
context.config.unwrap(),
context.config,
)
.rebreak(context.tables)
.results()
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/references/rf03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ FROM foo
fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let single_table_references =
self.single_table_references.as_deref().unwrap_or_else(|| {
context.config.unwrap().raw["rules"]["single_table_references"]
context.config.raw["rules"]["single_table_references"]
.as_string()
.unwrap()
});
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/rules/structure/st04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ FROM mytable
.map(LintFix::delete)
.collect_vec();

let tab_space_size = context.config.unwrap().raw["indentation"]["tab_space_size"]
let tab_space_size = context.config.raw["indentation"]["tab_space_size"]
.as_int()
.unwrap() as usize;
let indent_unit = context.config.unwrap().raw["indentation"]["indent_unit"]
let indent_unit = context.config.raw["indentation"]["indent_unit"]
.as_string()
.unwrap();
let indent_unit = IndentUnit::from_type_and_size(indent_unit, tab_space_size);
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/rules/structure/st08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl RuleST08 {
anchor,
context.parent_stack[0].clone(),
TargetSide::Before,
context.config.unwrap(),
context.config,
)
.replace(
anchor.clone(),
Expand Down Expand Up @@ -125,7 +125,7 @@ SELECT DISTINCT a, b FROM foo
&modifier[0],
context.parent_stack[0].clone(),
TargetSide::After,
context.config.unwrap(),
context.config,
));
}
}
Expand Down

0 comments on commit 20e71be

Please sign in to comment.