Skip to content

Move options from Element to Handlers - #64

Merged
letmutex merged 1 commit into
mainfrom
move-options
Nov 24, 2025
Merged

letmutex merged 1 commit into
mainfrom
move-options

Conversation

@letmutex

@letmutex letmutex commented Nov 24, 2025 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Refactor
    • Restructured internal configuration access patterns to use a unified interface, improving architectural consistency across the codebase.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Nov 24, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR refactors option access patterns throughout the codebase by removing the options field from the Element struct and introducing a new options() method on the Handlers trait. All element handlers are updated to access options through the centralized handlers interface rather than element-specific options.

Changes

Cohort / File(s) Summary
Core API changes
src/lib.rs, src/element_handler/mod.rs
Removed options: &'a Options field from Element struct; added new options(&self) -> &Options method to Handlers trait and implemented it in ElementHandlers; removed options field assignments during Element construction
Handler option access refactor
src/element_handler/anchor.rs, src/element_handler/br.rs, src/element_handler/code.rs, src/element_handler/headings.rs, src/element_handler/hr.rs, src/element_handler/html.rs, src/element_handler/li.rs, src/element_handler/list.rs, src/element_handler/pre.rs, src/element_handler/table.rs
Replaced direct access to element.options with handlers.options() for option retrieval across all handlers (link_style, br_style, code_block_style, heading_style, hr_style, translation_mode, bullet_list_marker, ul_bullet_spacing, etc.)
Utility macro updates
src/element_handler/element_util.rs
Updated handle_or_serialize_by_parent function and serialize_if_faithful macro to access translation_mode via handlers.options() instead of element.options

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Attention areas:
    • Verify the new options() trait method implementation in ElementHandlers correctly exposes the same options previously accessed via element
    • Confirm all handler files consistently access the same centralized options (no inconsistencies where some handlers might still expect element-scoped options)
    • Check that removing the options field from Element doesn't break any downstream code paths not covered by the handlers
    • Validate that the behavioral semantics remain identical (e.g., whether translation_mode applies globally now vs. per-element, if that distinction matters)

Possibly related PRs

  • Refactor walk_children() #62: Refactors element/handler interfaces and modifies ElementHandlers call patterns and Element construction across multiple handlers
  • Rename Chain to Handlers #63: Introduces Handlers API migration (replacing Chain pattern) and the centralized options access via handlers.options() aligns with this API design
  • Enhanced ElementHandler API #59: Previously introduced Element.options/Chain-based API, which this PR now supersedes by shifting option reads to handlers.options()

Poem

🐰 The handlers hop with glee,
No more scattered options be!
One source of truth, so clean and bright,
Centralizing all the right.
A rabbit's dream: one path to choose,
Less confusion, fewer duels! 🥬

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.11% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: moving options access from the Element struct to the Handlers trait/implementation, which is the core refactoring across all modified files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch move-options

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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/element_handler/anchor.rs (1)

64-75: Centralized link options are correct; consider caching handlers.options() locally

Reading link_style and link_reference_style from handlers.options() is correct and matches the new design. To avoid calling the virtual options() accessor twice and to make it clear both values come from the same snapshot, you could cache it in a local:

-        let md = match handlers.options().link_style {
+        let options = handlers.options();
+        let md = match options.link_style {
             LinkStyle::Inlined => self.build_inlined_anchor(&content, link, title, false),
             LinkStyle::InlinedPreferAutolinks => {
                 self.build_inlined_anchor(&content, link, title, true)
             }
             LinkStyle::Referenced => self.build_referenced_anchor(
                 &content,
                 link,
                 title,
-                &handlers.options().link_reference_style,
+                &options.link_reference_style,
             ),
         };

Functionality remains the same; this just tightens up the implementation.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 942b726 and 61da591.

📒 Files selected for processing (13)
  • src/element_handler/anchor.rs (2 hunks)
  • src/element_handler/br.rs (1 hunks)
  • src/element_handler/code.rs (3 hunks)
  • src/element_handler/element_util.rs (2 hunks)
  • src/element_handler/headings.rs (1 hunks)
  • src/element_handler/hr.rs (1 hunks)
  • src/element_handler/html.rs (1 hunks)
  • src/element_handler/li.rs (1 hunks)
  • src/element_handler/list.rs (3 hunks)
  • src/element_handler/mod.rs (3 hunks)
  • src/element_handler/pre.rs (1 hunks)
  • src/element_handler/table.rs (1 hunks)
  • src/lib.rs (0 hunks)
💤 Files with no reviewable changes (1)
  • src/lib.rs
🧰 Additional context used
🧬 Code graph analysis (3)
src/element_handler/html.rs (1)
src/node_util.rs (1)
  • get_parent_node (13-24)
src/element_handler/element_util.rs (2)
src/element_handler/mod.rs (2)
  • options (313-313)
  • options (350-352)
src/lib.rs (1)
  • options (171-174)
src/element_handler/mod.rs (1)
src/lib.rs (1)
  • options (171-174)
🔇 Additional comments (15)
src/element_handler/mod.rs (2)

368-378: Using handlers.options().translation_mode in block_handler aligns with new options model

The switch to handlers.options().translation_mode in block_handler keeps behavior equivalent while routing configuration consistently through the Handlers abstraction rather than per‑element state. No correctness issues spotted.


302-314: Handlers trait addition is confirmed a public API breaking change; ensure semver bump and external audit

The Handlers trait is indeed publicly exported as part of the crate's API surface (pub mod element_handler; in lib.rs, with pub trait Handlers at line 302). Adding the required fn options(&self) -> &Options; method is a source-breaking change for any external crate implementing Handlers—they will fail to compile until they add this method.

Within the codebase, only ElementHandlers implements Handlers (at line 316), and it correctly implements the new method. The usage in block_handler aligns with centralizing option access.

Action: Confirm this breaking change is intentional and reflected in a semver major version bump and changelog entry. If not intentional, consider sealing the trait or making it private.

src/element_handler/br.rs (1)

11-14: BR style now sourced from handlers.options(); looks correct

Using handlers.options().br_style instead of a per‑element options reference is consistent with the new design and preserves the existing TwoSpaces/Backslash behavior.

src/element_handler/hr.rs (1)

10-14: HR style via handlers.options().hr_style is consistent and correct

The match on handlers.options().hr_style cleanly centralizes configuration without altering the Dashes/Asterisks/Underscores rendering semantics.

src/element_handler/table.rs (1)

126-131: Faithful-mode gate now uses handlers.options(); behavior preserved

The condition

if handlers.options().translation_mode == TranslationMode::Faithful && !all_children_translated {
    // serialize element as HTML
}

is equivalent to the prior per‑element check but aligned with the global options access pattern. The fallback to raw HTML when any cell is not markdown‑translated remains correct.

src/element_handler/headings.rs (1)

16-24: Heading style selection via handlers.options().heading_style looks good

Using handlers.options().heading_style to choose Setext vs ATX for h1/h2 keeps behavior intact while matching the new centralized options approach. No issues here.

src/element_handler/li.rs (1)

21-31: UL list item now respects global bullet options; looks correct

Using handlers.options().bullet_list_marker and .ul_bullet_spacing inside ul_li matches the move to handler‑level options and preserves the existing bullet/spacing behavior.

src/element_handler/code.rs (1)

18-30: Code handler option reads now go through handlers.options(); semantics unchanged

The three refactors here—

  • Faithful-mode guard in code_handler using handlers.options().translation_mode,
  • Fenced vs indented code blocks and fence symbol selection using handlers.options().code_block_style / .code_block_fence,
  • Inline code preformatting toggle using handlers.options().preformatted_code,

all correctly move configuration lookup to the Handlers abstraction without altering behavior. The existing faithful-mode fallbacks and formatting logic remain intact.

Also applies to: 52-57, 134-138

src/element_handler/pre.rs (1)

28-31: LGTM! Option access correctly refactored.

The changes correctly replace element.options.translation_mode with handlers.options().translation_mode, maintaining the same logic for both the Pure mode check and the Faithful mode fallback serialization.

src/element_handler/element_util.rs (2)

27-27: LGTM! Consistent refactoring for faithful mode check.

The change correctly updates the translation mode check to use handlers.options() instead of element.options, maintaining the same behavior for parent tag validation in faithful mode.


191-191: LGTM! Macro correctly updated for centralized option access.

The serialize_if_faithful macro is correctly updated to access translation mode via handlers.options(). Since this macro is used across multiple handlers, this ensures consistent option access throughout the codebase.

src/element_handler/list.rs (3)

14-14: LGTM! Faithful mode check correctly updated.

The translation mode check is correctly refactored to use handlers.options() for the faithful mode validation at the start of the list handler.


53-54: LGTM! Post-processing faithful check correctly updated.

The faithful mode check after processing OL content is correctly refactored to use handlers.options(), maintaining the same serialization fallback behavior.


125-130: LGTM! Options parameter correctly passed to helper function.

The refactoring correctly passes handlers.options() instead of element.options to add_ol_li_marker, which expects a reference to Options for computing list numbering and spacing.

src/element_handler/html.rs (1)

14-21: LGTM! Translation mode checks correctly refactored.

The markdown_translatable logic is correctly updated to access translation mode via handlers.options() for both the Faithful mode check (line 14) and the Pure mode fallback (line 21), maintaining the same conditional behavior for HTML element translation.

@letmutex
letmutex merged commit 2da235c into main Nov 24, 2025
@letmutex
letmutex deleted the move-options branch November 24, 2025 04:41
This was referenced Dec 10, 2025
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.

1 participant