Refactor: reduce complexity hotspots and split large files

Refactor: reduce complexity hotspots and split large files

#31 in Riparion/riparion-cms — merged 2026-06-01

Works through the rust-code-analysis complexity cleanup list: extracts the concentrated cyclomatic/cognitive-complexity functions into focused helpers, and breaks the largest files into per-concern modules. No behavior change — public paths (crate::pages::reader::*, crate::model::*, crate::embeds::*, crate::components::sidebar::*) are preserved via flat re-exports.

Complexity hotspots

  • reader.rs (1103 SLOC) → reader/{post,comments,page,feeds,search,subscribe}.rs. CommentSection's optimistic-update + SSE reconciliation logic moved out; its submit closure extracted to submit_comment() and the merge to merge_comments().
  • seed.rs::run_if_empty (cc 43) → per-entity seed_* helpers + shared random_hex / grant_post_ownership.
  • main.rs::main (cc 30) → run_migrations, maybe_backfill_images, maybe_seed, resolve_rate_limit, spawn_live_producer, build_router.
  • server/posts.rs::get_post → image rewrite pulled into rewrite_inline_post_images.
  • server/comments.rs::create_comment (16 exits) → normalize_comment_body, resolve_commenter, enforce_comment_throttle, resolve_comment_status.
  • mdx.rs::parse_propsskip_whitespace / read_key / read_value.
  • live.rs::use_live → event match extracted to apply_live_event.

Large-file splits (modules + flat re-exports)

  • pages/widgets.rswidgets/{sidebar,feed}.rs (macros + FeedShape stay in mod).
  • model/mod.rsmodel/{content,comments,events,analytics,pages}.rs (wire_struct! macro + shared helpers stay in mod).
  • embeds.rsembeds/{splash,demos,charts}.rs (registry + dispatch in mod).
  • components/sidebar/component.rssidebar/{shell,layout,menu}.rs. styled_module! is hand-expanded so the css-module Styles + SIDEBAR_CSS are shared pub(crate); class hashes key off the style.css path, so styling is unchanged.

Verification

  • cargo fmt --check clean
  • cargo clippy clean on all three CI combos (server+sqlite, server+postgres, wasm) with the CI flags
  • cargo test --features server,sqlite — 26 passed

Note: the sidebar split touched a catalog component (hand-expanded styled_module!). A quick visual check of the admin sidebar is worth doing, and a future dx components add sidebar would overwrite the split.

🤖 Generated with Claude Code

Last updated 2026-06-02