-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
78 lines (63 loc) · 2.09 KB
/
main.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
mod cmdline;
mod module;
mod output;
mod pipeline;
use anyhow::Result;
use cmdline::{CommandLine, OutputFormat};
use crate::output::DocsetOutput;
use crate::output::JsonOutput;
use crate::output::{OutputWriter, PlainOutput};
use crate::pipeline::Pipeline;
#[derive(Debug, PartialEq, Eq)]
struct ItemInfo {
id: String,
module: String,
}
fn get_output_writer(cmdline: &CommandLine) -> Result<Box<dyn OutputWriter>> {
let stdout = std::io::stdout();
let stdout = stdout.lock();
match cmdline.output_format {
OutputFormat::Plain => {
let plain = PlainOutput::new(stdout);
Ok(Box::new(plain))
}
OutputFormat::Json => {
let json = JsonOutput::new(stdout);
Ok(Box::new(json))
}
OutputFormat::Docset => {
let docset = DocsetOutput::new(
cmdline.library_name.clone(),
std::env::current_dir()?,
cmdline.html_dir.clone(),
cmdline.main_page.clone(),
);
Ok(Box::new(docset))
}
}
}
fn main() -> Result<()> {
let cmdline = cmdline::parse();
let mut output = get_output_writer(&cmdline)?;
let pipeline = {
let pipeline = Pipeline::new();
let html_dir = &cmdline.html_dir;
let module_paths = std::fs::read_dir(html_dir)?;
module_paths.into_iter().for_each(|entry| match entry {
Ok(entry) => {
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "html") {
pipeline.process_module(path);
} else {
eprintln!("Skipping non-HTML file {}", path.display());
}
}
Err(err) => eprintln!("Failed to read entry from {}: {err}", html_dir.display()),
});
pipeline
};
output.write_output(pipeline.consume())
}