Skip to content

Commit

Permalink
chore: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mikirov committed Aug 9, 2023
1 parent b3a078d commit fe26ef2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
5 changes: 2 additions & 3 deletions src/injecting/injections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use wasm_instrument::parity_wasm::elements::{
use super::injector::FunctionMapper;

/// # Injection enum
///
///
/// This enum is used to select which injection to perform i.e what instructions to insert in the beginning of the WASM module.
/// # Example
///
///
/// ```
/// let mut module = load_module(); // you can use wasm_injector::util to load a module
/// let injection = Injection::Noops;
Expand All @@ -25,7 +25,6 @@ pub enum Injection {
HeapOverflow,
}


impl Injection {
/// # Takes a module and injects the selected injection into the module.
pub fn inject(self, module: &mut Module, function: &str) -> Result<(), String> {
Expand Down
6 changes: 3 additions & 3 deletions src/injecting/injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pub trait FunctionMapper {

impl FunctionMapper for Module {
/// # Takes a module and a function name and returns the global function index of the function.
///
///
/// # Errors
///
///
/// - Returns an error if the function is not found in the export section.
/// - Returns an error if the u32 cannot be mapped to usize.
fn get_global_function_index(&mut self, function_name: &str) -> Result<usize, String> {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl FunctionMapper for Module {
}

/// # Takes a module, a local function index and a function name and returns the function body.
///
///
/// # Errors
/// - Returns an error if the code section is not found.
/// - Returns an error if the local function index is not in the code section.
Expand Down
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! # WASM Injector
//!
//!
//! This crate provides a library for injecting WebAssembly modules with
//! predefined injections. The injections are primarily meant to be used for testing polkadot parachains hosts.
//! # Example
//! ```
//! use wasm_injector::{load_module_from_wasm, save_module_to_wasm, Injection};
//!
//!
//! let mut module = load_module_from_wasm(source.as_path())?; // supply your own path here
//! let injection = Injection::Noops; // choose your injection
//! injection.inject(&mut module)?; // inject the module
Expand All @@ -16,11 +16,10 @@ pub mod injecting;
pub mod stack_limiter;
pub mod util;


pub use self::injecting::injections::Injection;
pub use self::util::blob_from_module;
pub use self::util::module_from_blob;
pub use self::util::hexify_bytes;
pub use self::util::load_module_from_wasm;
pub use self::util::module_from_blob;
pub use self::util::save_module_to_wasm;
pub use self::util::hexify_bytes;
pub use self::util::unhexify_bytes;
pub use self::injecting::injections::Injection;
22 changes: 11 additions & 11 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use wasm_instrument::parity_wasm::{deserialize_buffer, elements::Module};

/// # Save bytes to a file in the given path
/// It will recursively create parent directories if needed, open and write to the file
///
///
/// # Errors
///
///
/// - If the file could not be opened or written to it will return an error
/// - If the parent directories could not be created it will return an error
pub fn save(path: &Path, bytes: &[u8]) -> Result<(), String> {
Expand All @@ -34,9 +34,9 @@ pub fn save(path: &Path, bytes: &[u8]) -> Result<(), String> {
}

/// # Get the filename from a path
///
///
/// # Errors
///
///
/// - If the path is not a file it will return an error
/// - If the filename could not be converted to a string it will return an error
pub fn get_file_name(path: &Path) -> Result<&str, String> {
Expand All @@ -61,9 +61,9 @@ pub fn modify_file_name(path: &Path, mapper: impl Fn(&str) -> String) -> Result<
}

/// # Extract the module from the (maybe hexified) (maybe compressed) WASM bytes
///
///
/// # Errors
///
///
/// - If the bytes could not be unhexified it will return an error
/// - If the bytes could not be decompressed it will return an error
/// - If the bytes could not be deserialized it will return an error
Expand All @@ -85,15 +85,15 @@ pub fn module_from_blob(blob_bytes: &[u8]) -> Result<Module, String> {
}

/// # Serialize the module into bytes
///
///
/// # Errors
/// - If the module could not be serialized it will return an error
pub fn blob_from_module(module: Module) -> Result<Vec<u8>, String> {
serialize(module).map_err(|err| format!("Could not serialize module: {}", err))
}

/// #Load a module from a wasm file in the given path
///
///
/// # Errors
/// - If the file could not be read it will return an error
/// - If the module could not be deserialized it will return an error
Expand All @@ -109,7 +109,7 @@ pub fn load_module_from_wasm(path: &Path) -> Result<Module, String> {
}

/// # Save a module to a wasm file in the given path
///
///
/// # Errors
/// - If the module could not be compressed it will return an error
pub fn save_module_to_wasm(
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn save_module_to_wasm(
}

/// function hexify_bytes
///
///
/// Takes in bytes and returns a hexified version of them
pub fn hexify_bytes(bytes: Vec<u8>) -> Vec<u8> {
let mut hexified_bytes = bytes
Expand All @@ -158,7 +158,7 @@ pub fn hexify_bytes(bytes: Vec<u8>) -> Vec<u8> {
}

/// function unhexify_bytes
///
///
/// Takes in hexified bytes and returns the original bytes
pub fn unhexify_bytes(bytes: Vec<u8>) -> Result<Vec<u8>, String> {
bytes
Expand Down

0 comments on commit fe26ef2

Please sign in to comment.