Skip to content

Commit cc2e3f1

Browse files
committed
feat: implement all entities
Signed-off-by: Rithul Kamesh <hi@rithul.dev>
1 parent 454190b commit cc2e3f1

File tree

10 files changed

+230
-7
lines changed

10 files changed

+230
-7
lines changed

src/entities/dep.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Module for representing a Rust dependency.
2+
//!
3+
//! This module defines the RustDependency struct, which represents a dependency
4+
//! in a Rust project. Dependencies can be internal or external, and an associated
5+
//! RustCrate (defined in the parent module) can be tied to the dependency.
6+
7+
use super::rcrate::RustCrate;
8+
9+
/// Represents a Rust dependency, which could be internal or external.
10+
#[derive(Debug, Clone, Eq, PartialEq)]
11+
pub struct RustDependency {
12+
/// The name of the dependency.
13+
pub name: String,
14+
/// Indicates if the dependency is external. Defaults to true.
15+
pub is_external: bool,
16+
/// Associated RustCrate, defined in the parent module.
17+
pub crate_ref: Option<RustCrate>,
18+
}
19+
20+
impl RustDependency {
21+
/// Creates a new RustDependency with the given name.
22+
///
23+
/// The `is_external` field is set to `true` by default.
24+
///
25+
/// # Arguments
26+
///
27+
/// * `name` - A string representing the dependency's name.
28+
/// * `crate_ref` - An optional reference to a RustCrate from the parent module.
29+
pub fn new(name: String, crate_ref: Option<RustCrate>) -> Self {
30+
RustDependency {
31+
name,
32+
is_external: true,
33+
crate_ref,
34+
}
35+
}
36+
}

src/entities/lifetime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::vec::Vec;
22

3+
use serde::{Deserialize, Serialize};
4+
35
/// Represents a lifetime parameter in Rust.
46
///
57
/// This struct holds information about a lifetime parameter, including its name
68
/// and any bounds associated with it.
7-
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
810
pub struct RustLifetimeParam {
911
/// The name of the lifetime parameter (e.g., 'a, 'b)
1012
pub name: String,

src/entities/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
use serde::{Deserialize, Serialize};
2+
13
pub mod attr;
24
pub mod callable;
35
pub mod callsite;
6+
pub mod dep;
47
pub mod lifetime;
8+
pub mod module;
59
pub mod param;
10+
pub mod rcrate;
611
pub mod renum;
712
pub mod rimpl;
813
pub mod rmacro;
@@ -11,7 +16,7 @@ pub mod rtrait;
1116
pub mod rtype;
1217
pub mod safety;
1318
pub mod variables;
14-
#[derive(Debug, Clone, PartialEq, Eq)]
19+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1520
pub enum RustVisibility {
1621
Public,
1722
Private,

src/entities/module.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use std::collections::HashMap;
2+
3+
use super::{
4+
RustVisibility,
5+
attr::RustAttribute,
6+
callable::RustCallable,
7+
renum::RustEnum,
8+
rimpl::RustImpl,
9+
rmacro::RustMacro,
10+
rstruct::RustStruct,
11+
rtrait::RustTrait,
12+
rtype::{RustType, RustTypeAlias},
13+
variables::RustVariableDeclaration,
14+
};
15+
16+
/// Represents a Rust module with all possible items.
17+
#[derive(Debug, Clone, PartialEq, Eq)]
18+
pub struct RustModule {
19+
/// The name of the module.
20+
pub name: String,
21+
/// Documentation comment for the module.
22+
pub doc_comment: Option<String>,
23+
/// Attributes applied to the module.
24+
pub attributes: Vec<RustAttribute>,
25+
/// Visibility of the module.
26+
pub visibility: RustVisibility,
27+
28+
// Type definitions
29+
/// Map of type definitions.
30+
pub types: HashMap<String, RustType>,
31+
/// Map of structs.
32+
pub structs: HashMap<String, RustStruct>,
33+
/// Map of enums.
34+
pub enums: HashMap<String, RustEnum>,
35+
/// Map of traits.
36+
pub traits: HashMap<String, RustTrait>,
37+
/// List of implementations.
38+
pub impls: Vec<RustImpl>,
39+
/// Map of type aliases.
40+
pub type_aliases: HashMap<String, RustTypeAlias>,
41+
42+
// Functions and macros
43+
/// Map of functions.
44+
pub functions: HashMap<String, RustCallable>,
45+
/// Map of safe functions.
46+
pub safe_functions: HashMap<String, RustCallable>,
47+
/// Map of unsafe functions.
48+
pub unsafe_functions: HashMap<String, RustCallable>,
49+
/// Map of macros.
50+
pub macros: HashMap<String, RustMacro>,
51+
52+
// Module structure
53+
/// Map of submodules.
54+
pub submodules: HashMap<String, RustModule>,
55+
/// Constants declared in the module.
56+
pub constants: Vec<RustVariableDeclaration>,
57+
/// List of use declarations.
58+
pub use_declarations: Vec<String>,
59+
/// List of extern crate declarations.
60+
pub extern_crates: Vec<String>,
61+
62+
// Module properties
63+
/// Indicates if the module is marked as unsafe.
64+
pub is_unsafe: bool,
65+
/// The file path of the module.
66+
pub file_path: Option<String>,
67+
/// True if the file is named mod.rs.
68+
pub is_mod_rs: bool,
69+
/// True if this is the root module.
70+
pub is_root_module: bool,
71+
}
72+
73+
impl RustModule {
74+
/// Creates a new `RustModule` with the given name.
75+
pub fn new(name: String) -> Self {
76+
Self {
77+
name,
78+
doc_comment: None,
79+
attributes: Vec::new(),
80+
visibility: RustVisibility::Private,
81+
types: HashMap::new(),
82+
structs: HashMap::new(),
83+
enums: HashMap::new(),
84+
traits: HashMap::new(),
85+
impls: Vec::new(),
86+
type_aliases: HashMap::new(),
87+
functions: HashMap::new(),
88+
safe_functions: HashMap::new(),
89+
unsafe_functions: HashMap::new(),
90+
macros: HashMap::new(),
91+
submodules: HashMap::new(),
92+
constants: Vec::new(),
93+
use_declarations: Vec::new(),
94+
extern_crates: Vec::new(),
95+
is_unsafe: false,
96+
file_path: None,
97+
is_mod_rs: false,
98+
is_root_module: false,
99+
}
100+
}
101+
}

src/entities/param.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use serde::{Deserialize, Serialize};
2+
13
use crate::entities::rtype::RustType;
24

35
/// Represents a generic type parameter in Rust.
46
///
57
/// This struct holds information about a generic parameter, including its name,
68
/// trait bounds, default type (if any), and whether it's a const generic.
7-
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
810
pub struct RustGenericParam {
911
/// The name of the generic parameter
1012
pub name: String,

src/entities/rcrate.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use super::{dep::RustDependency, module::RustModule};
2+
3+
/// Represents a complete Rust crate.
4+
#[derive(Debug, Clone, PartialEq, Eq)]
5+
pub struct RustCrate {
6+
/// The name of the crate.
7+
pub name: String,
8+
/// The version of the crate.
9+
pub version: String,
10+
/// Indicates if the crate is a library.
11+
pub is_lib: bool,
12+
/// List of modules in the crate.
13+
pub modules: Vec<RustModule>,
14+
/// The edition of the crate (default is "2021").
15+
pub edition: String,
16+
/// Features enabled in the crate.
17+
pub features: Vec<String>,
18+
/// Dependencies of the crate.
19+
pub dependencies: Vec<RustDependency>,
20+
}
21+
22+
impl RustCrate {
23+
/// Creates a new `RustCrate` with the given name, version, and modules.
24+
///
25+
/// # Arguments
26+
///
27+
/// * `name` - A string representing the crate's name.
28+
/// * `version` - A string representing the crate's version.
29+
/// * `modules` - A vector containing the crate's modules.
30+
///
31+
/// # Returns
32+
///
33+
/// A new instance of `RustCrate` with default values:
34+
/// - `is_lib` set to `false`
35+
/// - `edition` set to `"2021"`
36+
/// - Empty vectors for `features` and `dependencies`
37+
pub fn new(name: String, version: String, modules: Vec<RustModule>) -> Self {
38+
RustCrate {
39+
name,
40+
version,
41+
is_lib: false,
42+
modules,
43+
edition: "2021".to_owned(),
44+
features: Vec::new(),
45+
dependencies: Vec::new(),
46+
}
47+
}
48+
}

src/entities/renum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
};
77

88
/// Represents a Rust enum.
9-
#[derive(Debug, Clone)]
9+
#[derive(Debug, Clone, PartialEq, Eq)]
1010
pub struct RustEnum {
1111
/// The name of the enum.
1212
pub name: String,
@@ -39,7 +39,7 @@ pub struct RustEnum {
3939
}
4040

4141
/// Represents a variant in a Rust enum.
42-
#[derive(Debug, Clone)]
42+
#[derive(Debug, Clone, PartialEq, Eq)]
4343
pub struct RustEnumVariant {
4444
/// The name of the variant.
4545
pub name: String,

src/entities/rimpl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
};
66

77
/// Represents a Rust impl block.
8-
#[derive(Debug, Clone)]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
99
pub struct RustImpl {
1010
/// The type name that the impl block is for.
1111
pub type_name: String,

src/entities/rmacro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{RustVisibility, attr::RustAttribute};
66
/// in Rust source code. It contains the macro's name, visibility,
77
/// documentation comment, any attributes associated with it,
88
/// its defining rules, and metadata regarding its type and location.
9-
#[derive(Debug, Clone)]
9+
#[derive(Debug, Clone, PartialEq, Eq)]
1010
pub struct RustMacro {
1111
/// The name of the macro.
1212
pub name: String,

src/entities/rtype.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use std::fmt;
22

33
use serde::{Deserialize, Serialize};
44

5+
use super::{
6+
RustVisibility, attr::RustAttribute, lifetime::RustLifetimeParam, param::RustGenericParam,
7+
};
8+
59
/// Represents a Rust type.
610
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq)]
711
pub struct RustType {
@@ -55,3 +59,28 @@ impl fmt::Display for RustType {
5559
write!(f, "{}", self.name)
5660
}
5761
}
62+
63+
/// Represents a Rust type alias.
64+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq)]
65+
pub struct RustTypeAlias {
66+
/// Name of the type alias.
67+
pub name: String,
68+
/// Visibility of the type alias.
69+
pub visibility: RustVisibility,
70+
/// Optional documentation comment.
71+
pub doc_comment: Option<String>,
72+
/// Attributes associated with the type alias.
73+
pub attributes: Vec<RustAttribute>,
74+
/// Generic parameters for the type alias.
75+
pub generic_params: Vec<RustGenericParam>,
76+
/// Lifetime parameters for the type alias.
77+
pub lifetime_params: Vec<RustLifetimeParam>,
78+
/// Where clauses for the type alias.
79+
pub where_clauses: Vec<String>,
80+
/// The target type that the alias refers to.
81+
pub target_type: RustType,
82+
/// Starting line number in the source file.
83+
pub start_line: usize,
84+
/// Ending line number in the source file.
85+
pub end_line: usize,
86+
}

0 commit comments

Comments
 (0)