-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: extract more node compilers (I/L) #177
Conversation
WalkthroughThis update transforms pattern handling by introducing specialized compiler classes for various patterns, enhancing modularity and code encapsulation. The transition signifies a strategic architectural shift towards more sophisticated and maintainable code, focusing on pattern compilation and visibility adjustments across structures. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Status
Configuration used: CodeRabbit UI
Files selected for processing (13)
- crates/core/src/pattern/container.rs (2 hunks)
- crates/core/src/pattern/if.rs (2 hunks)
- crates/core/src/pattern/includes.rs (2 hunks)
- crates/core/src/pattern/like.rs (1 hunks)
- crates/core/src/pattern/limit.rs (2 hunks)
- crates/core/src/pattern/list_index.rs (1 hunks)
- crates/core/src/pattern/patterns.rs (6 hunks)
- crates/core/src/pattern_compiler/if_compiler.rs (1 hunks)
- crates/core/src/pattern_compiler/includes_compiler.rs (1 hunks)
- crates/core/src/pattern_compiler/like_compiler.rs (1 hunks)
- crates/core/src/pattern_compiler/limit_compiler.rs (1 hunks)
- crates/core/src/pattern_compiler/list_index_compiler.rs (1 hunks)
- crates/core/src/pattern_compiler/mod.rs (1 hunks)
Additional comments not posted (13)
crates/core/src/pattern_compiler/mod.rs (1)
10-14
: The addition of new compiler modules enhances the project's modularity and structure.crates/core/src/pattern_compiler/includes_compiler.rs (1)
8-36
: Correct implementation ofIncludesCompiler
adhering to best practices and project structure.crates/core/src/pattern/like.rs (1)
14-15
: Changes in field visibility and the removal of thefrom_node
method align with the PR's objectives to enhance modularity and maintainability.crates/core/src/pattern_compiler/limit_compiler.rs (1)
8-42
: Correct implementation ofLimitCompiler
adhering to best practices and project structure.crates/core/src/pattern_compiler/like_compiler.rs (1)
10-52
: Correct implementation ofLikeCompiler
adhering to best practices and project structure.crates/core/src/pattern/limit.rs (1)
Line range hint
1-1
: Removal of thefrom_node
function aligns with the PR's objectives to centralize pattern compilation, enhancing modularity and maintainability.crates/core/src/pattern_compiler/if_compiler.rs (1)
10-64
: Correct implementation ofIfCompiler
adhering to best practices and project structure.crates/core/src/pattern_compiler/list_index_compiler.rs (1)
13-76
: Correct implementation ofListIndexCompiler
adhering to best practices and project structure.crates/core/src/pattern/if.rs (1)
18-20
: Changing the visibility of theIf
struct fields frompub(crate)
topub
increases their accessibility. Ensure that this change aligns with the intended encapsulation and usage patterns of theIf
struct within the broader codebase.crates/core/src/pattern/container.rs (1)
73-73
: Replacing direct instantiation ofListIndex
withListIndexCompiler::from_node
aligns with the PR's goal of enhancing modularity and maintainability by using compiler patterns. This change centralizes the logic for creatingListIndex
instances, which can simplify future modifications and ensure consistency.crates/core/src/pattern/includes.rs (1)
4-7
: The removal of unused imports, such asvariable::VariableSourceLocations
andNode
, along with the adjustment of theState
import, cleans up the code and aligns with Rust best practices for managing dependencies. This change contributes to the maintainability and readability of the code.crates/core/src/pattern/list_index.rs (1)
18-32
: Changing the visibility of theListOrContainer
andContainerOrIndex
enums, as well as theListIndex
struct fields frompub(crate)
topub
, increases their accessibility. This change should be carefully considered to ensure it aligns with the intended encapsulation and usage patterns within the broader codebase. Additionally, ensure that the removal of thefrom_node
method from theListIndex
struct is consistent with the PR's goal of centralizing node compilation logic.crates/core/src/pattern/patterns.rs (1)
827-833
: > 📝 NOTEThis review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [718-987]
Replacing direct calls to constructors with calls to specific compiler classes (
LimitCompiler
,IncludesCompiler
,IfCompiler
,ListIndexCompiler
, andLikeCompiler
) for creatingLimit
,Includes
,If
,ListIndex
, andLike
patterns, respectively, is a significant improvement. This approach enhances the modularity and maintainability of the code by centralizing the logic for pattern creation. Ensure that these changes are thoroughly tested to confirm that the behavior of pattern creation remains consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review Status
Configuration used: CodeRabbit UI
Files selected for processing (5)
- crates/core/src/pattern/log.rs (2 hunks)
- crates/core/src/pattern/patterns.rs (7 hunks)
- crates/core/src/pattern/predicates.rs (2 hunks)
- crates/core/src/pattern_compiler/log_compiler.rs (1 hunks)
- crates/core/src/pattern_compiler/mod.rs (1 hunks)
Additional comments not posted (5)
crates/core/src/pattern_compiler/mod.rs (1)
10-15
: Ensure the newly added compiler modules are utilized appropriately within the codebase.Verification successful
The verification process confirms that the newly added compiler modules (
if_compiler
,includes_compiler
,like_compiler
,limit_compiler
,list_index_compiler
,log_compiler
) are utilized appropriately within the codebase, as evidenced by their references in various parts of the project.Scripts Executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Verify that the new compiler modules are imported and used in other parts of the project. for module in if_compiler includes_compiler like_compiler limit_compiler list_index_compiler log_compiler; do echo "Checking usage of $module..." rg --type rust --count "$module::" doneLength of output: 1698
crates/core/src/pattern/log.rs (2)
13-22
: MakingVariableInfo
public enhances modularity by allowing external access to variable information within logs.
26-31
: Public visibility of theLog
constructor and methods aligns with the goal of improving code modularity and maintainability.crates/core/src/pattern/predicates.rs (1)
130-130
: ReplacingLog
withLogCompiler
in thePredicate::Log
variant is a key step towards centralizing compilation logic.crates/core/src/pattern/patterns.rs (1)
827-833
: > 📝 NOTEThis review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [718-987]
Replacing direct constructor calls with compiler class calls for
Limit
,Includes
,If
,ListIndex
, andLike
patterns enhances the code's modularity and maintainability.
pub(crate) struct LogCompiler; | ||
|
||
impl NodeCompiler for LogCompiler { | ||
type TargetPattern = Log; | ||
|
||
fn from_node( | ||
node: &Node, | ||
context: &CompilationContext, | ||
vars: &mut BTreeMap<String, usize>, | ||
vars_array: &mut Vec<Vec<VariableSourceLocations>>, | ||
scope_index: usize, | ||
global_vars: &mut BTreeMap<String, usize>, | ||
logs: &mut AnalysisLogs, | ||
) -> Result<Self::TargetPattern> { | ||
let message = node.child_by_field_name("message"); | ||
let message = if let Some(message) = message { | ||
Some(Pattern::from_node( | ||
&message, | ||
context, | ||
vars, | ||
vars_array, | ||
scope_index, | ||
global_vars, | ||
false, | ||
logs, | ||
)?) | ||
} else { | ||
None | ||
}; | ||
let variable_node = node.child_by_field_name("variable"); | ||
let variable = variable_node | ||
.map(|n| { | ||
let name = n.utf8_text(context.src.as_bytes()).unwrap().to_string(); | ||
let variable = Variable::from_node( | ||
&n, | ||
context.file, | ||
context.src, | ||
vars, | ||
global_vars, | ||
vars_array, | ||
scope_index, | ||
)?; | ||
Ok(VariableInfo::new(name, variable)) | ||
}) | ||
.map_or(Ok(None), |v: Result<VariableInfo>| v.map(Some))?; | ||
|
||
Ok(Log::new(variable, message)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation of LogCompiler
aligns with the compiler pattern expansion objective. However, consider adding error handling for the unwrap
calls to prevent potential panics.
- let name = n.utf8_text(context.src.as_bytes()).unwrap().to_string();
+ let name = n.utf8_text(context.src.as_bytes()).ok_or_else(|| anyhow!("Failed to get utf8 text for variable node"))?.to_string();
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
pub(crate) struct LogCompiler; | |
impl NodeCompiler for LogCompiler { | |
type TargetPattern = Log; | |
fn from_node( | |
node: &Node, | |
context: &CompilationContext, | |
vars: &mut BTreeMap<String, usize>, | |
vars_array: &mut Vec<Vec<VariableSourceLocations>>, | |
scope_index: usize, | |
global_vars: &mut BTreeMap<String, usize>, | |
logs: &mut AnalysisLogs, | |
) -> Result<Self::TargetPattern> { | |
let message = node.child_by_field_name("message"); | |
let message = if let Some(message) = message { | |
Some(Pattern::from_node( | |
&message, | |
context, | |
vars, | |
vars_array, | |
scope_index, | |
global_vars, | |
false, | |
logs, | |
)?) | |
} else { | |
None | |
}; | |
let variable_node = node.child_by_field_name("variable"); | |
let variable = variable_node | |
.map(|n| { | |
let name = n.utf8_text(context.src.as_bytes()).unwrap().to_string(); | |
let variable = Variable::from_node( | |
&n, | |
context.file, | |
context.src, | |
vars, | |
global_vars, | |
vars_array, | |
scope_index, | |
)?; | |
Ok(VariableInfo::new(name, variable)) | |
}) | |
.map_or(Ok(None), |v: Result<VariableInfo>| v.map(Some))?; | |
Ok(Log::new(variable, message)) | |
} | |
pub(crate) struct LogCompiler; | |
impl NodeCompiler for LogCompiler { | |
type TargetPattern = Log; | |
fn from_node( | |
node: &Node, | |
context: &CompilationContext, | |
vars: &mut BTreeMap<String, usize>, | |
vars_array: &mut Vec<Vec<VariableSourceLocations>>, | |
scope_index: usize, | |
global_vars: &mut BTreeMap<String, usize>, | |
logs: &mut AnalysisLogs, | |
) -> Result<Self::TargetPattern> { | |
let message = node.child_by_field_name("message"); | |
let message = if let Some(message) = message { | |
Some(Pattern::from_node( | |
&message, | |
context, | |
vars, | |
vars_array, | |
scope_index, | |
global_vars, | |
false, | |
logs, | |
)?) | |
} else { | |
None | |
}; | |
let variable_node = node.child_by_field_name("variable"); | |
let variable = variable_node | |
.map(|n| { | |
let name = n.utf8_text(context.src.as_bytes()).ok_or_else(|| anyhow!("Failed to get utf8 text for variable node"))?.to_string(); | |
let variable = Variable::from_node( | |
&n, | |
context.file, | |
context.src, | |
vars, | |
global_vars, | |
vars_array, | |
scope_index, | |
)?; | |
Ok(VariableInfo::new(name, variable)) | |
}) | |
.map_or(Ok(None), |v: Result<VariableInfo>| v.map(Some))?; | |
Ok(Log::new(variable, message)) | |
} |
Grouped in #179 to avoid further merge issues. |
Applies the node compiler pattern from #164 to yet more node types.
See also: #173
Summary by CodeRabbit
New Features
If
,Includes
,Like
,Limit
, andListIndex
patterns, enhancing pattern compilation and processing.Refactor
If
,Includes
,Like
,Limit
, andListIndex
structures, improving module access control and pattern handling.Chores