codegraph_parser_api/entities/
trait_.rs

1use super::function::FunctionEntity;
2use serde::{Deserialize, Serialize};
3
4/// Represents a trait/protocol/interface definition
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct TraitEntity {
7    /// Trait name
8    pub name: String,
9
10    /// Visibility
11    pub visibility: String,
12
13    /// Starting line number
14    pub line_start: usize,
15
16    /// Ending line number
17    pub line_end: usize,
18
19    /// Required methods
20    pub required_methods: Vec<FunctionEntity>,
21
22    /// Parent traits (trait inheritance)
23    pub parent_traits: Vec<String>,
24
25    /// Documentation
26    pub doc_comment: Option<String>,
27
28    /// Attributes/decorators
29    pub attributes: Vec<String>,
30}
31
32impl TraitEntity {
33    pub fn new(name: impl Into<String>, line_start: usize, line_end: usize) -> Self {
34        Self {
35            name: name.into(),
36            visibility: "public".to_string(),
37            line_start,
38            line_end,
39            required_methods: Vec::new(),
40            parent_traits: Vec::new(),
41            doc_comment: None,
42            attributes: Vec::new(),
43        }
44    }
45
46    pub fn with_visibility(mut self, vis: impl Into<String>) -> Self {
47        self.visibility = vis.into();
48        self
49    }
50
51    pub fn with_methods(mut self, methods: Vec<FunctionEntity>) -> Self {
52        self.required_methods = methods;
53        self
54    }
55
56    pub fn with_parent_traits(mut self, parents: Vec<String>) -> Self {
57        self.parent_traits = parents;
58        self
59    }
60
61    pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
62        self.doc_comment = Some(doc.into());
63        self
64    }
65
66    pub fn with_attributes(mut self, attrs: Vec<String>) -> Self {
67        self.attributes = attrs;
68        self
69    }
70}