Skip to main content

shape_ast/ast/
modules.rs

1//! Module system types for Shape AST
2
3use serde::{Deserialize, Serialize};
4
5use super::DocComment;
6use super::functions::{Annotation, ForeignFunctionDef, FunctionDef};
7use super::program::Item;
8use super::span::Span;
9use super::types::{EnumDef, InterfaceDef, StructTypeDef, TraitDef};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ImportStmt {
13    pub items: ImportItems,
14    pub from: String,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub enum ImportItems {
19    /// from module::path use { a, b as c }
20    Named(Vec<ImportSpec>),
21    /// use module::path / use module::path as alias (binds local tail segment or alias)
22    Namespace { name: String, alias: Option<String> },
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct ImportSpec {
27    pub name: String,
28    pub alias: Option<String>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct ExportStmt {
33    pub item: ExportItem,
34    /// For `pub let/const/var`, the original variable declaration so the compiler
35    /// can compile the initialization (ExportItem::Named only preserves the name).
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub source_decl: Option<super::program::VariableDecl>,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub enum ExportItem {
42    /// pub fn name(...) { ... }
43    Function(FunctionDef),
44    /// pub type Name = Type;
45    TypeAlias(super::types::TypeAliasDef),
46    /// pub { name1, name2 as alias }
47    Named(Vec<ExportSpec>),
48    /// pub enum Name { ... }
49    Enum(EnumDef),
50    /// pub type Name { field: Type, ... }
51    Struct(StructTypeDef),
52    /// pub interface Name { ... }
53    Interface(InterfaceDef),
54    /// pub trait Name { ... }
55    Trait(TraitDef),
56    /// pub fn python name(...) { ... }
57    ForeignFunction(ForeignFunctionDef),
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct ExportSpec {
62    pub name: String,
63    pub alias: Option<String>,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct ModuleDecl {
68    pub name: String,
69    pub name_span: Span,
70    #[serde(default)]
71    pub doc_comment: Option<DocComment>,
72    pub annotations: Vec<Annotation>,
73    pub items: Vec<Item>,
74}