use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::Path;
#[macro_use]
extern crate nom;
#[macro_use]
extern crate bitflags;
pub mod attribute_info;
pub mod constant_info;
pub mod field_info;
pub mod method_info;
pub mod code_attribute;
pub mod parser;
pub mod types;
pub use parser::class_parser;
pub use types::*;
pub fn parse_class(class_name: &str) -> Result<ClassFile, String> {
let class_file_name = &format!("{}.class", class_name);
let path = Path::new(class_file_name);
let display = path.display();
let file = match File::open(path) {
Err(why) => {
return Err(format!("Unable to open {}: {}", display, &why.to_string()));
}
Ok(file) => file,
};
let mut reader = BufReader::new(file);
parse_class_from_reader(&mut reader, display.to_string())
}
pub fn parse_class_from_reader<T: Read>(
reader: &mut T,
file_path: String,
) -> Result<ClassFile, String> {
let mut class_bytes = Vec::new();
if let Err(why) = reader.read_to_end(&mut class_bytes) {
return Err(format!(
"Unable to read {}: {}",
file_path,
&why.to_string()
));
}
let parsed_class = class_parser(&class_bytes);
match parsed_class {
Ok((_, c)) => Ok(c),
_ => Err(format!("Failed to parse classfile {}", file_path)),
}
}