Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/remove common::parsing #284

Closed
0xaatif opened this issue Jun 5, 2024 · 2 comments
Closed

refactor/remove common::parsing #284

0xaatif opened this issue Jun 5, 2024 · 2 comments
Labels
crate: zero_bin Anything related to the zero-bin subcrates.

Comments

@0xaatif
Copy link
Contributor

0xaatif commented Jun 5, 2024

I don't think we should be writing a parsing library like this when nom or winnow exist

Originally posted by @0xaatif in 0xPolygonZero/zero-bin#90 (comment)


Imagine if 0xPolygonZero/zero-bin#90 looked like this:

use alloy_primitives::{FixedBytes, B256};
use winnow::{ascii::dec_uint, combinator::{alt, preceded, terminated}, error::ContextError, token::take, PResult, Parser};

pub enum BlockSlice {
    Just(BlockId),
    Range(BlockId, BlockId),
    From(BlockId),
}

pub enum BlockId {
    Hash(B256),
    Number(u64),
}

pub fn block_slice(input: &mut &str) -> PResult<BlockSlice> {
    alt((
        range.map(|(a, b)| BlockSlice::Range(a, b)),
        from.map(BlockSlice::From),
        block_id.map(BlockSlice::Just),
    ))
    .parse_next(input)
}

fn from(input: &mut &str) -> PResult<BlockId> {
    terminated(block_id, "..").parse_next(input)
}

fn range(input: &mut &str) -> PResult<(BlockId, BlockId)> {
    (block_id, preceded("..", block_id)).parse_next(input)
}

pub fn block_id(input: &mut &str) -> PResult<BlockId> {
    alt((b256.map(BlockId::Hash), dec_uint.map(BlockId::Number))).parse_next(input)
}

pub fn b256(input: &mut &str) -> PResult<B256> {
    preceded("0x", fixed_bytes).parse_next(input)
}

fn fixed_bytes<const N: usize>(input: &mut &str) -> PResult<FixedBytes<N>> {
    take(N * 2).try_map(FixedBytes::from_str).parse_next(input)
}

Instead of this:
https://github.com/0xPolygonZero/zero-bin/blob/c090919cadba56a09f10dcaf975527426ed1ec78/common/src/block_interval.rs#L62-L101
https://github.com/0xPolygonZero/zero-bin/blob/c090919cadba56a09f10dcaf975527426ed1ec78/common/src/parsing.rs#L34-L90

@0xaatif
Copy link
Contributor Author

0xaatif commented Jun 5, 2024

I'll implement the above at some point, happy for someone to beat me to the punch with Co-Authored-By

@Nashtare Nashtare transferred this issue from 0xPolygonZero/zero-bin Jun 18, 2024
@Nashtare Nashtare added the crate: zero_bin Anything related to the zero-bin subcrates. label Jun 18, 2024
@Nashtare Nashtare modified the milestone: Cleanups and Misc. Jun 21, 2024
@Nashtare Nashtare modified the milestones: Misc., System strengthening Jul 12, 2024
@0xaatif
Copy link
Contributor Author

0xaatif commented Nov 5, 2024

#728 removed most of this code :)

@0xaatif 0xaatif closed this as completed Nov 5, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in Zero EVM Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate: zero_bin Anything related to the zero-bin subcrates.
Projects
Status: Done
Development

No branches or pull requests

2 participants