Skip to content

Commit

Permalink
getter: simplify code by removing some redundant lines (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 authored Apr 5, 2023
1 parent 24602e3 commit e3d5740
Showing 1 changed file with 20 additions and 43 deletions.
63 changes: 20 additions & 43 deletions src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ macro_rules! get_operator {
($language:ident) => {
#[inline(always)]
fn get_operator_id_as_str(id: u16) -> &'static str {
let typ: $language = id.into();
let typ = id.into();
match typ {
$language::LPAREN => "()",
$language::LBRACK => "[]",
$language::LBRACE => "{}",
_ => {
let tok: &'static str = typ.into();
tok
}
_ => typ.into(),
}
}
};
Expand Down Expand Up @@ -53,8 +50,7 @@ pub trait Getter {

impl Getter for PythonCode {
fn get_space_kind(node: &Node) -> SpaceKind {
let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
Python::FunctionDefinition => SpaceKind::Function,
Python::ClassDefinition => SpaceKind::Class,
Python::Module => SpaceKind::Unit,
Expand All @@ -65,8 +61,7 @@ impl Getter for PythonCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Python::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_id().into() {
Import | DOT | From | COMMA | As | STAR | GTGT | Assert | COLONEQ | Return | Def
| Del | Raise | Pass | Break | Continue | If | Elif | Else | Async | For | In
| While | Try | Except | Finally | With | DASHGT | EQ | Global | Exec | AT | Not
Expand All @@ -91,19 +86,16 @@ impl Getter for PythonCode {
}
}

#[inline(always)]
fn get_operator_id_as_str(id: u16) -> &'static str {
let typ: Python = id.into();
typ.into()
Into::<Python>::into(id).into()
}
}

impl Getter for MozjsCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Mozjs::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
Function
| MethodDefinition
| GeneratorFunction
Expand Down Expand Up @@ -147,8 +139,7 @@ impl Getter for MozjsCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Mozjs::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_id().into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
Expand All @@ -172,8 +163,7 @@ impl Getter for JavascriptCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Javascript::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
Function
| MethodDefinition
| GeneratorFunction
Expand Down Expand Up @@ -217,8 +207,7 @@ impl Getter for JavascriptCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Javascript::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_id().into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
Expand All @@ -242,8 +231,7 @@ impl Getter for TypescriptCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Typescript::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
Function
| MethodDefinition
| GeneratorFunction
Expand Down Expand Up @@ -288,8 +276,7 @@ impl Getter for TypescriptCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Typescript::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_id().into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
Expand All @@ -313,8 +300,7 @@ impl Getter for TsxCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Tsx::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
Function
| MethodDefinition
| GeneratorFunction
Expand Down Expand Up @@ -359,8 +345,7 @@ impl Getter for TsxCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Tsx::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_id().into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
Expand Down Expand Up @@ -399,8 +384,7 @@ impl Getter for RustCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Rust::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
FunctionItem | ClosureExpression => SpaceKind::Function,
TraitItem => SpaceKind::Trait,
ImplItem => SpaceKind::Impl,
Expand All @@ -412,8 +396,7 @@ impl Getter for RustCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Rust::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_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
Expand All @@ -431,8 +414,7 @@ impl Getter for RustCode {

impl Getter for CppCode {
fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
Cpp::FunctionDefinition | Cpp::FunctionDefinition2 | Cpp::FunctionDefinition3 => {
if let Some(op_cast) = node.first_child(|id| Cpp::OperatorCast == id) {
let code = &code[op_cast.object().start_byte()..op_cast.object().end_byte()];
Expand Down Expand Up @@ -477,8 +459,7 @@ impl Getter for CppCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Cpp::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
FunctionDefinition | FunctionDefinition2 | FunctionDefinition3 => SpaceKind::Function,
StructSpecifier => SpaceKind::Struct,
ClassSpecifier => SpaceKind::Class,
Expand All @@ -491,8 +472,7 @@ impl Getter for CppCode {
fn get_op_type(node: &Node) -> HalsteadType {
use Cpp::*;

let id = node.object().kind_id();
match id.into() {
match node.object().kind_id().into() {
DOT | LPAREN | LPAREN2 | COMMA | STAR | GTGT | COLON | SEMI | Return | Break
| Continue | If | Else | Switch | Case | Default | For | While | Goto | Do | Delete
| New | Try | Catch | Throw | EQ | AMPAMP | PIPEPIPE | DASH | DASHDASH | DASHGT
Expand All @@ -515,8 +495,7 @@ impl Getter for JavaCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Java::*;

let typ = node.object().kind_id();
match typ.into() {
match node.object().kind_id().into() {
ClassDeclaration => SpaceKind::Class,
MethodDeclaration | ConstructorDeclaration | LambdaExpression => SpaceKind::Function,
InterfaceDeclaration => SpaceKind::Interface,
Expand All @@ -530,9 +509,7 @@ impl Getter for JavaCode {
// Some guides that informed grammar choice for Halstead
// keywords, operators, literals: https://docs.oracle.com/javase/specs/jls/se18/html/jls-3.html#jls-3.12
// https://www.geeksforgeeks.org/software-engineering-halsteads-software-metrics/?msclkid=5e181114abef11ecbb03527e95a34828
let typ = node.object().kind_id();

match typ.into() {
match node.object().kind_id().into() {
// Operator: function calls
MethodInvocation
// Operator: control flow
Expand Down

0 comments on commit e3d5740

Please sign in to comment.