Skip to content

Commit

Permalink
add docs and hide low-level functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz committed Sep 3, 2024
1 parent eb44994 commit 94d882a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
3 changes: 3 additions & 0 deletions rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ serde_repr = "0.1.19"
name = "nested_lists"
harness = false

[features]
fuzzing = []

[profile.bench]
debug = true
54 changes: 29 additions & 25 deletions rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ pub use de::from_bytes;
mod ser;
pub use ser::to_bytes;

#[doc(hidden)]
/// This low-level function is used to convert an rlp representation into bytes
pub fn pack_rlp(mut rlp: Rlp) -> Result<Vec<u8>, RlpError> {
let mut pack = Vec::new();
while let Some(rec) = rlp.pop_front() {
recursive_pack_rlp(&mut pack, rec)?;
}
Ok(pack)
}

#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug, Clone)]
#[doc(hidden)]
/// Convert
pub enum RecursiveBytes {
/// Bytes (string)
Bytes(Vec<u8>),
/// An empty list that should serialize to 0x80
EmptyList,
/// A nested data structure to represent arbitrarily arbitrarily nested arrays (list)
Nested(Vec<RecursiveBytes>),
}

#[doc(hidden)]
/// This low-level function is used to convert bytes into an rlp representation
pub fn unpack_rlp(bytes: &[u8]) -> Result<Rlp, RlpError> {
Ok(Rlp::new(unpack_rlp_element(bytes, 0)?.into()))
}

#[derive(Debug)]
pub enum RlpError {
MissingBytes,
Expand Down Expand Up @@ -46,17 +75,6 @@ impl Display for RlpError {

impl std::error::Error for RlpError {}

#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug, Clone)]
pub enum RecursiveBytes {
/// Bytes (string)
Bytes(Vec<u8>),
/// An empty list that should serialize to 0x80
EmptyList,
/// A nested data structure to represent arbitrarily arbitrarily nested arrays (list)
Nested(Vec<RecursiveBytes>),
}

impl RecursiveBytes {
#[cfg(test)]
fn empty_list() -> Self {
Expand Down Expand Up @@ -234,11 +252,6 @@ fn unpack_rlp_element(bytes: &[u8], mut cursor: usize) -> Result<Vec<RecursiveBy
Ok(unpacked)
}

// TODO pub(crate)
pub fn unpack_rlp(bytes: &[u8]) -> Result<Rlp, RlpError> {
Ok(Rlp::new(unpack_rlp_element(bytes, 0)?.into()))
}

fn parse_num<const N: usize>(bytes: [u8; N]) -> Option<Vec<u8>> {
bytes
.iter()
Expand Down Expand Up @@ -314,15 +327,6 @@ fn recursive_pack_rlp(pack: &mut Vec<u8>, rec: RecursiveBytes) -> Result<usize,
}
}

// TODO pub(crate)
pub fn pack_rlp(mut rlp: Rlp) -> Result<Vec<u8>, RlpError> {
let mut pack = Vec::new();
while let Some(rec) = rlp.pop_front() {
recursive_pack_rlp(&mut pack, rec)?;
}
Ok(pack)
}

// https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/#examples
#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 94d882a

Please sign in to comment.