|
| 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