The current implementation of the body_literals function relies on several small helper functions (extract_literals_from_body, process_body_element, process_token, and add_literal_if_not_empty) to parse and collect body literals. This approach increases code complexity and can be simplified.
Suggested Improvement:
Refactor body_literals to perform a single pass that collects and splits the body text, eliminating the need for the four helper functions. The new implementation would:
- Skip tokens up to and including the
:- token.
- Collect all tokens until the terminating
..
- Concatenate the text, split on commas, trim whitespace, and discard empty pieces.
Example Implementation:
#[must_use]
pub fn body_literals(&self) -> Vec<String> {
use rowan::NodeOrToken;
use crate::SyntaxKind::{T_DOT, T_IMPLIES};
// Skip up through the “:-” token, take everything until the terminating “.”
let body_text: String = self.syntax
.children_with_tokens()
.skip_while(|e| e.kind() != T_IMPLIES)
.skip(1) // drop the “:-”
.take_while(|e| e.kind() != T_DOT)
.map(|e| match e {
NodeOrToken::Node(n) => n.text().to_string(),
NodeOrToken::Token(t) => t.text().to_string(),
})
.collect();
// Split on commas, trim, and discard any empty pieces
body_text
.split(',')
.map(str::trim)
.filter(|lit| !lit.is_empty())
.map(str::to_string)
.collect()
}
Action Items:
- Refactor
body_literals as described above.
- Remove the now-unnecessary helper functions.
- Ensure all tests pass and output remains unchanged.
This change will simplify the codebase and improve maintainability.
I created this issue for @leynos from #89 (comment).
Tips and commands
Getting Help
The current implementation of the
body_literalsfunction relies on several small helper functions (extract_literals_from_body,process_body_element,process_token, andadd_literal_if_not_empty) to parse and collect body literals. This approach increases code complexity and can be simplified.Suggested Improvement:
Refactor
body_literalsto perform a single pass that collects and splits the body text, eliminating the need for the four helper functions. The new implementation would::-token...Example Implementation:
Action Items:
body_literalsas described above.This change will simplify the codebase and improve maintainability.
I created this issue for @leynos from #89 (comment).
Tips and commands
Getting Help