Repository navigation
Rename Chain to Handlers - #63
Conversation
WalkthroughThis pull request performs a comprehensive trait renaming and refactoring across the element handler subsystem. The Changes
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Areas warranting extra attention:
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (7)
src/lib.rs (1)
18-21: Handlers integration in builder API looks correctThe new
Handlersimport and theskip_tagsclosure signature (|_: &dyn Handlers, _: Element| None) line up with the updatedElementHandlertrait and Handlers-based dispatch. Only a minor cosmetic nit: you could merge the twoelement_handlerimports into a singleuse 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 HandlersThe signature and call into
handle_or_serialize_by_parentnow use&dyn Handlersconsistently, preserving behavior. If you ever revisit this,&["html"]would avoid allocating aVecon 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 correctThe table pipeline now consistently depends on
&dyn Handlers:
table_handlertakeshandlers: &dyn Handlersand uses it forserialize_if_faithful!,handle,walk_children, andserialize_element.extract_row_cellsacceptshandlersand useshandlers.handlefor each cell, propagatingmarkdown_translatedvia thetranslatedflag.This matches the previous
Chainbehavior while aligning with the new abstraction.One optional improvement: caption handling (Line 44) pushes
res.contentbut doesn’t feedres.markdown_translatedintoall_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 toHandlers; consider tests for new styleThe migration from
ChaintoHandlersis wired correctly (handlersis passed intoserialize_if_faithful!and not otherwise used), and the newHrStyle::Underscoresarm cleanly extends the existing match without changing prior behavior. If not already covered elsewhere, consider adding a focused test for theUnderscoresstyle to lock in the new output format.tests/basic_tests.rs (1)
533-542: Custom handler tests correctly useHandlersandfallbackBoth
add_handlerusages now take&dyn Handlers, and the fallback path callshandlers.fallback(element)when theid="do_not_skip_me"attribute is present. This mirrors the oldChain::proceedbehavior 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 toHandlersmatches helper expectationsThe caption handler now consistently uses
&dyn Handlersfor bothserialize_if_faithful!andhandle_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 changinghandle_or_serialize_by_parentto take a slice (&[&str]) instead of&Vec<&str>so callers don’t need to allocate viavec!["table"]each time.src/element_handler/thead.rs (1)
4-17: Thead handler correctly switched toHandlerswith unchanged semanticsThe handler now threads
&dyn Handlersthroughserialize_if_faithful!andhandle_or_serialize_by_parentas expected, preserving the previous markdown/faithful behavior for<thead>. As withcaption_handler, you could eventually movehandle_or_serialize_by_parentto 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
📒 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
ChaintoHandlersis 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
Handlersinstead ofChain.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
Handlersconsistently.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 ofhandle_or_serialize_by_parentimproves 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
Handlerscorrectly.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
Handlerscorrectly.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
Handlerscorrectly.Also applies to: 8-8, 23-23
src/element_handler/td_th.rs (1)
4-10: Handlers rename here is consistent and behavior‑preservingImport and signature changes from
ChaintoHandlerslook correct, and the uses ofserialize_if_faithful!andhandle_or_serialize_by_parentstill match the expected API and semantics for<td>/<th>handling.src/lib.rs (1)
185-199: Docs/examples correctly updated to HandlersThe example now imports and uses
element_handler::Handlersand&dyn Handlersconsistently 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_handlernow takes&dyn Handlersand uses it inserialize_if_faithful!andwalk_children; this is a direct, correct swap fromChainwith no logic changes.src/element_handler/anchor.rs (1)
5-77: Anchor handler correctly updated to use HandlersThe
ElementHandlerimpl now takes&dyn Handlersand uses it consistently inserialize_if_faithful!and bothwalk_childrencall sites. The missing-hreffallback 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_handlernow uses&dyn Handlersfor 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 HandlersThe import,
list_handlersignature, andget_ol_contentsignature all now useHandlers, 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 consistentSignature, imports,
serialize_if_faithful!,walk_children, andserialize_elementall now use&dyn Handlerscoherently; behavior should be identical to the formerChain-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 HandlersThe handler signature and internal calls (
walk_children,serialize_element) are all updated to&dyn Handlerswith 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 correctThe function now cleanly depends on
&dyn Handlersand wires throughserialize_if_faithful!andwalk_childrenas 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, andhandle_inline_codenow consistently use&dyn Handlersforserialize_if_faithful!,walk_children, andserialize_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 correctThe
ElementHandler::handlesignature and theimpl<F> ElementHandler for Fbound cleanly switch from the oldChaintoHandlerswithout changing behavior; using&dyn Handlerskeeps the trait object–based dispatch intact and matches howElementHandlersimplementsHandlers.
301-348: Handlers trait and its impl for ElementHandlers preserve chain/fallback semanticsThe new
Handlerstrait cleanly exposesfallback,handle, andwalk_children, and theimpl Handlers for ElementHandlerscorrectly implementsfallbackby incrementingskipped_handlersand delegating to the existingElementHandlers::handle. This matches the prior chaining behavior while only renamingproceed→fallback.
363-382: Block/bold/italic helpers correctly migrated to use Handlers
block_handler,bold_handler, anditalic_handlernow take&dyn Handlers, usehandlers.walk_children, and delegate toemphasis_handler(handlers, ...), which aligns with the updatedemphasis_handlerandserialize_elementsignatures. Behavior should remain unchanged aside from the trait rename.src/element_handler/br.rs (1)
3-14: BR handler rename toHandlersis consistent and behavior-preservingThe import, parameter type, and
serialize_if_faithful!invocation are all correctly updated to useHandlers, and theBrStylematch logic is unchanged. Looks good.tests/basic_tests.rs (1)
6-10: Test module import updated toHandlerscorrectlySwitching the import from
element_handler::Chaintoelement_handler::Handlerskeeps the tests aligned with the public API rename while leaving behavior unchanged.
ChaintoHandlersChain.proceedtoHandlers.fallbackSummary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.