Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Pfretzschner committed May 30, 2024
1 parent 6897691 commit 56806a7
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 42 deletions.
10 changes: 7 additions & 3 deletions loader/src/classfile_loader.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anyhow::{anyhow, Result};
use glob::glob;
use log::debug;
use model::api::Parser;
use model::class::JvmClass;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use log::debug;

use model::api::Classloader;

Expand All @@ -28,8 +28,12 @@ impl ClassfileLoader {

debug!("Parsing classfile {}", file_path.display());

parser.parse(&mut reader).map(|class| (classpath, class))
.map_err(|err| anyhow!("Failed to parse classfile {}: {}", file_path.display(), err))
parser
.parse(&mut reader)
.map(|class| (classpath, class))
.map_err(|err| {
anyhow!("Failed to parse classfile {}: {}", file_path.display(), err)
})
})
.collect::<Result<_>>()?;

Expand Down
6 changes: 4 additions & 2 deletions loader/src/jarfile_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use anyhow::anyhow;
use anyhow::Result;
use model::api::Classloader;
use model::api::Parser;
use model::class::JvmClass;
Expand Down Expand Up @@ -59,7 +59,9 @@ fn parse_classfiles(
Some((path, parser.parse(&mut file)))
})
.map(|(path, class_result)| {
class_result.map(|class| (path, class)).map_err(|err| anyhow!(err))
class_result
.map(|class| (path, class))
.map_err(|err| anyhow!(err))
})
.collect()
}
Expand Down
18 changes: 9 additions & 9 deletions model/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,33 @@ pub enum ClassConstant {
pub enum FieldAccessFlag {
/// Declared public; may be accesse from outside its package.
Public,

/// Declared private; usable only within the defining class.
Private,
Private,

/// Declared protected; may be accessed within subclasses.
Protected,
Protected,

/// Declared static.
Static,
Static,

/// Declared final; never directly assigned to after object construction.
Final,
Final,

/// Declared volatile; cannot be cached.
Volatile,
Volatile,

/// Declared transient; not written or read by a persistent object manager.
Transient,
Transient,

/// Declared synthetic; not present in the source code.
Synthetic,
Synthetic,

/// Declared as an annotation type.
Annotation,

/// Declared as an element of an enum.
Enum,
Enum,
}

pub type ClassFields = Vec<ClassField>;
Expand Down
6 changes: 1 addition & 5 deletions model/src/class_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ impl ClassConstants {
))?;
match constant {
&ClassConstant::Class(ref string) => Ok(string),
it => bail!(
"Expected Class but found {:?} at index {}",
it,
index
),
it => bail!("Expected Class but found {:?} at index {}", it, index),
}
}
}
4 changes: 1 addition & 3 deletions parser/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub fn map(classfile: &ClassFile, _constants: &ClassConstants) -> Result<ClassAt
classfile
.attributes
.iter()
.map(|attribute| {
Ok(ClassAttribute::NotImplemented)
})
.map(|attribute| Ok(ClassAttribute::NotImplemented))
.collect()
}
22 changes: 16 additions & 6 deletions parser/src/class_info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use anyhow::{bail, Result};
use classfile_parser::{types::ClassFile, ClassAccessFlags};
use enumset::EnumSet;
use model::class::{ClassConstants, ClassAccessFlag};

pub fn map(classfile: &ClassFile, constants: &ClassConstants) -> Result<(EnumSet<ClassAccessFlag>, String, Option<String>, Vec<String>)> {
use model::class::{ClassAccessFlag, ClassConstants};

pub fn map(
classfile: &ClassFile,
constants: &ClassConstants,
) -> Result<(
EnumSet<ClassAccessFlag>,
String,
Option<String>,
Vec<String>,
)> {
let access_flags = Wrap(classfile.access_flags).try_into()?;
let this_class = constants.get_class(classfile.this_class)?.into();

Expand All @@ -13,9 +21,11 @@ pub fn map(classfile: &ClassFile, constants: &ClassConstants) -> Result<(EnumSet
None
};

let interfaces: Vec<_> = classfile.interfaces.iter().map(|index|
constants.get_class(*index).map(|s| s.to_owned())
).collect::<Result<_>>()?;
let interfaces: Vec<_> = classfile
.interfaces
.iter()
.map(|index| constants.get_class(*index).map(|s| s.to_owned()))
.collect::<Result<_>>()?;

Ok((access_flags, this_class, super_class, interfaces))
}
Expand Down
4 changes: 2 additions & 2 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use anyhow::{bail, Result};
use model::class::{ClassVersion, JvmClass};
use std::io::Read;

mod type_signature;
mod method_signature;
mod type_signature;

mod attributes;
mod class_info;
mod constants;
mod fields;
mod methods;
mod attributes;

pub struct ClassfileParser {}

Expand Down
6 changes: 3 additions & 3 deletions parser/src/methods.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Result, bail};
use anyhow::{bail, Result};
use classfile_parser::{method_info::MethodAccessFlags, types::ClassFile};
use enumset::EnumSet;
use model::class::{ClassConstants, ClassMethods, ClassMethod, MethodAccessFlag};
use classfile_parser::{types::ClassFile, method_info::MethodAccessFlags};
use model::class::{ClassConstants, ClassMethod, ClassMethods, MethodAccessFlag};

use crate::method_signature::parse_method_signature;

Expand Down
6 changes: 3 additions & 3 deletions parser/src/type_signature.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::Chars;
use std::iter::Peekable;
use std::str::Chars;

use anyhow::{Result, anyhow, bail};
use anyhow::{anyhow, bail, Result};
use model::class::TypeSignature;

pub fn parse_type_signature(spec: &String) -> Result<TypeSignature> {
Expand All @@ -27,4 +27,4 @@ pub(crate) fn parse_type(iterator: &mut Peekable<Chars>) -> Result<TypeSignature

fn read_class_path(iterator: &mut Peekable<Chars>) -> String {
iterator.take_while(|c| *c != ';').collect::<String>()
}
}
7 changes: 2 additions & 5 deletions parser/tests/class_with_fields_only.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use enumset::EnumSet;
use model::class::{TypeSignature, ClassAccessFlag, FieldAccessFlag};
use model::class::{ClassAccessFlag, FieldAccessFlag, TypeSignature};

mod test_utils;

Expand All @@ -24,10 +24,7 @@ fn validate_against_javap() {
assert_eq!("testdata/ClassOnlyWithFields", class.this_class);

// super_class: #11 // java/lang/Object
assert_eq!(
Some("java/lang/Object".to_string()),
class.super_class
);
assert_eq!(Some("java/lang/Object".to_string()), class.super_class);

// interfaces: 0, fields: 5, methods: 2, attributes: 1
assert_eq!(0, class.interfaces.len());
Expand Down
5 changes: 4 additions & 1 deletion rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use loader::ClassfileLoader;
use model::api::{Classloader, Parser};

pub fn make_classloader(parser: &impl Parser) -> impl Classloader {
let rt_path = std::env::current_dir().unwrap().join("..").join("rt/jmods/java.base/classes");
let rt_path = std::env::current_dir()
.unwrap()
.join("..")
.join("rt/jmods/java.base/classes");
println!("### {:?}", rt_path);
ClassfileLoader::open(rt_path, parser).unwrap()
}

0 comments on commit 56806a7

Please sign in to comment.