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
use super::*;
use config::runtime::*;
use java::*;
use std::collections::*;
use std::error::Error;
use std::fs::*;
use std::io;
use std::path::*;
use std::result::Result;
pub struct RunResult {
pub features: BTreeMap<String, BTreeSet<String>>,
}
pub fn run(config: impl Into<Config>) -> Result<RunResult, Box<dyn Error>> {
let config : Config = config.into();
if config.logging_verbose {
println!("output: {}", config.output_path.display());
}
for file in config.input_files.iter() {
println!("cargo:rerun-if-changed={}", file.display());
}
let mut context = emit_rust::Context::new(&config);
for file in config.input_files.iter() {
gather_file(&mut context, file)?;
}
{
let mut out = util::GeneratedFile::new(&context, &config.output_path).unwrap();
context.write(&mut out)?;
context.completed_file(out)?;
}
Ok(RunResult{
features: context.features.clone(),
})
}
fn gather_file(context: &mut emit_rust::Context, path: &Path) -> Result<(), Box<dyn Error>> {
context.progress.lock().unwrap().update(format!("reading {}...", path.display()).as_str());
let ext = if let Some(ext) = path.extension() {
ext
} else {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Input files must have an extension"))?;
};
match ext.to_string_lossy().to_ascii_lowercase().as_str() {
"class" => {
let mut file = File::open(path)?;
let class = Class::read(&mut file)?;
context.add_struct(class)?;
},
"jar" => {
let mut jar = File::open(path)?;
let mut jar = zip::ZipArchive::new(&mut jar)?;
let n = jar.len();
for i in 0..n {
let mut file = jar.by_index(i)?;
if !file.name().ends_with(".class") { continue; }
context.progress.lock().unwrap().update(format!(" reading {:3}/{}: {}...", i, n, file.name()).as_str());
let class = Class::read(&mut file)?;
context.add_struct(class)?;
}
},
unknown => {
return Err(io::Error::new(io::ErrorKind::InvalidInput, format!("Input files must have a '.class' or '.jar' extension, not a '.{}' extension", unknown)))?;
}
}
Ok(())
}