Skip to content

Commit

Permalink
Add metrics for Rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Nov 20, 2019
1 parent 00d4307 commit 8b65075
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ impl Checker for RustCode {

mk_checker!(is_string, StringLiteral, RawStringLiteral);
mk_checker!(is_call, CallExpression);
mk_checker!(is_func, FunctionItem);
mk_checker!(is_func_space, SourceFile, FunctionItem, ImplItem);
mk_checker!(is_func, FunctionItem, ClosureExpression);
mk_checker!(
is_func_space,
SourceFile,
FunctionItem,
ImplItem,
TraitItem,
ClosureExpression
);
}
14 changes: 13 additions & 1 deletion src/cyclomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ impl Cyclomatic for TsxCode {
}
}

impl Cyclomatic for RustCode {
fn compute(node: &Node, stats: &mut Stats) {
use Rust::*;

match node.kind_id().into() {
If | For | While | Loop | MatchArm | MatchArm2 | QMARK | AMPAMP | PIPEPIPE => {
stats.cyclomatic += 1.;
}
_ => {}
}
}
}

impl Cyclomatic for PreprocCode {}
impl Cyclomatic for CcommentCode {}
impl Cyclomatic for CCode {}
Expand All @@ -126,4 +139,3 @@ impl Cyclomatic for JavaCode {}
impl Cyclomatic for GoCode {}
impl Cyclomatic for CssCode {}
impl Cyclomatic for HtmlCode {}
impl Cyclomatic for RustCode {}
2 changes: 1 addition & 1 deletion src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn dump_tree_helper(
write!(&mut stdout, "{}{}", prefix, pref).unwrap();

color!(stdout, Yellow, true);
write!(&mut stdout, "{{{}}} ", node.kind()).unwrap();
write!(&mut stdout, "{{{}:{}}} ", node.kind(), node.kind_id()).unwrap();

color!(stdout, White);
write!(&mut stdout, "from ").unwrap();
Expand Down
4 changes: 4 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum NodeKind {
Function,
Class,
Struct,
Trait,
Impl,
Unit,
}

Expand All @@ -16,6 +18,8 @@ impl fmt::Display for NodeKind {
NodeKind::Function => "function",
NodeKind::Class => "class",
NodeKind::Struct => "struct",
NodeKind::Trait => "trait",
NodeKind::Impl => "impl",
NodeKind::Unit => "unit",
};
write!(f, "{}", s)
Expand Down
16 changes: 15 additions & 1 deletion src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ impl Getter for TsxCode {
}
}

impl Getter for RustCode {
fn get_kind(node: &Node) -> NodeKind {
use Rust::*;

let typ = node.kind_id();
match typ.into() {
FunctionItem | ClosureExpression => NodeKind::Function,
TraitItem => NodeKind::Trait,
ImplItem => NodeKind::Impl,
SourceFile => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}
}

impl Getter for PreprocCode {}
impl Getter for CcommentCode {}
impl Getter for CCode {}
Expand All @@ -108,4 +123,3 @@ impl Getter for JavaCode {}
impl Getter for GoCode {}
impl Getter for CssCode {}
impl Getter for HtmlCode {}
impl Getter for RustCode {}
24 changes: 23 additions & 1 deletion src/halstead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ impl Halstead for TsxCode {
}
}

impl Halstead for RustCode {
fn compute<'a>(node: &Node<'a>, code: &'a [u8], stats: &mut Stats<'a>) {
use Rust::*;

let id = node.kind_id();
match id.into() {
LPAREN | LBRACE | LBRACK | EQGT | PLUS | STAR | Async | Await | Continue | For | If
| Let | Loop | Match | Return | Unsafe | While | BANG | EQ | COMMA | DASHGT | QMARK
| LT | GT | AMP | MutableSpecifier | DOTDOT | DOTDOTEQ | DASH | AMPAMP | PIPEPIPE
| PIPE | CARET | EQEQ | BANGEQ | LTEQ | GTEQ | LTLT | GTGT | SLASH | PERCENT
| PLUSEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ | AMPEQ | PIPEEQ | CARETEQ
| LTLTEQ | GTGTEQ | Move | DOT => {
*stats.operators.entry(id).or_insert(0) += 1;
}
Identifier | StringLiteral | RawStringLiteral | IntegerLiteral | FloatLiteral
| BooleanLiteral | Zelf | CharLiteral | UNDERSCORE => {
*stats.operands.entry(get_id(node, code)).or_insert(0) += 1;
}
_ => {}
}
}
}

impl Halstead for PreprocCode {}
impl Halstead for CcommentCode {}
impl Halstead for CCode {}
Expand All @@ -266,4 +289,3 @@ impl Halstead for JavaCode {}
impl Halstead for GoCode {}
impl Halstead for CssCode {}
impl Halstead for HtmlCode {}
impl Halstead for RustCode {}
17 changes: 16 additions & 1 deletion src/sloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ impl SourceLoc for TsxCode {
}
}

impl SourceLoc for RustCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool) {
use Rust::*;

let start = init(node, stats, is_func_space);

match node.kind_id().into() {
LineComment | BlockComment | StringLiteral | RawStringLiteral | ExpressionStatement
| Block => {}
_ => {
stats.lines.insert(start);
}
}
}
}

impl SourceLoc for PreprocCode {}
impl SourceLoc for CcommentCode {}
impl SourceLoc for CCode {}
Expand All @@ -154,4 +170,3 @@ impl SourceLoc for JavaCode {}
impl SourceLoc for GoCode {}
impl SourceLoc for CssCode {}
impl SourceLoc for HtmlCode {}
impl SourceLoc for RustCode {}

0 comments on commit 8b65075

Please sign in to comment.