Skip to content

Commit 10fd265

Browse files
committed
documentation
1 parent 2bc6e69 commit 10fd265

File tree

28 files changed

+288
-89
lines changed

28 files changed

+288
-89
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ rust.missing_debug_implementations = "warn"
4545
rust.missing_docs = "warn"
4646
rust.unreachable_pub = "warn"
4747
rust.unused_must_use = "deny"
48-
rust.rust_2018_idioms = "deny"
48+
rust.rust_2018_idioms = { level = "deny", priority = -1 }
4949
rustdoc.all = "warn"
5050

5151
[workspace.lints.clippy]

crates/common/src/ether/bytecode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ pub fn remove_pushbytes_from_bytecode(bytecode: alloy::primitives::Bytes) -> Res
4242

4343
let mut i = 0;
4444
while i < bytecode.len() {
45+
pruned.push(bytecode[i]);
46+
4547
if push_range.contains(&bytecode[i]) {
46-
pruned.push(bytecode[i]);
4748
i += bytecode[i] as usize - 0x5f + 1;
4849
} else {
49-
pruned.push(bytecode[i]);
5050
i += 1;
5151
}
5252
}

crates/common/src/ether/types.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ fn extract_types_from_string(string: &str) -> Result<Vec<DynSolType>> {
7575
let array_range = find_balanced_encapsulator(split, ('[', ']'))?;
7676

7777
let size = split[array_range].to_string();
78-
array_size = match size.parse::<usize>() {
79-
Ok(size) => Some(size),
80-
Err(_) => None,
81-
};
78+
array_size = size.parse::<usize>().ok();
8279
}
8380
}
8481

@@ -180,10 +177,7 @@ pub fn to_type(string: &str) -> DynSolType {
180177

181178
let size = string[array_range].to_string();
182179

183-
array_size.push_back(match size.parse::<usize>() {
184-
Ok(size) => Some(size),
185-
Err(_) => None,
186-
});
180+
array_size.push_back(size.parse::<usize>().ok());
187181

188182
string = string.replacen(&format!("[{}]", &size), "", 1);
189183
}

crates/common/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
//! Common utilities, constants, and resources used across the Heimdall codebase.
2+
//!
3+
//! This crate provides shared functionality for the Heimdall toolkit, including
4+
//! Ethereum-related utilities, common resources, and general utility functions.
5+
6+
/// Constants used throughout the Heimdall codebase.
17
pub mod constants;
8+
9+
/// Utilities for interacting with Ethereum, including bytecode, calldata,
10+
/// and RPC functionality.
211
pub mod ether;
12+
13+
/// External resources and API integrations, such as OpenAI and Transpose.
314
pub mod resources;
15+
16+
/// General utility functions and types for common tasks.
417
pub mod utils;

crates/common/src/resources/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
/// OpenAI API integration for AI-powered analysis.
12
pub mod openai;
3+
4+
/// Transpose API integration for blockchain data access.
25
pub mod transpose;

crates/common/src/utils/env.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
/// Sets an environment variable if it's not already set.
2+
///
3+
/// # Arguments
4+
///
5+
/// * `key` - The environment variable name
6+
/// * `value` - The value to set
17
pub fn set_env(key: &str, value: &str) {
28
if std::env::var(key).is_err() {
39
std::env::set_var(key, value);
410
}
511
}
612

13+
/// Gets the value of an environment variable.
14+
///
15+
/// # Arguments
16+
///
17+
/// * `key` - The environment variable name to retrieve
18+
///
19+
/// # Returns
20+
///
21+
/// * `Option<String>` - The environment variable value if it exists
722
pub fn get_env(key: &str) -> Option<String> {
823
std::env::var(key).ok()
924
}

crates/common/src/utils/hex.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use super::strings::encode_hex;
22
use alloy::primitives::{Address, Bytes, FixedBytes, U256};
33

4-
/// A convenience function which encodes a given EVM type into a sized, lowercase hex string.
4+
/// A convenience trait which encodes a given EVM type into a sized, lowercase hex string.
55
pub trait ToLowerHex {
6+
/// Converts the value to a lowercase hexadecimal string representation.
7+
///
8+
/// # Returns
9+
///
10+
/// * `String` - The lowercase hexadecimal representation
611
fn to_lower_hex(&self) -> String;
712
}
813

crates/common/src/utils/integers.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
/// Trait for formatting numbers with locale-specific formatting.
2+
///
3+
/// This trait adds methods to format numbers in a more human-readable way,
4+
/// such as adding thousands separators.
15
pub trait ToLocaleString {
6+
/// Formats a number with locale-specific formatting.
7+
///
8+
/// For numbers, this adds commas as thousand separators.
9+
///
10+
/// # Returns
11+
///
12+
/// * `String` - The formatted string
213
fn to_locale_string(&self) -> String;
314
}
415

crates/common/src/utils/io/logging.rs

+32
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,43 @@ use super::super::strings::replace_last;
66
/// Has several helper functions to add different types of traces.
77
#[derive(Clone, Debug)]
88
pub struct TraceFactory {
9+
/// The level of the trace. Higher numbers mean more verbose output.
910
pub level: i8,
11+
/// The collection of traces gathered during execution.
1012
pub traces: Vec<Trace>,
1113
}
1214

1315
/// The trace category is used to determine how the trace is formatted.
1416
#[derive(Clone, Debug)]
1517
pub enum TraceCategory {
18+
/// Standard log message.
1619
Log,
20+
/// Log message with unknown source.
1721
LogUnknown,
22+
/// General message.
1823
Message,
24+
/// Function call trace.
1925
Call,
26+
/// Contract creation trace.
2027
Create,
28+
/// Empty trace (placeholder).
2129
Empty,
30+
/// Contract self-destruct trace.
2231
Suicide,
2332
}
2433

2534
/// Individual trace, which is added to the trace factory.
2635
#[derive(Clone, Debug)]
2736
pub struct Trace {
37+
/// The category of the trace, determining its formatting and interpretation.
2838
pub category: TraceCategory,
39+
/// The instruction number or identifier for this trace.
2940
pub instruction: u32,
41+
/// The message content of the trace, potentially multiple lines.
3042
pub message: Vec<String>,
43+
/// The parent trace identifier (if this is a child trace).
3144
pub parent: u32,
45+
/// Child trace identifiers that are nested under this trace.
3246
pub children: Vec<u32>,
3347
}
3448

@@ -246,6 +260,24 @@ impl TraceFactory {
246260
self.add("call", parent_index, instruction, vec![title, returns])
247261
}
248262

263+
/// Adds a function call trace with extra information.
264+
///
265+
/// This method creates a trace entry for a function call and includes additional context
266+
/// information.
267+
///
268+
/// # Arguments
269+
///
270+
/// * `parent_index` - The index of the parent trace
271+
/// * `instruction` - The instruction identifier
272+
/// * `origin` - The origin context (e.g., contract name)
273+
/// * `function_name` - The name of the function being called
274+
/// * `args` - The arguments passed to the function
275+
/// * `returns` - The return value(s) of the function
276+
/// * `extra` - Additional context information to display
277+
///
278+
/// # Returns
279+
///
280+
/// * `u32` - The index of the newly added trace
249281
#[allow(clippy::too_many_arguments)]
250282
pub fn add_call_with_extra(
251283
&mut self,

crates/common/src/utils/io/macros.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/// Creates a spinner with an INFO-level style for progress indicators.
2+
///
3+
/// This macro generates a progress style with a timestamp, an "INFO" label,
4+
/// and a spinning animation character that can be used with the indicatif
5+
/// crate's ProgressBar to show ongoing operations.
6+
///
7+
/// # Returns
8+
///
9+
/// * `ProgressStyle` - A styled progress indicator for info-level messages
110
#[macro_export]
211
macro_rules! info_spinner {
312
() => {
@@ -12,6 +21,15 @@ macro_rules! info_spinner {
1221
};
1322
}
1423

24+
/// Creates a spinner with a DEBUG-level style for progress indicators.
25+
///
26+
/// This macro generates a progress style with a timestamp, a "DEBUG" label,
27+
/// and a spinning animation character that can be used with the indicatif
28+
/// crate's ProgressBar to show ongoing operations at debug level.
29+
///
30+
/// # Returns
31+
///
32+
/// * `ProgressStyle` - A styled progress indicator for debug-level messages
1533
#[macro_export]
1634
macro_rules! debug_spinner {
1735
() => {

crates/common/src/utils/io/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
/// File system operations and utilities.
12
pub mod file;
3+
4+
/// Logging functionality and utilities.
25
pub mod logging;
6+
7+
/// Macros for input/output operations.
38
pub mod macros;
9+
10+
/// Types used for input/output operations.
411
pub mod types;

crates/common/src/utils/io/types.rs

+15
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,23 @@ pub fn display(inputs: Vec<DynSolValue>, prefix: &str) -> Vec<String> {
6767
output
6868
}
6969

70+
/// Trait for converting values to parameterized strings and type information.
71+
///
72+
/// This trait is used primarily for displaying and serializing function parameters
73+
/// in a readable format when presenting decoded contract data.
7074
pub trait Parameterize {
75+
/// Converts the value to a parameterized string representation.
76+
///
77+
/// # Returns
78+
///
79+
/// * `String` - The string representation of the parameter value
7180
fn parameterize(&self) -> String;
81+
82+
/// Returns the type name of the parameter as a string.
83+
///
84+
/// # Returns
85+
///
86+
/// * `String` - The type name (e.g., "uint256", "address", etc.)
7287
fn to_type(&self) -> String;
7388
}
7489

crates/common/src/utils/iter.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
/// Extension trait for byte slices that adds helpful operations.
12
pub trait ByteSliceExt {
3+
/// Splits a byte slice by a delimiter byte slice.
4+
///
5+
/// # Arguments
6+
///
7+
/// * `delimiter` - The byte sequence to split on
8+
///
9+
/// # Returns
10+
///
11+
/// * `Vec<&[u8]>` - The split parts
212
fn split_by_slice(&self, delimiter: &[u8]) -> Vec<&[u8]>;
13+
14+
/// Checks if a byte slice contains another byte slice.
15+
///
16+
/// # Arguments
17+
///
18+
/// * `sequence` - The byte sequence to search for
19+
///
20+
/// # Returns
21+
///
22+
/// * `bool` - `true` if the sequence is found, `false` otherwise
323
fn contains_slice(&self, sequence: &[u8]) -> bool;
424
}
525

@@ -36,6 +56,19 @@ impl ByteSliceExt for [u8] {
3656
}
3757
}
3858

59+
/// Removes elements at specified indices from a collection.
60+
///
61+
/// This function takes a collection and a sorted list of indices, and removes
62+
/// the elements at those indices from the collection.
63+
///
64+
/// # Arguments
65+
///
66+
/// * `v` - The collection to remove elements from
67+
/// * `indices` - A sorted list of indices to remove
68+
///
69+
/// # Returns
70+
///
71+
/// * `Vec<T>` - A new collection with the elements at the specified indices removed
3972
pub fn remove_sorted_indices<T>(
4073
v: impl IntoIterator<Item = T>,
4174
indices: impl IntoIterator<Item = usize>,

crates/common/src/utils/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
/// Environment variable utilities.
12
pub mod env;
3+
4+
/// Hexadecimal encoding and decoding utilities.
25
pub mod hex;
6+
7+
/// HTTP request and response handling utilities.
38
pub mod http;
9+
10+
/// Integer manipulation and formatting utilities.
411
pub mod integers;
12+
13+
/// Input/output utilities for file manipulation and logging.
514
pub mod io;
15+
16+
/// Iterator and collection utilities.
617
pub mod iter;
18+
19+
/// String manipulation and formatting utilities.
720
pub mod strings;
21+
22+
/// Synchronization primitives and utilities.
823
pub mod sync;
24+
25+
/// Threading and multi-threading utilities.
926
pub mod threading;
27+
28+
/// Time manipulation and formatting utilities.
1029
pub mod time;
30+
31+
/// Version handling and management utilities.
1132
pub mod version;

crates/common/src/utils/strings.rs

+16
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,17 @@ pub fn extract_condition(s: &str, keyword: &str) -> Option<String> {
271271
None
272272
}
273273

274+
/// Extension trait for strings that adds helpful operations.
274275
pub trait StringExt {
276+
/// Truncates a string to a maximum length, adding an ellipsis if necessary.
277+
///
278+
/// # Arguments
279+
///
280+
/// * `max_length` - The maximum length of the returned string
281+
///
282+
/// # Returns
283+
///
284+
/// * `String` - The truncated string with ellipsis if needed
275285
fn truncate(&self, max_length: usize) -> String;
276286
}
277287

@@ -365,11 +375,17 @@ pub fn tokenize(s: &str) -> Vec<String> {
365375
}
366376

367377
#[derive(Debug, PartialEq, Eq)]
378+
/// Classification for tokens in code analysis.
368379
pub enum TokenType {
380+
/// Control flow related tokens (if, while, for, etc).
369381
Control,
382+
/// Operators (+, -, *, /, etc).
370383
Operator,
384+
/// Constant values (numbers, string literals, etc).
371385
Constant,
386+
/// Variable identifiers.
372387
Variable,
388+
/// Function identifiers.
373389
Function,
374390
}
375391

crates/common/src/utils/sync.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ where
77
tokio::task::block_in_place(f)
88
}
99

10+
/// A boxed future with a static lifetime.
11+
///
12+
/// This type alias is a convenience for returning a boxed future from a function.
13+
/// The future is pinned and can be awaited.
1014
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;

0 commit comments

Comments
 (0)