Skip to content

Commit d2cd59c

Browse files
committed
Refactor generate_docs from boolean soup to DocAction enum
Replace 6 boolean/string parameters (json, serve_docs, port, static_site, markdown_pages, base_url) with Option<&DocAction> match dispatch. The function body now uses a clean match on DocAction variants instead of cascading if-else chains.
1 parent 80af79f commit d2cd59c

2 files changed

Lines changed: 90 additions & 169 deletions

File tree

crates/fe/src/doc.rs

Lines changed: 85 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,15 @@ impl LspServerInfo {
7575
}
7676
}
7777

78-
#[allow(unused_variables, clippy::too_many_arguments)]
78+
#[allow(unused_variables)]
7979
pub fn generate_docs(
8080
path: &Utf8PathBuf,
8181
output: Option<&Utf8PathBuf>,
82-
json: bool,
83-
serve_docs: bool,
84-
port: u16,
85-
static_site: bool,
86-
markdown_pages: bool,
8782
builtins: bool,
88-
base_url: &str,
83+
action: Option<&crate::DocAction>,
8984
) {
9085
// First, check if there's a running LSP with docs server
91-
if serve_docs {
86+
if matches!(action, Some(crate::DocAction::Serve { .. })) {
9287
let canonical_path = path.canonicalize_utf8().ok();
9388
let start_dir = canonical_path.as_ref().and_then(|p| {
9489
if p.is_file() {
@@ -192,107 +187,96 @@ pub fn generate_docs(
192187
// This enriches rich_signature fields and produces JSON for embedding.
193188
let scip_json = generate_scip_json_for_doc(&mut db, path, &mut index, builtins);
194189

195-
if static_site {
196-
let output_dir = output
197-
.map(|p| p.as_std_path().to_path_buf())
198-
.unwrap_or_else(|| std::path::PathBuf::from("docs"));
199-
200-
// Auto-detect git source link base for GitHub links
201-
let source_link_base = detect_source_link_base(path.as_std_path());
202-
203-
if let Err(e) = fe_web::static_site::StaticSiteGenerator::generate_full(
204-
&index,
205-
&output_dir,
206-
scip_json.as_deref(),
207-
source_link_base.as_deref(),
208-
) {
209-
eprintln!("Error generating static docs: {e}");
210-
std::process::exit(1);
211-
}
212-
213-
// When --json is also set, write docs.json alongside index.html
214-
if json {
215-
let merged = build_merged_json(&index, scip_json.as_deref());
216-
let json_path = output_dir.join("docs.json");
217-
std::fs::write(&json_path, &merged).unwrap_or_else(|e| {
218-
eprintln!("Error writing docs.json: {e}");
219-
std::process::exit(1);
220-
});
221-
}
222-
223-
let suffix = match (scip_json.is_some(), json) {
224-
(true, true) => " (with SCIP + docs.json)",
225-
(true, false) => " (with SCIP)",
226-
(false, true) => " (with docs.json)",
227-
(false, false) => "",
228-
};
229-
println!("Static docs written to {}{suffix}", output_dir.display());
230-
return;
231-
}
232-
233-
if markdown_pages {
234-
let output_dir = output
235-
.map(|p| p.as_std_path().to_path_buf())
236-
.unwrap_or_else(|| std::path::PathBuf::from("docs"));
237-
if let Err(e) = fe_web::starlight::generate(&index, &output_dir, base_url) {
238-
eprintln!("Error generating markdown pages: {e}");
239-
std::process::exit(1);
240-
}
241-
println!("Markdown pages written to {}", output_dir.display());
242-
return;
243-
}
244-
245-
#[cfg(feature = "doc-server")]
246-
if serve_docs {
247-
use crate::doc_serve::{DocServeConfig, serve_docs as serve};
248-
249-
let config = DocServeConfig {
250-
port,
251-
host: "127.0.0.1".to_string(),
252-
};
253-
254-
println!("Starting documentation server...");
255-
println!("Open http://127.0.0.1:{port} in your browser");
256-
println!("Press Ctrl+C to stop");
190+
match action {
191+
Some(crate::DocAction::Static) => {
192+
let output_dir = output
193+
.map(|p| p.as_std_path().to_path_buf())
194+
.unwrap_or_else(|| std::path::PathBuf::from("docs"));
257195

258-
let rt = tokio::runtime::Runtime::new().unwrap();
259-
rt.block_on(async {
260196
let source_link_base = detect_source_link_base(path.as_std_path());
261-
if let Err(e) = serve(index, config, scip_json, source_link_base).await {
262-
eprintln!("Server error: {e}");
197+
198+
if let Err(e) = fe_web::static_site::StaticSiteGenerator::generate_full(
199+
&index,
200+
&output_dir,
201+
scip_json.as_deref(),
202+
source_link_base.as_deref(),
203+
) {
204+
eprintln!("Error generating static docs: {e}");
263205
std::process::exit(1);
264206
}
265-
});
266-
return;
267-
}
268207

269-
#[cfg(not(feature = "doc-server"))]
270-
if serve_docs {
271-
eprintln!("Error: doc-server feature not enabled. Rebuild with --features doc-server");
272-
std::process::exit(1);
273-
}
274-
275-
if json {
276-
// Produce merged docs.json (DocIndex + SCIP) for web component consumption
277-
let merged = build_merged_json(&index, scip_json.as_deref());
278-
if let Some(output_path) = output {
279-
let output_dir = output_path.as_std_path();
280-
std::fs::create_dir_all(output_dir).unwrap_or_else(|e| {
281-
eprintln!("Error creating output directory {output_path}: {e}");
208+
let suffix = if scip_json.is_some() {
209+
" (with SCIP)"
210+
} else {
211+
""
212+
};
213+
println!("Static docs written to {}{suffix}", output_dir.display());
214+
}
215+
Some(crate::DocAction::Json) => {
216+
let merged = build_merged_json(&index, scip_json.as_deref());
217+
if let Some(output_path) = output {
218+
let output_dir = output_path.as_std_path();
219+
std::fs::create_dir_all(output_dir).unwrap_or_else(|e| {
220+
eprintln!("Error creating output directory {output_path}: {e}");
221+
std::process::exit(1);
222+
});
223+
let json_path = output_dir.join("docs.json");
224+
std::fs::write(&json_path, &merged).unwrap_or_else(|e| {
225+
eprintln!("Error writing docs.json: {e}");
226+
std::process::exit(1);
227+
});
228+
println!("Wrote docs.json to {output_path}");
229+
} else {
230+
println!("{merged}");
231+
}
232+
}
233+
Some(crate::DocAction::Pages { base_url }) => {
234+
let output_dir = output
235+
.map(|p| p.as_std_path().to_path_buf())
236+
.unwrap_or_else(|| std::path::PathBuf::from("docs"));
237+
if let Err(e) = fe_web::starlight::generate(&index, &output_dir, base_url) {
238+
eprintln!("Error generating markdown pages: {e}");
282239
std::process::exit(1);
283-
});
284-
let json_path = output_dir.join("docs.json");
285-
std::fs::write(&json_path, &merged).unwrap_or_else(|e| {
286-
eprintln!("Error writing docs.json: {e}");
240+
}
241+
println!("Markdown pages written to {}", output_dir.display());
242+
}
243+
Some(crate::DocAction::Serve { port }) => {
244+
#[cfg(feature = "doc-server")]
245+
{
246+
use crate::doc_serve::{DocServeConfig, serve_docs as serve};
247+
248+
let config = DocServeConfig {
249+
port: *port,
250+
host: "127.0.0.1".to_string(),
251+
};
252+
253+
println!("Starting documentation server...");
254+
println!("Open http://127.0.0.1:{port} in your browser");
255+
println!("Press Ctrl+C to stop");
256+
257+
let rt = tokio::runtime::Runtime::new().unwrap();
258+
rt.block_on(async {
259+
let source_link_base = detect_source_link_base(path.as_std_path());
260+
if let Err(e) = serve(index, config, scip_json, source_link_base).await {
261+
eprintln!("Server error: {e}");
262+
std::process::exit(1);
263+
}
264+
});
265+
}
266+
#[cfg(not(feature = "doc-server"))]
267+
{
268+
eprintln!(
269+
"Error: doc-server feature not enabled. Rebuild with --features doc-server"
270+
);
287271
std::process::exit(1);
288-
});
289-
println!("Wrote docs.json to {output_path}");
290-
} else {
291-
println!("{merged}");
272+
}
273+
}
274+
Some(crate::DocAction::Bundle) => {
275+
unreachable!("Bundle is handled before generate_docs is called")
276+
}
277+
None => {
278+
print_doc_summary(&index);
292279
}
293-
} else {
294-
// Print summary
295-
print_doc_summary(&index);
296280
}
297281
}
298282

crates/fe/src/main.rs

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -487,77 +487,14 @@ pub fn run(opts: &Options) {
487487
output,
488488
builtins,
489489
action,
490-
} => match action {
491-
Some(DocAction::Static) => {
492-
doc::generate_docs(
493-
path,
494-
output.as_ref(),
495-
false,
496-
false,
497-
8080,
498-
true,
499-
false,
500-
*builtins,
501-
"/api",
502-
);
503-
}
504-
Some(DocAction::Json) => {
505-
doc::generate_docs(
506-
path,
507-
output.as_ref(),
508-
true,
509-
false,
510-
8080,
511-
false,
512-
false,
513-
*builtins,
514-
"/api",
515-
);
516-
}
517-
Some(DocAction::Bundle) => {
490+
} => {
491+
if let Some(DocAction::Bundle) = action {
518492
let output_dir = output.clone().unwrap_or_else(|| Utf8PathBuf::from("."));
519493
doc::write_bundle(&output_dir.join("fe-web.js"));
494+
} else {
495+
doc::generate_docs(path, output.as_ref(), *builtins, action.as_ref());
520496
}
521-
Some(DocAction::Pages { base_url }) => {
522-
doc::generate_docs(
523-
path,
524-
output.as_ref(),
525-
false,
526-
false,
527-
8080,
528-
false,
529-
true,
530-
*builtins,
531-
base_url,
532-
);
533-
}
534-
Some(DocAction::Serve { port }) => {
535-
doc::generate_docs(
536-
path,
537-
output.as_ref(),
538-
false,
539-
true,
540-
*port,
541-
false,
542-
false,
543-
*builtins,
544-
"/api",
545-
);
546-
}
547-
None => {
548-
doc::generate_docs(
549-
path,
550-
output.as_ref(),
551-
false,
552-
false,
553-
8080,
554-
false,
555-
false,
556-
*builtins,
557-
"/api",
558-
);
559-
}
560-
},
497+
}
561498
#[cfg(not(target_arch = "wasm32"))]
562499
Command::Tree { path } => {
563500
if tree::print_tree(path) {

0 commit comments

Comments
 (0)