1use std::fmt::{self, Write};
2
3use crate::docs::Docs;
4use crate::formatter::Formatter;
5use crate::function::Function;
6use crate::scope::Scope;
7
8use crate::r#enum::Enum;
9use crate::r#impl::Impl;
10use crate::r#struct::Struct;
11use crate::r#trait::Trait;
12
13#[derive(Debug, Clone)]
15pub struct Module {
16 pub name: String,
18
19 vis: Option<String>,
21
22 docs: Option<Docs>,
24
25 scope: Scope,
27
28 attributes: Vec<String>,
30}
31
32impl Module {
33 pub fn new(name: &str) -> Self {
35 Module {
36 name: name.to_string(),
37 vis: None,
38 docs: None,
39 scope: Scope::new(),
40 attributes: Vec::new(),
41 }
42 }
43
44 pub fn scope(&mut self) -> &mut Scope {
46 &mut self.scope
47 }
48
49 pub fn vis(&mut self, vis: &str) -> &mut Self {
51 self.vis = Some(vis.to_string());
52 self
53 }
54
55 pub fn import(&mut self, path: &str, ty: &str) -> &mut Self {
60 self.scope.import(path, ty);
61 self
62 }
63
64 pub fn attr(&mut self, attribute: impl Into<String>) -> &mut Self {
66 self.attributes.push(attribute.into());
67 self
68 }
69
70 pub fn new_module(&mut self, name: &str) -> &mut Module {
83 self.scope.new_module(name)
84 }
85
86 pub fn get_module<Q: ?Sized>(&self, name: &Q) -> Option<&Module>
88 where
89 String: PartialEq<Q>,
90 {
91 self.scope.get_module(name)
92 }
93
94 pub fn get_module_mut<Q: ?Sized>(&mut self, name: &Q) -> Option<&mut Module>
96 where
97 String: PartialEq<Q>,
98 {
99 self.scope.get_module_mut(name)
100 }
101
102 pub fn get_or_new_module(&mut self, name: &str) -> &mut Module {
105 self.scope.get_or_new_module(name)
106 }
107
108 pub fn push_module(&mut self, item: Module) -> &mut Self {
121 self.scope.push_module(item);
122 self
123 }
124
125 pub fn new_struct(&mut self, name: &str) -> &mut Struct {
127 self.scope.new_struct(name)
128 }
129
130 pub fn push_struct(&mut self, item: Struct) -> &mut Self {
132 self.scope.push_struct(item);
133 self
134 }
135
136 pub fn new_fn(&mut self, name: &str) -> &mut Function {
138 self.scope.new_fn(name)
139 }
140
141 pub fn push_fn(&mut self, item: Function) -> &mut Self {
143 self.scope.push_fn(item);
144 self
145 }
146
147 pub fn new_enum(&mut self, name: &str) -> &mut Enum {
149 self.scope.new_enum(name)
150 }
151
152 pub fn push_enum(&mut self, item: Enum) -> &mut Self {
154 self.scope.push_enum(item);
155 self
156 }
157
158 pub fn new_impl(&mut self, target: &str) -> &mut Impl {
160 self.scope.new_impl(target)
161 }
162
163 pub fn push_impl(&mut self, item: Impl) -> &mut Self {
165 self.scope.push_impl(item);
166 self
167 }
168
169 pub fn new_trait(&mut self, name: impl Into<String>) -> &mut Trait {
171 self.scope.new_trait(name)
172 }
173
174 pub fn push_trait(&mut self, item: Trait) -> &mut Self {
176 self.scope.push_trait(item);
177 self
178 }
179
180 pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
182 for attr in &self.attributes {
183 writeln!(fmt, "#[{}] ", attr)?;
184 }
185
186 if let Some(ref vis) = self.vis {
187 write!(fmt, "{} ", vis)?;
188 }
189
190 write!(fmt, "mod {}", self.name)?;
191 fmt.block(|fmt| self.scope.fmt(fmt))
192 }
193}