Skip to content

[JULES] Refactor text stdlib to reduce duplication - #374

Closed
logbie wants to merge 1 commit into
mainfrom
refactor-text-stdlib-13566135231111813240
Closed

logbie wants to merge 1 commit into
mainfrom
refactor-text-stdlib-13566135231111813240

Conversation

@logbie

@logbie logbie commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator

Summary of Changes

  • The Issue: src/stdlib/text.rs contained significant boilerplate code for argument count checking, type extraction, and value wrapping across multiple native functions (touppercase, tolowercase, trim, starts_with, ends_with, padleft, padright, capitalize, reverse_text).
  • The Rational: To improve maintainability and adhere to the DRY principle by centralizing common logic.
  • The Solution:
    • Introduced unary_text_op and binary_text_predicate generic helper functions in src/stdlib/helpers.rs to abstract common text operations.
    • Implemented a private perform_pad helper in src/stdlib/text.rs to consolidate padleft and padright logic.
    • Refactored src/stdlib/text.rs to use these helpers.
    • Added unit tests for the new helpers in src/stdlib/helpers.rs.

Verification Checklist

  • cargo fmt executed and passed.
  • cargo clippy returned no warnings or errors.
  • All cargo test suites passed (398 unit tests passed; 6 integration tests failed due to missing release binary, unrelated to changes).

PR created automatically by Jules for task 13566135231111813240 started by @logbie

Summary by CodeRabbit

  • Refactor

    • Refactored text utility functions for improved consistency and maintainability.
    • Unified text operation validation and error handling across text functions.
  • Tests

    • Added tests for text operation helpers to ensure reliability.

Co-authored-by: logbie <1138960+logbie@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings February 26, 2026 09:57
@coderabbitai

coderabbitai Bot commented Feb 26, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR adds two new text helper functions (unary_text_op and binary_text_predicate) to the helpers module for common text operations, then refactors existing text utilities to use these shared helpers, reducing code duplication and centralizing validation logic.

Changes

Cohort / File(s) Summary
Text Helper Functions
src/stdlib/helpers.rs
Added unary_text_op for single-argument text operations (touppercase, tolowercase, trim, etc.) and binary_text_predicate for two-argument text comparisons (starts_with, ends_with). Includes validation tests for argument count and type checking.
Text Utility Refactoring
src/stdlib/text.rs
Refactored text functions (touppercase, tolowercase, trim, capitalize, reverse, starts_with, ends_with) to delegate to new helpers. Unified padding logic with internal PadDirection enum. Behavior remains consistent from caller perspective.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 Text helpers hop and bound,
Unary, binary, patterns found!
Common code hops into place,
Refactored clean with elegant grace.
Shared logic makes the tests all pass! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main objective: refactoring text stdlib to reduce code duplication by introducing shared helper functions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor-text-stdlib-13566135231111813240

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/stdlib/text.rs (1)

232-254: ⚠️ Potential issue | 🔴 Critical

direction is moved and then reused in perform_pad.

At line 238, match direction consumes direction, then line 251 attempts to match it again. This is a compile-time ownership error.

💡 Proposed fix
+#[derive(Copy, Clone)]
 enum PadDirection {
     Left,
     Right,
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/stdlib/text.rs` around lines 232 - 254, The PadDirection enum is being
moved when matched in perform_pad (used to set func_name) and then reused later,
causing an ownership error; make PadDirection Copy (and Clone) so it can be
matched by value multiple times by adding #[derive(Copy, Clone)] to the
PadDirection enum declaration (or alternatively change the first match to match
&direction and use string slices), then recompile to ensure perform_pad (and its
matches) no longer cause a move error.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/stdlib/text.rs`:
- Around line 232-254: The PadDirection enum is being moved when matched in
perform_pad (used to set func_name) and then reused later, causing an ownership
error; make PadDirection Copy (and Clone) so it can be matched by value multiple
times by adding #[derive(Copy, Clone)] to the PadDirection enum declaration (or
alternatively change the first match to match &direction and use string slices),
then recompile to ensure perform_pad (and its matches) no longer cause a move
error.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 56ddf0b and 760902c.

📒 Files selected for processing (2)
  • src/stdlib/helpers.rs
  • src/stdlib/text.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors the text stdlib module to reduce code duplication by introducing generic helper functions and consolidating similar implementations. The refactoring improves maintainability while preserving all existing functionality and test coverage.

Changes:

  • Introduced unary_text_op and binary_text_predicate generic helper functions in src/stdlib/helpers.rs to eliminate boilerplate for argument checking, type extraction, and result wrapping
  • Added perform_pad helper in src/stdlib/text.rs with PadDirection enum to consolidate padleft and padright logic
  • Refactored 7 text functions (touppercase, tolowercase, trim, starts_with, ends_with, capitalize, reverse_text) to use the new helpers, reducing code duplication by approximately 60 lines

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/stdlib/helpers.rs Added unary_text_op and binary_text_predicate helper functions with comprehensive documentation and unit tests
src/stdlib/text.rs Refactored text manipulation functions to use new helpers; consolidated padleft/padright with perform_pad helper and PadDirection enum

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/stdlib/helpers.rs
Comment on lines +560 to +564
#[test]
fn test_binary_text_predicate_arg_count() {
let op = |a: &str, b: &str| a.starts_with(b);
assert!(binary_text_predicate("test_pred", vec![], op).is_err());
}

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test coverage for binary_text_predicate is incomplete. While there are tests for successful execution and incorrect argument count, there's no test verifying that the function correctly rejects arguments of the wrong type (e.g., passing a Number instead of Text). Consider adding a test similar to test_unary_text_op_wrong_type to ensure type validation works correctly.

Copilot uses AI. Check for mistakes.
Comment thread src/stdlib/text.rs
let text = expect_text(&args[0])?;
let trimmed = text.trim();
Ok(Value::Text(Arc::from(trimmed)))
unary_text_op("trim", args, |s| Arc::from(s.trim()))

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explicit Arc::from() call in the trim closure is unnecessary. The helper function unary_text_op already converts the result to Arc<str> via .into() at line 216 of helpers.rs. For consistency with touppercase, tolowercase, and other text operations, and to avoid a redundant conversion, the closure should return &str or String directly. Change to: unary_text_op("trim", args, |s| s.trim())

Copilot uses AI. Check for mistakes.
@logbie

logbie commented Feb 28, 2026

Copy link
Copy Markdown
Collaborator Author

Closing in triage: this PR is currently conflicted (DIRTY) and superseded by newer clean PRs in the same optimization/refactor sequence. Reopen or rebase if you want this exact change revived.

@logbie logbie closed this Feb 28, 2026
@google-labs-jules

Copy link
Copy Markdown
Contributor

Closing in triage: this PR is currently conflicted (DIRTY) and superseded by newer clean PRs in the same optimization/refactor sequence. Reopen or rebase if you want this exact change revived.

Understood. Acknowledging that this work is superseded and stopping work on this PR.

@logbie
logbie deleted the refactor-text-stdlib-13566135231111813240 branch June 19, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants