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
use crate::*;
mod ocaml;
mod rust;
pub use ocaml::OCaml;
pub use rust::Rust;
pub(crate) fn first_uppercase(s: &str) -> String {
let mut s = s.to_string();
if let Some(r) = s.get_mut(0..1) {
r.make_ascii_uppercase();
}
s
}
pub struct Config {
pub output_path: std::path::PathBuf,
pub output_file: std::fs::File,
pub auto_sync: bool,
}
impl Config {
pub fn new(output: impl AsRef<std::path::Path>) -> Result<Config, Error> {
Ok(Config {
output_path: output.as_ref().to_path_buf(),
output_file: std::fs::File::create(output)?,
auto_sync: true,
})
}
pub fn no_auto_sync(mut self) -> Self {
self.auto_sync = false;
self
}
}
pub trait Generate {
fn generate(&mut self, library: &Library, config: &mut Config) -> Result<(), Error>;
}
fn rust() -> Box<impl Generate> {
Box::new(Rust::default())
}
fn ocaml() -> Box<impl Generate> {
Box::new(OCaml::default())
}
impl Config {
pub fn detect(&self) -> Option<Box<dyn Generate>> {
match self
.output_path
.extension()
.map(|x| x.to_str().expect("Invalid extension"))
{
Some("rs") => Some(rust()),
Some("ml") => Some(ocaml()),
_ => None,
}
}
}