use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SemanticUnitKind {
Function,
Struct,
Enum,
Trait,
Impl,
Const,
Static,
TypeAlias,
Macro,
Module,
}
impl SemanticUnitKind {
pub fn as_str(&self) -> &'static str {
match self {
Self::Function => "function",
Self::Struct => "struct",
Self::Enum => "enum",
Self::Trait => "trait",
Self::Impl => "impl",
Self::Const => "const",
Self::Static => "static",
Self::TypeAlias => "type_alias",
Self::Macro => "macro",
Self::Module => "module",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Visibility {
Public,
Crate,
Restricted,
Private,
}
impl Visibility {
pub fn as_str(&self) -> &'static str {
match self {
Self::Public => "public",
Self::Crate => "crate",
Self::Restricted => "restricted",
Self::Private => "private",
}
}
pub fn is_public(&self) -> bool {
matches!(self, Self::Public)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct LineSpan {
pub start: usize,
pub end: usize,
}
impl LineSpan {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub fn contains(&self, line: usize) -> bool {
line >= self.start && line <= self.end
}
pub fn len(&self) -> usize {
if self.end >= self.start {
self.end - self.start + 1
} else {
0
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SemanticUnit {
pub kind: SemanticUnitKind,
pub name: String,
pub impl_name: Option<String>,
pub visibility: Visibility,
pub span: LineSpan,
pub attributes: Vec<String>,
}
impl SemanticUnit {
pub fn new(
kind: SemanticUnitKind,
name: String,
visibility: Visibility,
span: LineSpan,
attributes: Vec<String>,
) -> Self {
Self {
kind,
name,
impl_name: None,
visibility,
span,
attributes,
}
}
pub fn with_impl(
kind: SemanticUnitKind,
name: String,
impl_name: String,
visibility: Visibility,
span: LineSpan,
attributes: Vec<String>,
) -> Self {
Self {
kind,
name,
impl_name: Some(impl_name),
visibility,
span,
attributes,
}
}
pub fn qualified_name(&self) -> String {
match &self.impl_name {
Some(impl_name) => format!("{}::{}", impl_name, self.name),
None => self.name.clone(),
}
}
pub fn has_attribute(&self, attr: &str) -> bool {
self.attributes.iter().any(|a| a == attr)
}
}