Skip to content

Rename Chain to Handlers - #63

Merged
letmutex merged 3 commits into
mainfrom
rename-chain
Nov 24, 2025
Merged

letmutex merged 3 commits into
mainfrom
rename-chain

Conversation

@letmutex

@letmutex letmutex commented Nov 24, 2025 •

Copy link
Copy Markdown
Owner
  • Rename Chain to Handlers
  • Rename Chain.proceed to Handlers.fallback

Summary by CodeRabbit

  • Refactor
    • Internal API improvements for better maintainability and clarity.

✏️ 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 pull request performs a comprehensive trait renaming and refactoring across the element handler subsystem. The Chain trait is renamed to Handlers, and the proceed method is renamed to fallback. All handler function signatures, imports, and internal call sites are updated to use the new trait name and parameter names. Additionally, a new HrStyle::Underscores variant is added to support an additional horizontal rule style.

Changes

Cohort / File(s) Change Summary
Core trait definition
src/element_handler/mod.rs
Trait Chain renamed to Handlers; method proceed renamed to fallback. ElementHandler::handle, generic bounds, and block/bold/italic helpers updated to use &dyn Handlers instead of &dyn Chain. All internal calls to walk_children, handle, and serialize_element updated to use new handlers context.
Utility functions
src/element_handler/element_util.rs
Functions handle_or_serialize_by_parent and serialize_element refactored to accept &dyn Handlers instead of &dyn Chain. Macro serialize_if_faithful! parameter updated from $chain: expr to $handlers: expr. All internal references updated to route through handlers.
Individual element handlers
src/element_handler/{anchor,blockquote,br,caption,code,emphasis,head_body,headings,html,img,li,p,pre,td_th,tbody,thead,tr}.rs
Handler function signatures changed to accept handlers: &dyn Handlers instead of chain: &dyn Chain. All macro invocations (serialize_if_faithful!) and method calls (walk_children, handle_or_serialize_by_parent) updated to use handlers parameter. Imports adjusted to include Handlers and remove Chain.
Complex handlers
src/element_handler/{list,table,code}.rs
Handler function signatures and helper function signatures updated to accept Handlers. All internal logic paths (serialize_element, handle, walk_children) refactored to route through handlers. Helper functions like get_ol_content and extract_row_cells updated accordingly.
Horizontal rule styling
src/element_handler/hr.rs
In addition to Chain → Handlers refactoring, new enum variant HrStyle::Underscores added with corresponding match arm returning "\n\n_ _ _\n\n".
Public API and library entry points
src/lib.rs
Import changed from element_handler::Chain to element_handler::Handlers. Documentation examples and skip_tags builder method closure signature updated to use &dyn Handlers.
Tests and examples
tests/basic_tests.rs, examples/simple/main.rs, README.md
Handler trait references and method calls updated from Chain/proceed to Handlers/fallback. Closure parameter types updated from &dyn Chain to &dyn Handlers. All usage examples reflect new API.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client Code
    participant Handler as ElementHandler
    participant Handlers as Handlers Trait
    participant Context as Element Context

    Client->>Handler: handle(handlers, element)
    Handler->>Handlers: walk_children(node)
    Handlers->>Context: traverse children
    Context-->>Handlers: return content
    Handlers-->>Handler: return HandlerResult
    Note over Handler: Old: chain.proceed(element)<br/>New: handlers.fallback(element)
    alt Match found
        Handler-->>Client: return HandlerResult
    else No match
        Handler->>Handlers: fallback(element)
        Handlers->>Handler: try next handler in chain
        Handler-->>Client: return HandlerResult
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Large file scope (~25 files affected) – While the number is substantial, the changes are highly homogeneous and follow a consistent pattern across all handlers.
  • High pattern repetition – The same refactoring (trait rename, parameter rename, import update) is mechanically applied across all element handlers, reducing cognitive load per file.
  • Core trait changes in mod.rs – The central trait definition and method renaming require careful review to ensure all call sites are correctly updated throughout the codebase.
  • Minor functional addition – The new HrStyle::Underscores variant is straightforward but should be verified for consistency.

Areas warranting extra attention:

  • src/element_handler/mod.rs – Verify trait definition, generic bounds, and all impl blocks are correctly updated.
  • src/element_handler/element_util.rs – Ensure macro serialize_if_faithful! parameter propagation is correct across all call sites.
  • Complex handlers (src/element_handler/{list,table,code}.rs) – Confirm helper function signatures and internal routing through handlers is consistent.
  • Cross-file consistency – Spot-check a few different handler implementations to ensure the pattern is uniformly applied.

Possibly related PRs

  • Refactor walk_children() #62 – Introduces walk_children exposure on the handler context and threads context through serialize_element and element handlers; this PR builds directly upon that refactoring by renaming the context trait and method.
  • Enhanced ElementHandler API #59 – Modifies the element handler API and trait signatures (ElementHandler/ElementHandlers); this PR's rename of Chain → Handlers and proceed → fallback refactors the same core abstractions.
  • Refactor element handlers #60 – Modifies the element handler subsystem with changes to handler traits and per-element handler signatures; related through shared handler infrastructure updates.

Poem

🐰 A chain by any other name is still a path,
Yet "handlers" feels more true to life,
From anchor to table, through every leaf and graph,
The refactoring flows without strife.
Old proceed becomes fallback true,
A hop, a skip—the API's new! 🎉

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 32.50% 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 'Rename Chain to Handlers' accurately and clearly describes the primary change throughout the entire changeset, which involves systematically renaming the Chain trait to Handlers and proceed method to fallback.
✨ 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 rename-chain

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 (7)
src/lib.rs (1)

18-21: Handlers integration in builder API looks correct

The new Handlers import and the skip_tags closure signature (|_: &dyn Handlers, _: Element| None) line up with the updated ElementHandler trait and Handlers-based dispatch. Only a minor cosmetic nit: you could merge the two element_handler imports into a single use crate::element_handler::{ElementHandler, ElementHandlers, Handlers}; to keep imports consolidated.

Also applies to: 178-181

src/element_handler/head_body.rs (1)

4-12: Head/body handler correctly migrated to Handlers

The signature and call into handle_or_serialize_by_parent now use &dyn Handlers consistently, preserving behavior. If you ever revisit this, &["html"] would avoid allocating a Vec on each call, but that’s purely a micro-optimization here.

src/element_handler/table.rs (1)

2-2: table_handler / extract_row_cells Handlers wiring is correct

The table pipeline now consistently depends on &dyn Handlers:

  • table_handler takes handlers: &dyn Handlers and uses it for serialize_if_faithful!, handle, walk_children, and serialize_element.
  • extract_row_cells accepts handlers and uses handlers.handle for each cell, propagating markdown_translated via the translated flag.

This matches the previous Chain behavior while aligning with the new abstraction.

One optional improvement: caption handling (Line 44) pushes res.content but doesn’t feed res.markdown_translated into all_children_translated, even though the comment suggests “All child table elements” must be translated in faithful mode. If you want captions to participate in that decision, consider:

-                    "caption" => {
-                        if let Some(res) = handlers.handle(&child) {
-                            captions.push(res.content.trim().to_string());
-                        }
-                    }
+                    "caption" => {
+                        if let Some(res) = handlers.handle(&child) {
+                            if !res.markdown_translated {
+                                all_children_translated = false;
+                            }
+                            captions.push(res.content.trim().to_string());
+                        }
+                    }

Also applies to: 18-19, 44-44, 62-62, 66-66, 79-79, 90-90, 101-101, 105-105, 113-113, 128-128, 135-135, 151-151, 182-182, 193-193

src/element_handler/hr.rs (1)

3-14: Hr handler correctly migrated to Handlers; consider tests for new style

The migration from Chain to Handlers is wired correctly (handlers is passed into serialize_if_faithful! and not otherwise used), and the new HrStyle::Underscores arm cleanly extends the existing match without changing prior behavior. If not already covered elsewhere, consider adding a focused test for the Underscores style to lock in the new output format.

tests/basic_tests.rs (1)

533-542: Custom handler tests correctly use Handlers and fallback

Both add_handler usages now take &dyn Handlers, and the fallback path calls handlers.fallback(element) when the id="do_not_skip_me" attribute is present. This mirrors the old Chain::proceed behavior and keeps the semantics of “skip vs. delegate to default handling” intact. As an optional idiomatic tweak, you could replace .iter().find(...).is_some() with .iter().any(...) to express the intent more directly.

Also applies to: 545-571

src/element_handler/caption.rs (1)

4-15: Caption handler migration to Handlers matches helper expectations

The caption handler now consistently uses &dyn Handlers for both serialize_if_faithful! and handle_or_serialize_by_parent, which aligns with the helper’s current signature and keeps behavior unchanged. As a future clean‑up (outside this PR’s scope), you might consider changing handle_or_serialize_by_parent to take a slice (&[&str]) instead of &Vec<&str> so callers don’t need to allocate via vec!["table"] each time.

src/element_handler/thead.rs (1)

4-17: Thead handler correctly switched to Handlers with unchanged semantics

The handler now threads &dyn Handlers through serialize_if_faithful! and handle_or_serialize_by_parent as expected, preserving the previous markdown/faithful behavior for <thead>. As with caption_handler, you could eventually move handle_or_serialize_by_parent to accept a slice instead of &Vec<&str> to avoid repeated small allocations at call sites.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee96b37 and 911bd61.

📒 Files selected for processing (26)
  • README.md (1 hunks)
  • examples/simple/main.rs (2 hunks)
  • src/element_handler/anchor.rs (4 hunks)
  • src/element_handler/blockquote.rs (1 hunks)
  • src/element_handler/br.rs (1 hunks)
  • src/element_handler/caption.rs (1 hunks)
  • src/element_handler/code.rs (6 hunks)
  • src/element_handler/element_util.rs (7 hunks)
  • src/element_handler/emphasis.rs (1 hunks)
  • src/element_handler/head_body.rs (1 hunks)
  • src/element_handler/headings.rs (1 hunks)
  • src/element_handler/hr.rs (1 hunks)
  • src/element_handler/html.rs (2 hunks)
  • src/element_handler/img.rs (2 hunks)
  • src/element_handler/li.rs (1 hunks)
  • src/element_handler/list.rs (5 hunks)
  • src/element_handler/mod.rs (3 hunks)
  • src/element_handler/p.rs (1 hunks)
  • src/element_handler/pre.rs (3 hunks)
  • src/element_handler/table.rs (11 hunks)
  • src/element_handler/tbody.rs (1 hunks)
  • src/element_handler/td_th.rs (1 hunks)
  • src/element_handler/thead.rs (1 hunks)
  • src/element_handler/tr.rs (1 hunks)
  • src/lib.rs (2 hunks)
  • tests/basic_tests.rs (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (16)
src/element_handler/head_body.rs (1)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
src/element_handler/emphasis.rs (1)
src/element_handler/code.rs (1)
  • content (121-121)
src/element_handler/blockquote.rs (1)
src/element_handler/code.rs (1)
  • content (121-121)
src/element_handler/td_th.rs (1)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
examples/simple/main.rs (1)
src/lib.rs (1)
  • options (173-176)
src/element_handler/tr.rs (1)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
src/element_handler/tbody.rs (1)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
src/element_handler/caption.rs (1)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
src/element_handler/pre.rs (1)
src/element_handler/element_util.rs (1)
  • serialize_element (46-177)
src/element_handler/code.rs (3)
src/element_handler/element_util.rs (1)
  • serialize_element (46-177)
src/node_util.rs (2)
  • get_node_tag_name (5-11)
  • get_parent_node (13-24)
src/lib.rs (1)
  • options (173-176)
src/element_handler/thead.rs (1)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
src/element_handler/hr.rs (1)
src/lib.rs (1)
  • options (173-176)
src/element_handler/table.rs (1)
src/element_handler/element_util.rs (1)
  • serialize_element (46-177)
src/element_handler/html.rs (1)
src/element_handler/element_util.rs (1)
  • serialize_element (46-177)
src/element_handler/mod.rs (4)
src/element_handler/anchor.rs (1)
  • handle (32-78)
src/dom_walker.rs (1)
  • walk_children (105-158)
src/element_handler/element_util.rs (1)
  • serialize_element (46-177)
src/element_handler/emphasis.rs (1)
  • emphasis_handler (8-31)
src/element_handler/anchor.rs (2)
src/element_handler/mod.rs (5)
  • handle (89-89)
  • handle (96-98)
  • handle (250-292)
  • handle (309-309)
  • handle (326-334)
src/element_handler/code.rs (1)
  • content (121-121)
🔇 Additional comments (23)
src/element_handler/element_util.rs (1)

4-4: LGTM! Consistent trait renaming throughout the file.

The renaming from Chain to Handlers is applied consistently across imports, function signatures, parameters, internal calls, and macro documentation. All references have been updated correctly.

Also applies to: 17-17, 31-31, 35-35, 46-46, 71-71, 185-186, 196-196

examples/simple/main.rs (1)

1-1: LGTM! Example code updated correctly.

The import and closure parameter have been updated to use Handlers, and the simplified return statement is a valid improvement (removing unnecessary .to_string() call).

Also applies to: 30-31

README.md (1)

62-62: LGTM! Documentation updated to reflect the new API.

The import path and closure example have been updated correctly to use Handlers instead of Chain.

Also applies to: 65-65

src/element_handler/blockquote.rs (1)

3-3: LGTM! Handler function updated consistently.

The function signature, imports, macro invocation, and method calls have all been updated to use Handlers consistently.

Also applies to: 8-13

src/element_handler/tbody.rs (1)

4-4: LGTM! Handler function updated consistently with improved formatting.

The function signature and calls have been updated to use Handlers, and the multi-line formatting of handle_or_serialize_by_parent improves readability.

Also applies to: 8-17

src/element_handler/headings.rs (1)

3-3: LGTM! Handler function updated consistently.

The imports, function signature, macro invocation, and method calls have all been updated to use Handlers correctly.

Also applies to: 9-12

src/element_handler/tr.rs (1)

4-4: LGTM! Handler function updated consistently.

The imports, function signature, and all parameter passing have been updated to use Handlers correctly.

Also applies to: 8-13

src/element_handler/img.rs (1)

3-3: LGTM! Handler function updated consistently.

The imports, function signature, and macro invocation have all been updated to use Handlers correctly.

Also applies to: 8-8, 23-23

src/element_handler/td_th.rs (1)

4-10: Handlers rename here is consistent and behavior‑preserving

Import and signature changes from Chain to Handlers look correct, and the uses of serialize_if_faithful! and handle_or_serialize_by_parent still match the expected API and semantics for <td>/<th> handling.

src/lib.rs (1)

185-199: Docs/examples correctly updated to Handlers

The example now imports and uses element_handler::Handlers and &dyn Handlers consistently with the new public API; this should keep the README/tests in sync with the trait rename.

src/element_handler/emphasis.rs (1)

3-15: Emphasis handler Handlers migration is behavior‑preserving

emphasis_handler now takes &dyn Handlers and uses it in serialize_if_faithful! and walk_children; this is a direct, correct swap from Chain with no logic changes.

src/element_handler/anchor.rs (1)

5-77: Anchor handler correctly updated to use Handlers

The ElementHandler impl now takes &dyn Handlers and uses it consistently in serialize_if_faithful! and both walk_children call sites. The missing-href fallback and attribute handling logic remain unchanged aside from the trait rename.

src/element_handler/li.rs (1)

3-46: List item handler Handlers refactor is consistent

list_item_handler now uses &dyn Handlers for faithful-mode checks and child traversal; the UL/OL branching and whitespace handling are unchanged, so behavior should remain identical.

src/element_handler/list.rs (1)

5-71: List handling correctly wired through Handlers

The import, list_handler signature, and get_ol_content signature all now use Handlers, and all internal calls (serialize_if_faithful!, serialize_element, walk_children, handle) are consistently updated to the new trait. Faithful-mode early serialization and the ordered-list numbering logic are preserved without behavioral changes.

Also applies to: 78-132

src/element_handler/pre.rs (1)

3-3: pre_handler Handlers refactor looks consistent

Signature, imports, serialize_if_faithful!, walk_children, and serialize_element all now use &dyn Handlers coherently; behavior should be identical to the former Chain-based version.

Also applies to: 10-11, 29-29, 35-35, 44-44

src/element_handler/html.rs (1)

5-5: html_handler correctly switched to Handlers

The handler signature and internal calls (walk_children, serialize_element) are all updated to &dyn Handlers with no behavior change.

Also applies to: 11-11, 25-25, 30-30

src/element_handler/p.rs (1)

3-3: p_handler Handlers migration is straightforward and correct

The function now cleanly depends on &dyn Handlers and wires through serialize_if_faithful! and walk_children as before; no regressions spotted.

Also applies to: 8-10

src/element_handler/code.rs (1)

8-8: Code element handlers cleanly migrated to Handlers

code_handler, handle_code_block, and handle_inline_code now consistently use &dyn Handlers for serialize_if_faithful!, walk_children, and serialize_element. This preserves the existing behavior while aligning with the new trait.

Also applies to: 15-15, 27-27, 39-41, 46-50, 65-65, 76-76, 112-113, 120-120

src/element_handler/mod.rs (3)

81-99: ElementHandler now depends on &dyn Handlers – rename and Fn bound look correct

The ElementHandler::handle signature and the impl<F> ElementHandler for F bound cleanly switch from the old Chain to Handlers without changing behavior; using &dyn Handlers keeps the trait object–based dispatch intact and matches how ElementHandlers implements Handlers.


301-348: Handlers trait and its impl for ElementHandlers preserve chain/fallback semantics

The new Handlers trait cleanly exposes fallback, handle, and walk_children, and the impl Handlers for ElementHandlers correctly implements fallback by incrementing skipped_handlers and delegating to the existing ElementHandlers::handle. This matches the prior chaining behavior while only renaming proceed → fallback.


363-382: Block/bold/italic helpers correctly migrated to use Handlers

block_handler, bold_handler, and italic_handler now take &dyn Handlers, use handlers.walk_children, and delegate to emphasis_handler(handlers, ...), which aligns with the updated emphasis_handler and serialize_element signatures. Behavior should remain unchanged aside from the trait rename.

src/element_handler/br.rs (1)

3-14: BR handler rename to Handlers is consistent and behavior-preserving

The import, parameter type, and serialize_if_faithful! invocation are all correctly updated to use Handlers, and the BrStyle match logic is unchanged. Looks good.

tests/basic_tests.rs (1)

6-10: Test module import updated to Handlers correctly

Switching the import from element_handler::Chain to element_handler::Handlers keeps the tests aligned with the public API rename while leaving behavior unchanged.

@letmutex
letmutex merged commit 942b726 into main Nov 24, 2025
@letmutex
letmutex deleted the rename-chain branch November 24, 2025 03:50
@coderabbitai coderabbitai Bot mentioned this pull request 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