use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
pub use crate::expr::Expr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
Folder,
Namespace,
ClassFolder,
Class,
Enumeration,
Function,
Script,
LiveScript,
Property,
}
impl Kind {
pub fn as_str(self) -> &'static str {
match self {
Kind::Folder => "folder",
Kind::Namespace => "namespace",
Kind::ClassFolder | Kind::Class => "class",
Kind::Enumeration => "enumeration",
Kind::Function => "function",
Kind::Script => "script",
Kind::LiveScript => "live_script",
Kind::Property => "property",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Docstring {
pub value: String,
pub raw: String,
pub lineno: usize,
pub endlineno: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ArgumentKind {
#[serde(rename = "positional-only")]
PositionalOnly,
#[serde(rename = "optional")]
Optional,
#[serde(rename = "keyword-only")]
KeywordOnly,
#[serde(rename = "varargin")]
Varargin,
}
impl ArgumentKind {
pub fn as_str(self) -> &'static str {
match self {
ArgumentKind::PositionalOnly => "positional-only",
ArgumentKind::Optional => "optional",
ArgumentKind::KeywordOnly => "keyword-only",
ArgumentKind::Varargin => "varargin",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Argument {
pub name: String,
pub kind: ArgumentKind,
#[serde(rename = "type")]
pub type_: Option<Expr>,
pub dimensions: Option<Vec<String>>,
pub validators: Option<Expr>,
pub default: Option<Expr>,
pub docstring: Option<Docstring>,
}
impl Argument {
pub fn new(name: &str, kind: ArgumentKind) -> Self {
Argument {
name: name.to_string(),
kind,
type_: None,
dimensions: None,
validators: None,
default: None,
docstring: None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionDetail {
pub arguments: Vec<Argument>,
pub returns: Vec<Argument>,
pub is_getter: bool,
pub is_setter: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PropertyDetail {
#[serde(rename = "type")]
pub type_: Option<Expr>,
pub dimensions: Option<Vec<String>>,
pub validators: Option<Expr>,
pub default: Option<Expr>,
pub getter: Option<Box<Entity>>,
pub setter: Option<Box<Entity>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SectionKind {
Text,
Code,
}
impl SectionKind {
pub fn as_str(self) -> &'static str {
match self {
SectionKind::Text => "text",
SectionKind::Code => "code",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LiveSection {
pub kind: SectionKind,
pub content: String,
pub lineno: Option<usize>,
pub endlineno: Option<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Detail {
None,
Class {
bases: Vec<Expr>,
#[serde(default)]
events: Vec<String>,
#[serde(default)]
declares_constructor: bool,
},
Function(FunctionDetail),
Property(PropertyDetail),
Enumeration {
value: Option<Expr>,
},
LiveScript {
sections: Vec<LiveSection>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entity {
pub kind: Kind,
pub name: String,
pub path: Option<PathBuf>,
pub lineno: Option<usize>,
pub endlineno: Option<usize>,
pub docstring: Option<Docstring>,
pub attributes: BTreeSet<String>,
pub detail: Detail,
pub members: BTreeMap<String, Entity>,
}
impl Entity {
pub fn new(kind: Kind, name: &str) -> Self {
Entity {
kind,
name: name.to_string(),
path: None,
lineno: None,
endlineno: None,
docstring: None,
attributes: BTreeSet::new(),
detail: Detail::None,
members: BTreeMap::new(),
}
}
pub fn function(&self) -> Option<&FunctionDetail> {
match &self.detail {
Detail::Function(f) => Some(f),
_ => None,
}
}
pub fn property(&self) -> Option<&PropertyDetail> {
match &self.detail {
Detail::Property(p) => Some(p),
_ => None,
}
}
pub fn bases(&self) -> &[Expr] {
match &self.detail {
Detail::Class { bases, .. } => bases,
_ => &[],
}
}
pub fn events(&self) -> &[String] {
match &self.detail {
Detail::Class { events, .. } => events,
_ => &[],
}
}
pub fn declares_constructor(&self) -> bool {
matches!(
self.detail,
Detail::Class {
declares_constructor: true,
..
}
)
}
pub fn sections(&self) -> &[LiveSection] {
match &self.detail {
Detail::LiveScript { sections } => sections,
_ => &[],
}
}
pub fn enumeration_value(&self) -> Option<&Expr> {
match &self.detail {
Detail::Enumeration { value } => value.as_ref(),
_ => None,
}
}
}