cfsp/parse/
mod.rs

1use std::io::Read;
2
3pub use error::{ParseError, ParseResult};
4pub use signature::{class_signature, field_signature, method_signature};
5
6use crate::node::class::Class;
7
8mod attribute;
9mod class;
10mod constant;
11mod error;
12mod field;
13mod method;
14mod signature;
15
16/// Parses class file based on given options.
17pub fn to_class<R: Read>(class_bytes: &mut R, option: ParsingOption) -> ParseResult<Class> {
18    let class = class::class(class_bytes, option)?;
19
20    let mut remain = vec![];
21
22    class_bytes.read_to_end(&mut remain)?;
23
24    if !remain.is_empty() {
25        Err(ParseError::Remains(remain.len()))
26    } else {
27        Ok(class)
28    }
29}
30
31/// Parsing options allows marking some parsing phase optional.
32#[derive(Debug, Default)]
33pub struct ParsingOption {
34    skip_attribute: bool,
35    skip_instruction: bool,
36}
37
38impl ParsingOption {
39    /// Skips on attribute parsing.
40    pub fn skip_attribute(mut self) -> Self {
41        self.skip_attribute = true;
42
43        self
44    }
45
46    /// Skips on instruction parsing on code attribute.
47    pub fn skip_instruction(mut self) -> Self {
48        self.skip_instruction = true;
49
50        self
51    }
52}