Skip to main content

shape_ast/ast/
modules.rs

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