-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.rs
100 lines (89 loc) · 2.87 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use pagefind::{PagefindInboundConfig, SearchOptions, SearchState};
use std::path::PathBuf;
use std::time::Instant;
use twelf::reexports::clap::CommandFactory;
use twelf::Layer;
const CONFIGS: &[&str] = &[
"pagefind.json",
"pagefind.yml",
"pagefind.yaml",
"pagefind.toml",
];
#[tokio::main]
async fn main() {
let start = Instant::now();
let matches = PagefindInboundConfig::command()
// .ignore_errors(true)
.get_matches();
let mut config_layers = vec![];
let configs: Vec<&str> = CONFIGS
.iter()
.filter(|c| std::path::Path::new(c).exists())
.cloned()
.collect();
if configs.len() > 1 {
eprintln!(
"Found multiple possible config files: [{}]",
configs.join(", ")
);
eprintln!("Pagefind only supports loading one configuration file format, please ensure only one file exists.");
return;
}
for config in configs {
let layer_fn = if config.ends_with("json") {
Layer::Json
} else if config.ends_with("toml") {
Layer::Toml
} else if config.ends_with("yaml") || config.ends_with("yml") {
Layer::Yaml
} else {
panic!("Unknown config file format");
};
config_layers.push(layer_fn(config.into()));
}
config_layers.push(Layer::Env(Some("PAGEFIND_".to_string())));
config_layers.push(Layer::Clap(matches));
match PagefindInboundConfig::with_layers(&config_layers) {
Ok(config) => {
if let Ok(options) = SearchOptions::load(config.clone()) {
let mut runner = SearchState::new(options);
runner.run().await;
let duration = start.elapsed();
println!(
"Finished in {}.{} seconds",
duration.as_secs(),
duration.subsec_millis()
);
if config.serve {
pagefind::serve::serve_dir(PathBuf::from(config.source)).await;
}
}
}
Err(e) => {
eprintln!("Error loading Pagefind config:");
match e {
twelf::Error::Io(e) => {
eprintln!("{}", e);
}
twelf::Error::Envy(e) => {
eprintln!("{}", e);
}
twelf::Error::Json(e) => {
eprintln!("{}", e);
}
twelf::Error::Toml(e) => {
eprintln!("{}", e);
}
twelf::Error::Yaml(e) => {
eprintln!("{}", e);
}
twelf::Error::Deserialize(e) => {
eprintln!("{}", e);
}
_ => {
eprintln!("Unknown Error");
}
}
}
}
}