code_gen/rust/function/
function.rs

1use crate::rust::CommentType::OuterLineDoc;
2use crate::rust::{
3    Access, Signature, WithAccess, WithAttributes, WithComments, WithSignature, WithUnsafeFlag,
4};
5use crate::{CodeBuffer, Statement, WithStatements};
6
7/// A function declaration.
8pub struct Function {
9    comments: Vec<String>,
10    attributes: Vec<String>,
11    is_async: bool,
12    is_const: bool,
13    access: Access,
14    signature: Signature,
15    statements: Vec<Box<dyn Statement>>,
16}
17
18impl<S: Into<Signature>> From<S> for Function {
19    fn from(signature: S) -> Self {
20        Self {
21            comments: Vec::default(),
22            attributes: Vec::default(),
23            is_async: false,
24            is_const: false,
25            access: Access::default(),
26            signature: signature.into(),
27            statements: Vec::default(),
28        }
29    }
30}
31
32impl WithComments for Function {
33    fn comments(&self) -> &[String] {
34        self.comments.as_slice()
35    }
36
37    fn add_comment<S>(&mut self, comment: S)
38    where
39        S: Into<String>,
40    {
41        self.comments.push(comment.into());
42    }
43}
44
45impl WithAttributes for Function {
46    fn attributes(&self) -> &[String] {
47        self.attributes.as_slice()
48    }
49
50    fn add_attribute<S>(&mut self, attribute: S)
51    where
52        S: Into<String>,
53    {
54        self.attributes.push(attribute.into());
55    }
56}
57
58impl WithAccess for Function {
59    fn access(&self) -> &Access {
60        &self.access
61    }
62
63    fn set_access<A>(&mut self, access: A)
64    where
65        A: Into<Access>,
66    {
67        self.access = access.into();
68    }
69}
70
71impl Function {
72    //! Async
73
74    /// Sets the `is_async` flag.
75    pub fn set_async(&mut self, is_async: bool) {
76        self.is_async = is_async;
77    }
78
79    /// Sets the `is_async` flag.
80    pub fn with_async(mut self, is_async: bool) -> Self {
81        self.set_async(is_async);
82        self
83    }
84}
85
86impl Function {
87    //! Const
88
89    /// Sets the `is_const` flag.
90    pub fn set_const(&mut self, is_const: bool) {
91        self.is_const = is_const;
92    }
93
94    /// Sets the `is_const` flag.
95    pub fn with_const(mut self, is_const: bool) -> Self {
96        self.set_const(is_const);
97        self
98    }
99}
100
101impl WithSignature for Function {
102    fn signature(&self) -> &Signature {
103        &self.signature
104    }
105}
106
107impl WithStatements for Function {
108    fn statements(&self) -> &[Box<dyn Statement>] {
109        self.statements.as_slice()
110    }
111
112    fn add_boxed_statement(&mut self, statement: Box<dyn Statement>) {
113        self.statements.push(statement);
114    }
115}
116
117impl Statement for Function {
118    fn write(&self, b: &mut CodeBuffer, level: usize) {
119        self.write_comments(OuterLineDoc, b, level);
120        self.write_attributes(b, level);
121        b.indent(level);
122        self.write_access(b);
123        if self.is_async {
124            b.write("async ");
125        }
126        if self.is_const {
127            b.write("const ");
128        }
129        self.signature.write_unsafe(b);
130        b.write("fn ");
131        self.write_signature(b);
132        b.space();
133        self.write_curly_statement_block(b, level);
134        b.end_line();
135    }
136}