Skip to main content

Crate cccc_rs

Crate cccc_rs 

Source
Expand description

Rust adapter: parses source with syn and lowers the AST into the language-agnostic cccc_core::ir.

This is a pure library — it depends only on cccc-core and syn, with no CLI machinery, so embedders pay nothing for clap/ignore/rayon. The cccc-rs binary lives in the separate cccc-rs-cli crate, which combines analyze_source/DEFAULT_EXTS with the shared cccc-cli runner.

This crate contains no scoring logic — it only recognizes the constructs the engine cares about (functions/methods/closures, if/else, match, loops, labelled jumps, &&/|| sequences, calls) and emits the matching IR nodes. All complexity rules live in cccc_core::engine.

§Why a Visit-driven builder

Lowering is driven by syn’s Visit trait. Its default visit_* methods traverse the entire AST; we override only the nodes that produce IR, so a nested function or logical operator appearing in any expression position is still reached — we never have to enumerate every node kind by hand. The IR tree is assembled with a stack of “collectors”: [Builder::collect] pushes a fresh child vector, runs a sub-traversal, and pops the nodes it gathered.

§Rust-to-IR mapping notes

  • fn / impl method / trait default method / closure → Node::Function.
  • if / else if / elseNode::Branch (chaining else if as a nested Branch so it scores flat). if let / while let are just the same nodes.
  • for / while / loopNode::Loop.
  • matchNode::Switch; a _ (or bare binding) arm is the default. An arm guard (pat if cond) is visited inside the case body.
  • labelled break 'a / continue 'aNode::Jump (labeled: true).
  • && / || runs → folded Node::Logical (one node per like-operator run).
  • calls (f(..), obj.m(..)) → Node::Call for recursion detection.

Rust has no ternary (if is an expression instead) and no try/catch (errors propagate via ?), so no Conditional/Catch nodes are emitted.

Constants§

DEFAULT_EXTS
File extensions analyzed by default (when --ext is not given).

Functions§

analyze_source
Parse source and produce its FileReport, scoring via the core engine. This is the convenience entry point used by the CLI; for the raw IR (e.g. to feed a different consumer) use to_ir.
to_ir
Parse source and lower it to the complexity IR, returning the module-level nodes plus any parser error messages. syn parses a whole file at once and does not recover from syntax errors, so a parse failure yields an empty node list and a single error string.