use crate::ast::{
AttributeAnnotation, BindingPattern, Expr, ExternAbi, GenericParam, Ident, ParamConvention,
Type, Visibility,
};
use crate::location::Span;
use serde::{Deserialize, Serialize};
pub const FORMAT_VERSION: u32 = 1;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct File {
pub format_version: u32,
pub statements: Vec<Statement>,
pub span: Span,
}
impl File {
#[must_use]
#[expect(
clippy::missing_const_for_fn,
reason = "Vec<Statement> is not const-compatible"
)]
pub fn new(statements: Vec<Statement>, span: Span) -> Self {
Self {
format_version: FORMAT_VERSION,
statements,
span,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Statement {
Use(UseStmt),
Let(Box<LetBinding>),
Definition(Box<Definition>),
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Definition {
Trait(TraitDef),
Struct(StructDef),
Impl(ImplDef),
Enum(EnumDef),
Module(ModuleDef),
Function(Box<FunctionDef>),
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FunctionDef {
pub visibility: Visibility,
pub name: Ident,
pub generics: Vec<GenericParam>,
pub params: Vec<FnParam>,
pub return_type: Option<Type>,
pub body: Option<Expr>,
pub extern_abi: Option<ExternAbi>,
pub attributes: Vec<AttributeAnnotation>,
pub doc: Option<String>,
pub span: Span,
}
impl FunctionDef {
#[must_use]
pub const fn is_extern(&self) -> bool {
self.extern_abi.is_some()
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UseStmt {
pub visibility: Visibility,
pub path: Vec<Ident>,
pub items: UseItems,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum UseItems {
Single(Ident),
Multiple(Vec<Ident>),
Glob,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LetBinding {
pub visibility: Visibility,
pub mutable: bool,
pub pattern: BindingPattern,
pub type_annotation: Option<Type>,
pub value: Expr,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TraitDef {
pub visibility: Visibility,
pub name: Ident,
pub generics: Vec<GenericParam>,
pub traits: Vec<Ident>,
pub fields: Vec<FieldDef>,
pub methods: Vec<FnSig>,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructDef {
pub visibility: Visibility,
pub name: Ident,
pub generics: Vec<GenericParam>,
pub fields: Vec<StructField>,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructField {
pub mutable: bool,
pub name: Ident,
pub ty: Type,
pub optional: bool,
pub default: Option<Expr>,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImplDef {
pub trait_name: Option<Ident>,
pub trait_args: Vec<Type>,
pub name: Ident,
pub generics: Vec<GenericParam>,
pub functions: Vec<FnDef>,
pub is_extern: bool,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FnDef {
pub name: Ident,
pub params: Vec<FnParam>,
pub return_type: Option<Type>,
pub body: Option<Expr>,
pub attributes: Vec<AttributeAnnotation>,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FnSig {
pub name: Ident,
pub params: Vec<FnParam>,
pub return_type: Option<Type>,
pub attributes: Vec<AttributeAnnotation>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FnParam {
pub convention: ParamConvention,
pub external_label: Option<Ident>,
pub name: Ident,
pub ty: Option<Type>,
pub default: Option<Expr>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnumDef {
pub visibility: Visibility,
pub name: Ident,
pub generics: Vec<GenericParam>,
pub variants: Vec<EnumVariant>,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnumVariant {
pub name: Ident,
pub fields: Vec<FieldDef>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModuleDef {
pub visibility: Visibility,
pub name: Ident,
pub definitions: Vec<Definition>,
pub doc: Option<String>,
pub span: Span,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FieldDef {
pub mutable: bool,
pub name: Ident,
pub ty: Type,
pub doc: Option<String>,
pub span: Span,
}