galvan_ast/item/
fn.rs

1use galvan_ast_macro::AstNode;
2
3use crate::{AstNode, PrintAst, Span};
4
5use super::*;
6
7#[derive(Clone, Debug, PartialEq, Eq, AstNode)]
8pub struct FnDecl {
9    // pub annotations,
10    pub signature: FnSignature,
11    pub body: Body,
12    pub span: Span,
13}
14
15impl From<FnSignature> for FnDecl {
16    fn from(value: FnSignature) -> Self {
17        Self {
18            signature: value,
19            body: Body {
20                statements: vec![],
21                span: Span::default(),
22            },
23            span: Span::default(),
24        }
25    }
26}
27
28#[derive(Clone, Debug, PartialEq, Eq, AstNode)]
29pub struct FnSignature {
30    // pub asyncness: Async,
31    // pub constness: Const,
32    pub visibility: Visibility,
33    pub identifier: Ident,
34    pub parameters: ParamList,
35    pub return_type: TypeElement,
36    pub span: Span,
37}
38
39impl FnSignature {
40    pub fn receiver(&self) -> Option<&Param> {
41        self.parameters
42            .params
43            .first()
44            .filter(|param| param.identifier.as_str() == "self")
45    }
46}
47
48#[derive(Clone, Debug, PartialEq, Eq, AstNode)]
49pub struct ParamList {
50    pub params: Vec<Param>,
51    pub span: Span,
52}
53
54#[derive(Clone, Debug, PartialEq, Eq, AstNode)]
55pub struct Param {
56    pub decl_modifier: Option<DeclModifier>,
57    pub identifier: Ident,
58    pub param_type: TypeElement,
59    pub span: Span,
60}
61
62#[derive(Copy, Clone, Debug, PartialEq, Eq)]
63pub enum DeclModifier {
64    Let,
65    Mut,
66    Ref,
67}
68
69impl PrintAst for DeclModifier {
70    fn print_ast(&self, indent: usize) -> String {
71        let indent_str = " ".repeat(indent);
72        match self {
73            DeclModifier::Let => format!("{indent_str}let"),
74            DeclModifier::Mut => format!("{indent_str}mut"),
75            DeclModifier::Ref => format!("{indent_str}ref"),
76        }
77    }
78}