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
mod context;
mod file;
mod visitor;
mod file_generator;
mod ffi_generator;
pub use context::*;
pub use visitor::*;
pub use file::*;
pub use file_generator::*;
pub use ffi_generator::*;
use crate::prelude::*;
use crate::ir::{Implementation, Attributes};
use crate::utils::fs::write_file;
pub trait Generator: FileGenerator + FFIGenerator {
fn new(context: &Context, attributes: &Attributes) -> Self where Self: Sized;
fn pre_process(&self, _context: &Context, implementation: Option<&Implementation>) -> Option<Implementation> {
implementation.map(|implementation| {
let mut implementation = implementation.clone();
implementation.replace_self_with_explicit_names();
implementation
})
}
fn generate(&self, context: &Context, implementation: Option<&Implementation>) -> Result<TokenStream> {
let implementation = self.pre_process(context, implementation);
let implementation = implementation.map(|implementation| Visitor::new((), implementation));
let implementation = implementation.as_ref();
let mut file_set = FileSet::default();
self.generate_files(&context, &mut file_set, implementation);
self.save_file_set(context, file_set)?;
Ok(self.generate_ffi(&context, implementation))
}
fn save_file_set(&self, context: &Context, file_set: FileSet) -> Result<()> {
let target_ligen_dir = &context.arguments.target_dir.join("ligen");
let project_dir = target_ligen_dir.join(&context.arguments.crate_name);
for (_path, file) in file_set.files {
let file_path = project_dir.join(file.path);
write_file(&file_path, file.content)?;
}
Ok(())
}
}