1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::{
backend::{self, BuiltinBackend, Callbacks, Resolver},
documentation::Documentation,
ConfigFile, Error, GodotVersion,
};
use std::{fs, path::PathBuf};
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Package {
Name(String),
Root(PathBuf),
}
#[derive(Debug)]
pub struct Builder {
backends: Vec<(Box<dyn Callbacks>, PathBuf)>,
user_config: ConfigFile,
package: Option<Package>,
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
impl Builder {
pub fn new() -> Self {
Self {
backends: Vec::new(),
user_config: ConfigFile::default(),
package: None,
}
}
pub fn user_config(mut self, config: ConfigFile) -> Self {
self.user_config = config;
self
}
pub fn package(mut self, package: Package) -> Self {
self.package = Some(package);
self
}
pub fn add_backend(mut self, backend: BuiltinBackend, output_dir: PathBuf) -> Self {
let callbacks: Box<dyn Callbacks> = match &backend {
BuiltinBackend::Markdown => Box::new(backend::MarkdownCallbacks::default()),
BuiltinBackend::Html => Box::new(backend::HtmlCallbacks::default()),
BuiltinBackend::Gut => Box::new(backend::GutCallbacks::default()),
};
self.backends.push((callbacks, output_dir));
self
}
pub fn add_backend_with_callbacks(
mut self,
callbacks: Box<dyn Callbacks>,
output_dir: PathBuf,
) -> Self {
self.backends.push((callbacks, output_dir));
self
}
#[allow(clippy::or_fun_call)]
pub fn build(mut self) -> Result<(), Error> {
let mut resolver = Resolver::new(match &self.user_config.godot_version {
Some(s) => GodotVersion::try_from(s.as_str())?,
None => GodotVersion::Version35,
});
let (markdown_options, opening_comment) = {
let opening_comment = self.user_config.opening_comment.unwrap_or(true);
let markdown_options = self
.user_config
.markdown_options()
.unwrap_or(pulldown_cmark::Options::empty());
resolver.apply_user_config(&self.user_config);
(markdown_options, opening_comment)
};
let documentation = self.build_documentation(&resolver)?;
for (mut callbacks, output_dir) in self.backends {
let generator = backend::Generator::new(
&resolver,
&documentation,
markdown_options,
opening_comment,
);
let files = callbacks.generate_files(generator);
if let Err(err) = fs::create_dir_all(&output_dir) {
return Err(Error::Io(output_dir, err));
}
for (file_name, content) in files {
let out_file = output_dir.join(file_name);
if let Err(err) = fs::write(&out_file, content) {
return Err(Error::Io(out_file, err));
}
}
}
Ok(())
}
fn build_documentation(&mut self, resolver: &Resolver) -> Result<Documentation, Error> {
log::debug!("building documentation");
let (name, root_file) = match self.package.take() {
Some(Package::Root(root_file)) => ("_".to_string(), root_file),
Some(Package::Name(name)) => find_root_file(Some(&name))?,
None => find_root_file(None)?,
};
let mut documentation = Documentation::from_root_file(name, root_file)?;
resolver.rename_classes(&mut documentation);
Ok(documentation)
}
}
fn find_root_file(package_name: Option<&str>) -> Result<(String, PathBuf), Error> {
let metadata = cargo_metadata::MetadataCommand::new().exec()?;
let mut root_files = Vec::new();
for package in metadata.packages {
if metadata.workspace_members.contains(&package.id) {
if let Some(target) = package
.targets
.into_iter()
.find(|target| target.kind.iter().any(|kind| kind == "cdylib"))
{
root_files.push((package.name, target.src_path.into()))
}
}
}
if let Some(package_name) = package_name {
match root_files
.into_iter()
.find(|(name, _)| name == package_name)
{
Some((_, root_file)) => Ok((package_name.to_string(), root_file)),
None => Err(Error::NoMatchingCrate(package_name.to_string())),
}
} else {
if root_files.len() > 1 {
return Err(Error::MultipleCandidateCrate(
root_files.into_iter().map(|(name, _)| name).collect(),
));
}
if let Some((name, root_file)) = root_files.pop() {
Ok((name, root_file))
} else {
Err(Error::NoCandidateCrate)
}
}
}