Skip to content

Commit 454190b

Browse files
committed
feat: add more entities
Signed-off-by: Rithul Kamesh <hi@rithul.dev>
1 parent 61e896f commit 454190b

File tree

14 files changed

+357
-14
lines changed

14 files changed

+357
-14
lines changed

src/entities/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
#[derive(Debug, Clone, Serialize, Deserialize)]
3+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
44
pub struct RustAttribute {
55
pub name: String,
66
pub arguments: Vec<String>,

src/entities/callable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::{
1010
};
1111

1212
/// Represents a Rust function or method.
13-
#[derive(Debug, Clone)]
13+
#[derive(Debug, Clone, Eq, PartialEq)]
1414
pub struct RustCallable {
1515
/// The name of the function or method.
1616
pub name: String,

src/entities/callsite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33
use super::rtype::RustType;
44

55
/// Represents a location where a function is called.
6-
#[derive(Debug, Clone, Serialize, Deserialize)]
6+
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
77
pub struct CallSite {
88
/// The line number where the function is called
99
pub line_number: u32,

src/entities/lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::vec::Vec;
44
///
55
/// This struct holds information about a lifetime parameter, including its name
66
/// and any bounds associated with it.
7-
#[derive(Debug, Clone, PartialEq)]
7+
#[derive(Debug, Clone, PartialEq, Eq)]
88
pub struct RustLifetimeParam {
99
/// The name of the lifetime parameter (e.g., 'a, 'b)
1010
pub name: String,

src/entities/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ pub mod callable;
33
pub mod callsite;
44
pub mod lifetime;
55
pub mod param;
6+
pub mod renum;
7+
pub mod rimpl;
8+
pub mod rmacro;
9+
pub mod rstruct;
10+
pub mod rtrait;
611
pub mod rtype;
712
pub mod safety;
813
pub mod variables;
9-
#[derive(Debug, Clone, PartialEq)]
14+
#[derive(Debug, Clone, PartialEq, Eq)]
1015
pub enum RustVisibility {
1116
Public,
1217
Private,
@@ -27,7 +32,7 @@ impl RustVisibility {
2732
}
2833
}
2934

30-
#[derive(Debug, Clone, PartialEq)]
35+
#[derive(Debug, Clone, PartialEq, Eq)]
3136
pub enum SafetyClassification {
3237
Safe,
3338
Unsafe,
@@ -65,7 +70,7 @@ impl SafetyClassification {
6570
/// let reason = UnsafeReason::RawPointerDeref;
6671
/// assert_eq!(reason.as_str(), "raw_pointer_deref");
6772
/// ```
68-
#[derive(Debug, Clone, PartialEq)]
73+
#[derive(Debug, Clone, PartialEq, Eq)]
6974
pub enum UnsafeReason {
7075
RawPointerDeref,
7176
MutableStatic,
@@ -110,7 +115,7 @@ impl UnsafeReason {
110115
/// - `Normal`: A regular struct with named fields.
111116
/// - `Tuple`: A tuple struct with unnamed fields.
112117
/// - `Unit`: A unit struct without any fields.
113-
#[derive(Debug, Clone, PartialEq)]
118+
#[derive(Debug, Clone, PartialEq, Eq)]
114119
pub enum RustStructKind {
115120
/// Regular struct with named fields.
116121
Normal,

src/entities/param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::entities::rtype::RustType;
44
///
55
/// This struct holds information about a generic parameter, including its name,
66
/// trait bounds, default type (if any), and whether it's a const generic.
7-
#[derive(Debug, Clone, PartialEq)]
7+
#[derive(Debug, Clone, PartialEq, Eq)]
88
pub struct RustGenericParam {
99
/// The name of the generic parameter
1010
pub name: String,
@@ -32,7 +32,7 @@ impl RustGenericParam {
3232
}
3333
}
3434

35-
#[derive(Debug, Clone, PartialEq)]
35+
#[derive(Debug, Clone, PartialEq, Eq)]
3636
pub struct RustParameter {
3737
/// Name of the parameter
3838
pub name: String,

src/entities/renum.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::collections::HashMap;
2+
3+
use super::{
4+
attr::RustAttribute, callable::RustCallable, lifetime::RustLifetimeParam,
5+
param::RustGenericParam, rstruct::RustStructField, rtype::RustType, *,
6+
};
7+
8+
/// Represents a Rust enum.
9+
#[derive(Debug, Clone)]
10+
pub struct RustEnum {
11+
/// The name of the enum.
12+
pub name: String,
13+
/// The visibility of the enum.
14+
pub visibility: RustVisibility,
15+
/// Optional documentation comment.
16+
pub doc_comment: Option<String>,
17+
/// Attributes associated with the enum.
18+
pub attributes: Vec<RustAttribute>,
19+
/// Variants defined for the enum.
20+
pub variants: Vec<RustEnumVariant>,
21+
/// Generic parameters.
22+
pub generic_params: Vec<RustGenericParam>,
23+
/// Lifetime parameters.
24+
pub lifetime_params: Vec<RustLifetimeParam>,
25+
/// Where clauses.
26+
pub where_clauses: Vec<String>,
27+
/// Traits to derive.
28+
pub derives: Vec<String>,
29+
/// Associated items mapped by their name.
30+
pub associated_items: HashMap<String, RustCallable>,
31+
/// Traits implemented for the enum.
32+
pub impl_traits: Vec<String>,
33+
/// Indicates if the enum is public.
34+
pub is_public: bool,
35+
/// The starting line in the source file.
36+
pub start_line: i32,
37+
/// The ending line in the source file.
38+
pub end_line: i32,
39+
}
40+
41+
/// Represents a variant in a Rust enum.
42+
#[derive(Debug, Clone)]
43+
pub struct RustEnumVariant {
44+
/// The name of the variant.
45+
pub name: String,
46+
/// Fields for a struct variant.
47+
pub fields: Option<Vec<RustStructField>>,
48+
/// Types for a tuple variant.
49+
pub tuple_types: Option<Vec<RustType>>,
50+
/// Explicit discriminant for the variant.
51+
pub discriminant: Option<String>,
52+
/// Optional documentation comment.
53+
pub doc_comment: Option<String>,
54+
/// Attributes associated with the variant.
55+
pub attributes: Vec<RustAttribute>,
56+
}

src/entities/rimpl.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::collections::HashMap;
2+
3+
use super::{
4+
callable::RustCallable, lifetime::RustLifetimeParam, param::RustGenericParam, rtype::RustType,
5+
};
6+
7+
/// Represents a Rust impl block.
8+
#[derive(Debug, Clone)]
9+
pub struct RustImpl {
10+
/// The type name that the impl block is for.
11+
pub type_name: String,
12+
/// Optional trait name; None for inherent impls.
13+
pub trait_name: Option<String>,
14+
/// Generic parameters of the impl.
15+
pub generic_params: Vec<RustGenericParam>,
16+
/// Lifetime parameters of the impl.
17+
pub lifetime_params: Vec<RustLifetimeParam>,
18+
/// Where clauses associated with the impl.
19+
pub where_clauses: Vec<String>,
20+
/// Methods in the impl; the key is the method name.
21+
pub methods: HashMap<String, RustCallable>,
22+
/// Associated types defined in the impl.
23+
pub associated_types: HashMap<String, RustType>,
24+
/// Associated constants defined in the impl.
25+
pub associated_consts: HashMap<String, String>,
26+
/// Whether the impl is marked as unsafe.
27+
pub is_unsafe: bool,
28+
/// Whether the impl is a negative impl (e.g., `!Send`).
29+
pub is_negative: bool,
30+
/// The starting line number of the impl block in the source.
31+
pub start_line: usize,
32+
/// The ending line number of the impl block in the source.
33+
pub end_line: usize,
34+
}

src/entities/rmacro.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use super::{RustVisibility, attr::RustAttribute};
2+
3+
/// Represents a Rust macro definition.
4+
///
5+
/// This struct encapsulates details about a macro defined
6+
/// in Rust source code. It contains the macro's name, visibility,
7+
/// documentation comment, any attributes associated with it,
8+
/// its defining rules, and metadata regarding its type and location.
9+
#[derive(Debug, Clone)]
10+
pub struct RustMacro {
11+
/// The name of the macro.
12+
pub name: String,
13+
/// The visibility of the macro (e.g., public or private).
14+
pub visibility: RustVisibility,
15+
/// An optional documentation comment for the macro.
16+
pub doc_comment: Option<String>,
17+
/// Attributes applied to the macro, such as annotations.
18+
pub attributes: Vec<RustAttribute>,
19+
/// Rules or tokens that define the macro's behavior.
20+
pub rules: Vec<String>,
21+
/// Indicates if the macro is procedural.
22+
pub is_procedural: bool,
23+
/// Indicates if the macro is a derive macro.
24+
pub is_derive: bool,
25+
/// Indicates if the macro is an attribute macro.
26+
pub is_attribute: bool,
27+
/// Indicates if the macro follows the function-like syntax.
28+
pub is_function_like: bool,
29+
/// True if the macro is exported via a macro_use directive.
30+
pub exported_from_macro_use: bool,
31+
/// The starting line number of the macro definition.
32+
pub start_line: usize,
33+
/// The ending line number of the macro definition.
34+
pub end_line: usize,
35+
}
36+
37+
impl RustMacro {
38+
/// Creates a new `RustMacro` instance with default values.
39+
///
40+
/// # Arguments
41+
///
42+
/// * `name` - The name of the macro.
43+
/// * `start_line` - The starting line of the macro definition.
44+
/// * `end_line` - The ending line of the macro definition.
45+
pub fn new(name: String, start_line: usize, end_line: usize) -> Self {
46+
Self {
47+
name,
48+
visibility: RustVisibility::Private, // Defaults to PRIVATE visibility.
49+
doc_comment: None,
50+
attributes: Vec::new(),
51+
rules: Vec::new(),
52+
is_procedural: false,
53+
is_derive: false,
54+
is_attribute: false,
55+
is_function_like: true,
56+
exported_from_macro_use: false,
57+
start_line,
58+
end_line,
59+
}
60+
}
61+
}

src/entities/rstruct.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use std::collections::HashMap;
2+
3+
use super::{
4+
RustStructKind, RustVisibility, attr::RustAttribute, callable::RustCallable,
5+
lifetime::RustLifetimeParam, param::RustGenericParam, rtype::RustType,
6+
};
7+
8+
/// Represents a field in a Rust struct.
9+
#[derive(Debug, Clone, PartialEq, Eq)]
10+
pub struct RustStructField {
11+
/// Name of the field.
12+
pub name: String,
13+
/// The type of the field.
14+
pub ty: RustType,
15+
/// Visibility of the field. Defaults to `RustVisibility::PRIVATE`.
16+
pub visibility: RustVisibility,
17+
/// Optional documentation comment.
18+
pub doc_comment: Option<String>,
19+
/// List of attributes associated with the field.
20+
pub attributes: Vec<RustAttribute>,
21+
}
22+
23+
impl RustStructField {
24+
/// Creates a new `RustStructField` with the given name and type.
25+
/// Visibility is set to `PRIVATE`, no doc comment, and an empty attributes list.
26+
pub fn new(name: String, ty: RustType) -> Self {
27+
Self {
28+
name,
29+
ty,
30+
visibility: RustVisibility::Private,
31+
doc_comment: None,
32+
attributes: Vec::new(),
33+
}
34+
}
35+
}
36+
37+
/// Represents a Rust struct definition.
38+
#[derive(Debug, Clone, PartialEq, Eq)]
39+
pub struct RustStruct {
40+
/// Name of the Rust struct.
41+
pub name: String,
42+
/// Kind of the struct. Defaults to `RustStructKind::NORMAL`.
43+
pub kind: RustStructKind,
44+
/// Visibility of the struct. Defaults to `RustVisibility::PRIVATE`.
45+
pub visibility: RustVisibility,
46+
/// Optional documentation comment.
47+
pub doc_comment: Option<String>,
48+
/// List of attributes associated with the struct.
49+
pub attributes: Vec<RustAttribute>,
50+
/// List of fields contained in the struct.
51+
pub fields: Vec<RustStructField>,
52+
/// Generic parameters for the struct.
53+
pub generic_params: Vec<RustGenericParam>,
54+
/// Lifetime parameters for the struct.
55+
pub lifetime_params: Vec<RustLifetimeParam>,
56+
/// Where clauses applied to the struct.
57+
pub where_clauses: Vec<String>,
58+
/// Derives applied to the struct.
59+
pub derives: Vec<String>,
60+
/// Associated items (e.g., methods, functions) where key is the item name.
61+
pub associated_items: HashMap<String, RustCallable>,
62+
/// Traits that this struct implements.
63+
pub impl_traits: Vec<String>,
64+
/// Indicates whether the struct is public.
65+
pub is_public: bool,
66+
/// Indicates whether the struct contains unsafe code.
67+
pub contains_unsafe: bool,
68+
/// The starting line number of the struct definition in the source file.
69+
pub start_line: usize,
70+
/// The ending line number of the struct definition in the source file.
71+
pub end_line: usize,
72+
}
73+
74+
impl RustStruct {
75+
/// Creates a new `RustStruct` with the given name, start_line, and end_line.
76+
/// Default values are used for the remaining fields.
77+
pub fn new(name: String, start_line: usize, end_line: usize) -> Self {
78+
Self {
79+
name,
80+
kind: RustStructKind::Normal,
81+
visibility: RustVisibility::Private,
82+
doc_comment: None,
83+
attributes: Vec::new(),
84+
fields: Vec::new(),
85+
generic_params: Vec::new(),
86+
lifetime_params: Vec::new(),
87+
where_clauses: Vec::new(),
88+
derives: Vec::new(),
89+
associated_items: HashMap::new(),
90+
impl_traits: Vec::new(),
91+
is_public: false,
92+
contains_unsafe: false,
93+
start_line,
94+
end_line,
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)