big-code-analysis 1.1.0

Tool to compute and export code metrics
Documentation
// Per-language metric and AST modules deliberately consume the macro-
// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
// inside match expressions — explicit imports would list dozens of
// variants per arm and obscure the per-language token sets that are the
// point of these files. Allowed at the module level rather than per
// function so the per-language impl blocks stay readable.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]

use std::path::Path;
use std::sync::Arc;

use crate::abc::Abc;
use crate::alterator::Alterator;
use crate::checker::Checker;
use crate::cognitive::Cognitive;
use crate::cyclomatic::Cyclomatic;
use crate::exit::Exit;
use crate::getter::Getter;
use crate::halstead::Halstead;
use crate::langs::*;
use crate::loc::Loc;
use crate::mi::Mi;
use crate::nargs::NArgs;
use crate::node::Node;
use crate::nom::Nom;
use crate::npa::Npa;
use crate::npm::Npm;
use crate::parser::Filter;
use crate::preproc::PreprocResults;
use crate::tokens::Tokens;
use crate::wmc::Wmc;

/// A trait for callback functions.
///
/// Allows to call a private library function, getting as result
/// its output value.
pub trait Callback {
    /// The output type returned by the callee
    type Res;
    /// The input type used by the caller to pass the arguments to the callee
    type Cfg;

    /// Calls a function inside the library and returns its value
    fn call<T: ParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res;
}

/// Static identification of a language code tag.
///
/// Implemented by every `XxxCode` type generated by the internal
/// `mk_code!` macro.
pub trait LanguageInfo {
    /// Tree-sitter base language enum carried by this code tag.
    type BaseLang;

    /// Returns the workspace-level [`LANG`] variant for this code tag.
    fn get_lang() -> LANG;
    /// Returns the human-readable language name for this code tag.
    fn get_lang_name() -> &'static str;
}

// Internal language-dispatch trait kept `pub` so the macro-generated
// `Parser<T>` impls in `src/parser.rs` and the `action::<T>` /
// `Callback` plumbing in `src/macros.rs` and `src/traits.rs` can refer
// to it, but marked `#[doc(hidden)]` so it does not appear in the
// curated rustdoc surface (per issue #256). The 15 associated types
// are not part of any documented extension contract — downstream
// consumers should drive metric extraction through the non-generic
// `analyze` / `metrics_from_tree` entry points, not by implementing
// this trait. See STABILITY.md.
#[doc(hidden)]
pub trait ParserTrait {
    type Checker: Alterator + Checker;
    type Getter: Getter;
    type Cognitive: Cognitive;
    type Cyclomatic: Cyclomatic;
    type Halstead: Halstead;
    type Loc: Loc;
    type Nom: Nom;
    type Mi: Mi;
    type NArgs: NArgs;
    type Exit: Exit;
    type Wmc: Wmc;
    type Abc: Abc;
    type Npm: Npm;
    type Npa: Npa;
    type Tokens: Tokens;

    fn new(code: Vec<u8>, path: &Path, pr: Option<Arc<PreprocResults>>) -> Self;
    fn get_language(&self) -> LANG;
    fn get_root(&self) -> Node<'_>;
    fn get_code(&self) -> &[u8];
    fn get_filters(&self, filters: &[String]) -> Filter;
}

pub(crate) trait Search<'a> {
    fn first_occurrence(&self, pred: fn(u16) -> bool) -> Option<Node<'a>>;
    fn act_on_node(&self, pred: &mut dyn FnMut(&Node<'a>));
    fn first_child(&self, pred: fn(u16) -> bool) -> Option<Node<'a>>;
    fn act_on_child(&self, action: &mut dyn FnMut(&Node<'a>));
}