Skip to content

Commit

Permalink
refactor: split models
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed May 21, 2021
1 parent 8d3c769 commit 57cad49
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 165 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tree-sitter-rust = "0.19.0"

# serialize
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
serde_json = "1"

regex = "1"

Expand Down
13 changes: 13 additions & 0 deletions src/domain/code_annotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodeAnnotation {
pub name: String,
pub key_values: Vec<AnnotationKeyValue>
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AnnotationKeyValue {
pub key: String,
pub values: Vec<AnnotationKeyValue>
}
37 changes: 37 additions & 0 deletions src/domain/code_class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use serde::{Deserialize, Serialize};

use crate::domain::code_function::CodeFunction;
use crate::domain::CodePoint;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodeClass {
pub name: String,
pub package: String,
pub extends: Vec<String>,
pub implements: Vec<String>,
pub constant: Vec<ClassConstant>,
pub functions: Vec<CodeFunction>,
pub start: CodePoint,
pub end: CodePoint
}

impl Default for CodeClass {
fn default() -> Self {
CodeClass {
name: "".to_string(),
package: "".to_string(),
extends: vec![],
implements: vec![],
constant: vec![],
functions: vec![],
start: Default::default(),
end: Default::default()
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClassConstant {
pub name: String,
pub typ: String,
}
27 changes: 27 additions & 0 deletions src/domain/code_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{Deserialize, Serialize};

use crate::domain::code_function::CodeFunction;
use crate::domain::code_class::CodeClass;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodeFile {
pub name: String,
pub path: String,
pub package: String,
pub imports: Vec<String>,
pub classes: Vec<CodeClass>,
pub functions: Vec<CodeFunction>,
}

impl Default for CodeFile {
fn default() -> Self {
CodeFile {
name: "".to_string(),
path: "".to_string(),
package: "".to_string(),
imports: vec![],
classes: vec![],
functions: vec![],
}
}
}
36 changes: 36 additions & 0 deletions src/domain/code_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::{Deserialize, Serialize};

use crate::domain::CodePoint;
use crate::domain::Location;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodeFunction {
pub name: String,
// todo: thinking in access
pub vars: Vec<String>,
pub start: CodePoint,
pub end: CodePoint
}

impl Default for CodeFunction {
fn default() -> Self {
CodeFunction {
name: "".to_string(),
vars: vec![],
start: Default::default(),
end: Default::default()
}
}
}

impl Location for CodeFunction {
fn set_start(&mut self, row: usize, column: usize) {
self.start.row = row;
self.start.column = column;
}

fn set_end(&mut self, row: usize, column: usize) {
self.end.row = row;
self.end.column = column;
}
}
8 changes: 8 additions & 0 deletions src/domain/code_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodeImport {
pub name: String,
pub import: String,
pub source: String,
}
10 changes: 10 additions & 0 deletions src/domain/code_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::domain::code_package::CodePackage;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodeModule {
pub name: String,
pub path: String,
pub package: Vec<CodePackage>,
}
10 changes: 10 additions & 0 deletions src/domain/code_package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::domain::code_class::CodeClass;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodePackage {
pub name: String,
pub path: String,
pub class: Vec<CodeClass>,
}
43 changes: 43 additions & 0 deletions src/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};

use code_class::CodeClass;

pub mod code_import;
pub mod code_file;
pub mod code_package;
pub mod code_module;
pub mod code_class;
pub mod code_function;
pub mod code_annotation;

impl Location for CodeClass {
fn set_start(&mut self, row: usize, column: usize) {
self.start.row = row;
self.start.column = column;
}

fn set_end(&mut self, row: usize, column: usize) {
self.end.row = row;
self.end.column = column;
}
}

pub trait Location {
fn set_start(&mut self, row: usize, column: usize);
fn set_end(&mut self, row: usize, column: usize);
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodePoint {
pub row: usize,
pub column: usize
}

impl Default for CodePoint {
fn default() -> Self {
CodePoint {
row: 0,
column: 0
}
}
}
5 changes: 4 additions & 1 deletion src/identify/code_ident.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::identify::code_model::{CodeFile, Location, CodeFunction};
use tree_sitter::{Node, QueryCapture};

use crate::domain::code_function::CodeFunction;
use crate::domain::code_file::CodeFile;
use crate::domain::Location;
use crate::identify::rust_ident::RustIdent;

pub trait CodeIdent {
Expand Down
153 changes: 0 additions & 153 deletions src/identify/code_model.rs

This file was deleted.

5 changes: 3 additions & 2 deletions src/identify/java_ident.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use tree_sitter::{Node, Parser, Query, QueryCursor};

use crate::identify::code_model::{CodeClass, CodeFile};
use crate::domain::code_file::CodeFile;
use crate::domain::code_class::CodeClass;
use crate::identify::code_ident::CodeIdent;

const JAVA_QUERY: &'static str = "
Expand Down Expand Up @@ -123,8 +124,8 @@ impl CodeIdent for JavaIdent {

#[cfg(test)]
mod tests {
use crate::identify::java_ident::JavaIdent;
use crate::identify::code_ident::CodeIdent;
use crate::identify::java_ident::JavaIdent;

#[test]
fn should_parse_import() {
Expand Down
Loading

0 comments on commit 57cad49

Please sign in to comment.