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
|
use proc_macro::TokenStream;
use quote::quote;
use std::fs;
use std::path::Path;
#[proc_macro]
pub fn generate_helpdoc_mapping(_input: TokenStream) -> TokenStream {
let manifest_dir =
std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
let repo_root = Path::new(&manifest_dir);
let helpdoc_dir = repo_root.join("resources").join("helpdoc");
if !helpdoc_dir.exists() {
return quote! {
fn get_doc(_doc_name: &str, _lang: &str) -> &'static str {
""
}
}
.into();
}
let mut doc_entries = Vec::new();
scan_directory(&helpdoc_dir, &mut doc_entries, &helpdoc_dir);
let match_arms = generate_match_arms(&doc_entries);
let expanded = quote! {
fn get_doc(doc_name: &str, lang: &str) -> &'static str {
let key = format!("{}.{}", doc_name, lang);
match key.as_str() {
#(#match_arms)*
_ => "",
}
}
};
expanded.into()
}
fn scan_directory(dir: &Path, entries: &mut Vec<(String, String)>, base_dir: &Path) {
if let Ok(entries_iter) = fs::read_dir(dir) {
for entry in entries_iter.filter_map(Result::ok) {
let path = entry.path();
if path.is_dir() {
scan_directory(&path, entries, base_dir);
} else if let Some(extension) = path.extension()
&& extension == "md"
&& let Ok(relative_path) = path.strip_prefix(base_dir)
&& let Some(file_stem) = path.file_stem() {
let file_stem_str = file_stem.to_string_lossy();
if let Some(dot_pos) = file_stem_str.rfind('.') {
let doc_name = &file_stem_str[..dot_pos];
let lang = &file_stem_str[dot_pos + 1..];
let parent = relative_path.parent();
let full_doc_name = if let Some(parent) = parent {
if parent.to_string_lossy().is_empty() {
doc_name.to_string()
} else {
format!("{}/{}", parent.to_string_lossy(), doc_name)
}
} else {
doc_name.to_string()
};
let full_doc_name = just_fmt::fmt_path::fmt_path(full_doc_name)
.unwrap()
.to_string_lossy()
.to_string();
entries.push((full_doc_name, lang.to_string()));
}
}
}
}
}
fn generate_match_arms(entries: &[(String, String)]) -> Vec<proc_macro2::TokenStream> {
let mut arms = Vec::new();
for (doc_name, lang) in entries {
let key = format!("{}.{}", doc_name, lang);
let file_path = format!("resources/helpdoc/{}.{}.md", doc_name, lang);
let arm = quote! {
#key => include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", #file_path)),
};
arms.push(arm);
}
arms
}
#[proc_macro]
pub fn generate_helpdoc_list(_input: TokenStream) -> TokenStream {
let manifest_dir =
std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
let repo_root = Path::new(&manifest_dir);
let helpdoc_dir = repo_root.join("resources").join("helpdoc");
if !helpdoc_dir.exists() {
return quote! {
fn get_docs_list() -> Vec<&'static str> {
Vec::new()
}
}
.into();
}
let mut doc_entries = Vec::new();
scan_directory(&helpdoc_dir, &mut doc_entries, &helpdoc_dir);
let mut unique_docs = std::collections::HashSet::new();
for (doc_name, _) in &doc_entries {
unique_docs.insert(doc_name.clone());
}
let mut doc_list = Vec::new();
for doc_name in unique_docs {
doc_list.push(quote! {
#doc_name
});
}
let expanded = quote! {
fn get_docs_list() -> Vec<&'static str> {
vec![
#(#doc_list),*
]
}
};
expanded.into()
}
#[proc_macro]
pub fn generate_helpdoc_test(_input: TokenStream) -> TokenStream {
let manifest_dir =
std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
let repo_root = Path::new(&manifest_dir);
let helpdoc_dir = repo_root.join("resources").join("helpdoc");
if !helpdoc_dir.exists() {
return quote! {
#[cfg(test)]
mod helpdoc_tests {
#[test]
fn test_no_docs() {
}
}
}
.into();
}
let mut doc_entries = Vec::new();
scan_directory(&helpdoc_dir, &mut doc_entries, &helpdoc_dir);
let mut test_cases = Vec::new();
for (doc_name, lang) in &doc_entries {
let test_name_str = format!(
"test_doc_{}_{}",
doc_name
.replace(['/', '.', '-'], "_"),
lang.replace('-', "_")
);
let test_name = syn::Ident::new(&test_name_str, proc_macro2::Span::call_site());
let test_case = quote! {
#[test]
fn #test_name() {
let doc = super::get_doc(#doc_name, #lang);
assert!(!doc.is_empty(), "Document {}.{} should not be empty", #doc_name, #lang);
}
};
test_cases.push(test_case);
}
let expanded = quote! {
#[cfg(test)]
mod helpdoc_tests {
#(#test_cases)*
}
};
expanded.into()
}
|