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
use cargo_metadata::{CargoOpt, MetadataCommand, Target};
use log::{debug, info};
use std::{borrow::Cow, collections::HashMap, env, fmt::Display, fs, path::PathBuf};
#[doc(hidden)]
pub mod depinfo;
#[doc(hidden)]
pub mod diagnostic;
#[doc(hidden)]
pub mod input;
#[doc(hidden)]
pub mod links;
#[doc(hidden)]
pub mod output;
#[doc(hidden)]
pub mod preproc;
#[doc(hidden)]
pub mod verify;
use crate::input::Scope;
use diagnostic::Diagnostic;
use input::{CrateCode, InputFile, TargetType};
#[doc(hidden)]
pub fn read_input(
manifest_path: Option<PathBuf>,
prefer_bin: bool,
expand_macros: bool,
template: PathBuf
) -> (InputFile, Cow<'static, str>, Diagnostic) {
fn fail<T: Display>(msg: T) -> (InputFile, Cow<'static, str>, Diagnostic) {
let input = InputFile {
crate_name: "N/A".into(),
target_type: TargetType::Lib,
repository: None,
license: None,
rust_version: None,
rustdoc: String::new(),
dependencies: HashMap::new(),
scope: Scope::empty()
};
let template = "".into();
let mut diagnostic = Diagnostic::new("<none>".into(), String::new());
diagnostic.error(msg);
(input, template, diagnostic)
}
trait Fail {
type Ok;
fn fail(self, msg: &'static str) -> Result<Self::Ok, Cow<'static, str>>;
}
impl<T> Fail for Option<T> {
type Ok = T;
fn fail(self, msg: &'static str) -> Result<Self::Ok, Cow<'static, str>> {
self.ok_or(Cow::Borrowed(msg))
}
}
impl<T, E: Display> Fail for Result<T, E> {
type Ok = T;
fn fail(self, msg: &'static str) -> Result<T, Cow<'static, str>> {
self.map_err(|err| format!("{msg}: {err}").into())
}
}
macro_rules! unwrap {
($expr:expr) => {
match $expr {
Ok(ok) => ok,
Err(err) => return fail(err)
}
};
($expr:expr, $msg:literal) => {
match Fail::fail($expr, $msg) {
Ok(ok) => ok,
Err(err) => return fail(err)
}
};
}
let manifest_path = match manifest_path {
Some(path) if path.is_relative() => Some(env::current_dir().unwrap().join(path)),
Some(path) => Some(path),
None => None
};
let mut cmd = MetadataCommand::new();
cmd.features(CargoOpt::AllFeatures);
if let Some(path) = &manifest_path {
cmd.manifest_path(path);
}
let metadata = unwrap!(cmd.exec(), "Failed to get cargo metadata");
let pkg = unwrap!(
metadata.root_package(),
"Missing package. Please make sure there is a package here, workspace roots don't contain any documentation."
);
let is_lib = |target: &&Target| target.is_lib();
let is_default_bin =
|target: &&Target| target.is_bin() && target.name == pkg.name.as_str();
let target_and_type = if prefer_bin {
pkg.targets
.iter()
.find(is_default_bin)
.map(|target| (target, TargetType::Bin))
.or_else(|| {
pkg.targets
.iter()
.find(is_lib)
.map(|target| (target, TargetType::Lib))
})
} else {
pkg.targets
.iter()
.find(is_lib)
.map(|target| (target, TargetType::Lib))
.or_else(|| {
pkg.targets
.iter()
.find(is_default_bin)
.map(|target| (target, TargetType::Bin))
})
};
let (target, target_type) = unwrap!(
target_and_type.or_else(|| {
pkg.targets
.iter()
.find(|target| target.is_bin())
.map(|target| (target, TargetType::Bin))
}),
"Failed to find a library or binary target"
);
let template: Cow<'static, str> = if template.exists() {
unwrap!(fs::read_to_string(template), "Failed to read template").into()
} else {
include_str!("README.j2").into()
};
let file = target.src_path.as_std_path();
let filename = file
.file_name()
.expect("File has no filename")
.to_string_lossy()
.into_owned();
let code = if expand_macros {
unwrap!(
CrateCode::read_expansion(manifest_path.as_ref(), target),
"Failed to read crate code"
)
} else {
unwrap!(CrateCode::read_from_disk(file), "Failed to read crate code")
};
let mut diagnostics = Diagnostic::new(filename, code.0.clone());
info!("Reading {}", file.display());
let input_file =
input::read_code(&metadata, pkg, code, target_type, &mut diagnostics);
debug!("Processing {input_file:#?}");
(input_file, template, diagnostics)
}