use super::function::FunctionEntity;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Field {
pub name: String,
pub type_annotation: Option<String>,
pub visibility: String,
pub is_static: bool,
pub is_constant: bool,
pub default_value: Option<String>,
}
impl Field {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
type_annotation: None,
visibility: "public".to_string(),
is_static: false,
is_constant: false,
default_value: None,
}
}
pub fn with_type(mut self, type_ann: impl Into<String>) -> Self {
self.type_annotation = Some(type_ann.into());
self
}
pub fn with_visibility(mut self, vis: impl Into<String>) -> Self {
self.visibility = vis.into();
self
}
pub fn static_field(mut self) -> Self {
self.is_static = true;
self
}
pub fn constant(mut self) -> Self {
self.is_constant = true;
self
}
pub fn with_default(mut self, default: impl Into<String>) -> Self {
self.default_value = Some(default.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ClassEntity {
pub name: String,
pub visibility: String,
pub line_start: usize,
pub line_end: usize,
pub is_abstract: bool,
pub is_interface: bool,
pub base_classes: Vec<String>,
pub implemented_traits: Vec<String>,
pub methods: Vec<FunctionEntity>,
pub fields: Vec<Field>,
pub doc_comment: Option<String>,
pub attributes: Vec<String>,
pub type_parameters: Vec<String>,
}
impl ClassEntity {
pub fn new(name: impl Into<String>, line_start: usize, line_end: usize) -> Self {
Self {
name: name.into(),
visibility: "public".to_string(),
line_start,
line_end,
is_abstract: false,
is_interface: false,
base_classes: Vec::new(),
implemented_traits: Vec::new(),
methods: Vec::new(),
fields: Vec::new(),
doc_comment: None,
attributes: Vec::new(),
type_parameters: Vec::new(),
}
}
pub fn with_visibility(mut self, vis: impl Into<String>) -> Self {
self.visibility = vis.into();
self
}
pub fn abstract_class(mut self) -> Self {
self.is_abstract = true;
self
}
pub fn interface(mut self) -> Self {
self.is_interface = true;
self
}
pub fn with_bases(mut self, bases: Vec<String>) -> Self {
self.base_classes = bases;
self
}
pub fn with_traits(mut self, traits: Vec<String>) -> Self {
self.implemented_traits = traits;
self
}
pub fn with_methods(mut self, methods: Vec<FunctionEntity>) -> Self {
self.methods = methods;
self
}
pub fn with_fields(mut self, fields: Vec<Field>) -> Self {
self.fields = fields;
self
}
pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
self.doc_comment = Some(doc.into());
self
}
pub fn with_attributes(mut self, attrs: Vec<String>) -> Self {
self.attributes = attrs;
self
}
pub fn with_type_parameters(mut self, type_params: Vec<String>) -> Self {
self.type_parameters = type_params;
self
}
}