Skip to content

Commit

Permalink
apply foundry's formatting config
Browse files Browse the repository at this point in the history
  • Loading branch information
sslivkoff committed Jul 26, 2023
1 parent 8dc8361 commit 2e79826
Show file tree
Hide file tree
Showing 38 changed files with 275 additions and 723 deletions.
17 changes: 4 additions & 13 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct Args {
// long,
// allow_hyphen_values(true),
// help_heading = "Content Options",
// help = "Select by data transaction instead of by block,\ncan be a list or a file, see syntax below",
// )]
// help = "Select by data transaction instead of by block,\ncan be a list or a file, see
// syntax below", )]
// pub txs: Vec<String>,
/// Columns to include alongside the default output,
/// use `all` to include all available columns
Expand Down Expand Up @@ -74,12 +74,7 @@ pub struct Args {
pub network_name: Option<String>,

/// Ratelimit on requests per second
#[arg(
short('l'),
long,
value_name = "limit",
help_heading = "Acquisition Options"
)]
#[arg(short('l'), long, value_name = "limit", help_heading = "Acquisition Options")]
pub requests_per_second: Option<u32>,

/// Global number of concurrent requests
Expand Down Expand Up @@ -146,11 +141,7 @@ pub struct Args {
pub contract: Option<String>,

/// [logs] filter logs by topic0
#[arg(
long,
visible_alias = "event",
help_heading = "Dataset-specific Options"
)]
#[arg(long, visible_alias = "event", help_heading = "Dataset-specific Options")]
pub topic0: Option<String>,

/// [logs] filter logs by topic1
Expand Down
9 changes: 2 additions & 7 deletions crates/cli/src/parse/args.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::sync::Arc;

use cryo_freeze::FileOutput;
use cryo_freeze::MultiQuery;
use cryo_freeze::ParseError;
use cryo_freeze::Source;
use cryo_freeze::{FileOutput, MultiQuery, ParseError, Source};

use crate::args::Args;

use super::file_output;
use super::query;
use super::source;
use super::{file_output, query, source};

/// parse options for running freeze
pub async fn parse_opts(args: &Args) -> Result<(MultiQuery, Source, FileOutput), ParseError> {
Expand Down
44 changes: 13 additions & 31 deletions crates/cli/src/parse/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use std::sync::Arc;

use ethers::prelude::*;

use cryo_freeze::BlockChunk;
use cryo_freeze::Chunk;
use cryo_freeze::ChunkData;
use cryo_freeze::ParseError;
use cryo_freeze::Subchunk;
use cryo_freeze::{BlockChunk, Chunk, ChunkData, ParseError, Subchunk};

use crate::args::Args;

Expand All @@ -16,10 +12,7 @@ pub(crate) async fn parse_blocks(
) -> Result<Vec<Chunk>, ParseError> {
let block_chunks = parse_block_inputs(&args.blocks, &provider).await?;
let block_chunks = if args.align {
block_chunks
.into_iter()
.filter_map(|x| x.align(args.chunk_size))
.collect()
block_chunks.into_iter().filter_map(|x| x.align(args.chunk_size)).collect()
} else {
block_chunks
};
Expand All @@ -28,10 +21,7 @@ pub(crate) async fn parse_blocks(
None => block_chunks.subchunk_by_size(&args.chunk_size),
};
let block_chunks = apply_reorg_buffer(block_chunks, args.reorg_buffer, &provider).await?;
let chunks: Vec<Chunk> = block_chunks
.iter()
.map(|x| Chunk::Block(x.clone()))
.collect();
let chunks: Vec<Chunk> = block_chunks.iter().map(|x| Chunk::Block(x.clone())).collect();
Ok(chunks)
}

Expand All @@ -45,9 +35,7 @@ async fn parse_block_inputs(
let first_input = inputs.get(0).ok_or_else(|| {
ParseError::ParseError("Failed to get the first input".to_string())
})?;
parse_block_token(first_input, true, provider)
.await
.map(|x| vec![x])
parse_block_token(first_input, true, provider).await.map(|x| vec![x])
}
_ => {
let mut chunks = Vec::new();
Expand Down Expand Up @@ -132,19 +120,15 @@ async fn parse_block_number(
provider: &Provider<Http>,
) -> Result<u64, ParseError> {
match (block_ref, range_position) {
("latest", _) => provider
.get_block_number()
.await
.map(|n| n.as_u64())
.map_err(|_e| {
ParseError::ParseError("Error retrieving latest block number".to_string())
}),
("latest", _) => provider.get_block_number().await.map(|n| n.as_u64()).map_err(|_e| {
ParseError::ParseError("Error retrieving latest block number".to_string())
}),
("", RangePosition::First) => Ok(0),
("", RangePosition::Last) => provider
.get_block_number()
.await
.map(|n| n.as_u64())
.map_err(|_e| ParseError::ParseError("Error retrieving last block number".to_string())),
("", RangePosition::Last) => {
provider.get_block_number().await.map(|n| n.as_u64()).map_err(|_e| {
ParseError::ParseError("Error retrieving last block number".to_string())
})
}
("", RangePosition::None) => Err(ParseError::ParseError("invalid input".to_string())),
_ if block_ref.ends_with('B') | block_ref.ends_with('b') => {
let s = &block_ref[..block_ref.len() - 1];
Expand Down Expand Up @@ -182,9 +166,7 @@ async fn apply_reorg_buffer(
let latest_block = match provider.get_block_number().await {
Ok(result) => result.as_u64(),
Err(_e) => {
return Err(ParseError::ParseError(
"reorg buffer parse error".to_string(),
))
return Err(ParseError::ParseError("reorg buffer parse error".to_string()))
}
};
let max_allowed = latest_block - reorg_filter;
Expand Down
52 changes: 15 additions & 37 deletions crates/cli/src/parse/file_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::fs;

use polars::prelude::*;

use cryo_freeze::FileFormat;
use cryo_freeze::FileOutput;
use cryo_freeze::ParseError;
use cryo_freeze::Source;
use cryo_freeze::{FileFormat, FileOutput, ParseError, Source};

use crate::args::Args;

Expand All @@ -19,12 +16,7 @@ pub(crate) fn parse_file_output(args: &Args, source: &Source) -> Result<FileOutp
.into_owned();
match fs::create_dir_all(&output_dir) {
Ok(_) => {}
Err(e) => {
return Err(ParseError::ParseError(format!(
"Error creating directory: {}",
e
)))
}
Err(e) => return Err(ParseError::ParseError(format!("Error creating directory: {}", e))),
};

let file_suffix = &args.file_suffix;
Expand Down Expand Up @@ -72,9 +64,9 @@ pub(crate) fn parse_network_name(args: &Args, chain_id: u64) -> String {

pub(crate) fn parse_output_format(args: &Args) -> Result<FileFormat, ParseError> {
match (args.csv, args.json) {
(true, true) => Err(ParseError::ParseError(
"choose one of parquet, csv, or json".to_string(),
)),
(true, true) => {
Err(ParseError::ParseError("choose one of parquet, csv, or json".to_string()))
}
(true, _) => Ok(FileFormat::Csv),
(_, true) => Ok(FileFormat::Json),
(false, false) => Ok(FileFormat::Parquet),
Expand All @@ -90,44 +82,30 @@ fn parse_compression(input: &Vec<String>) -> Result<ParquetCompression, ParseErr
[algorithm, level_str] if algorithm.as_str() == "gzip" => match level_str.parse::<u8>() {
Ok(level) => match GzipLevel::try_new(level) {
Ok(gzip_level) => Ok(ParquetCompression::Gzip(Some(gzip_level))),
Err(_) => Err(ParseError::ParseError(
"Invalid compression level".to_string(),
)),
Err(_) => Err(ParseError::ParseError("Invalid compression level".to_string())),
},
Err(_) => Err(ParseError::ParseError(
"Invalid compression level".to_string(),
)),
Err(_) => Err(ParseError::ParseError("Invalid compression level".to_string())),
},
[algorithm, level_str] if algorithm.as_str() == "brotli" => {
match level_str.parse::<u32>() {
Ok(level) => match BrotliLevel::try_new(level) {
Ok(brotli_level) => Ok(ParquetCompression::Brotli(Some(brotli_level))),
Err(_) => Err(ParseError::ParseError(
"Invalid compression level".to_string(),
)),
Err(_) => Err(ParseError::ParseError("Invalid compression level".to_string())),
},
Err(_) => Err(ParseError::ParseError(
"Invalid compression level".to_string(),
)),
Err(_) => Err(ParseError::ParseError("Invalid compression level".to_string())),
}
}
[algorithm, level_str] if algorithm.as_str() == "zstd" => match level_str.parse::<i32>() {
Ok(level) => match ZstdLevel::try_new(level) {
Ok(zstd_level) => Ok(ParquetCompression::Zstd(Some(zstd_level))),
Err(_) => Err(ParseError::ParseError(
"Invalid compression level".to_string(),
)),
Err(_) => Err(ParseError::ParseError("Invalid compression level".to_string())),
},
Err(_) => Err(ParseError::ParseError(
"Invalid compression level".to_string(),
)),
Err(_) => Err(ParseError::ParseError("Invalid compression level".to_string())),
},
[algorithm] if ["gzip", "brotli", "zstd"].contains(&algorithm.as_str()) => Err(
ParseError::ParseError("Missing compression level".to_string()),
),
_ => Err(ParseError::ParseError(
"Invalid compression algorithm".to_string(),
)),
[algorithm] if ["gzip", "brotli", "zstd"].contains(&algorithm.as_str()) => {
Err(ParseError::ParseError("Missing compression level".to_string()))
}
_ => Err(ParseError::ParseError("Invalid compression algorithm".to_string())),
}
}

Expand Down
40 changes: 10 additions & 30 deletions crates/cli/src/parse/query.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use ethers::prelude::*;
use hex::FromHex;

use cryo_freeze::ColumnEncoding;
use cryo_freeze::Datatype;
use cryo_freeze::FileFormat;
use cryo_freeze::MultiQuery;
use cryo_freeze::ParseError;
use cryo_freeze::RowFilter;
use cryo_freeze::Table;
use cryo_freeze::{ColumnEncoding, Datatype, FileFormat, MultiQuery, ParseError, RowFilter, Table};

use super::blocks;
use super::file_output;
use super::{blocks, file_output};
use crate::args::Args;

pub(crate) async fn parse_query(
Expand All @@ -33,18 +25,11 @@ pub(crate) async fn parse_query(
parse_topic(&args.topic2),
parse_topic(&args.topic3),
];
let row_filter = RowFilter {
address: contract,
topics,
};
let row_filter = RowFilter { address: contract, topics };
let mut row_filters: HashMap<Datatype, RowFilter> = HashMap::new();
row_filters.insert(Datatype::Logs, row_filter);

let query = MultiQuery {
schemas,
chunks,
row_filters,
};
let query = MultiQuery { schemas, chunks, row_filters };
Ok(query)
}

Expand Down Expand Up @@ -74,10 +59,7 @@ fn parse_datatypes(raw_inputs: &Vec<String>) -> Result<Vec<Datatype>, ParseError
"vm_traces" => Datatype::VmTraces,
"opcode_traces" => Datatype::VmTraces,
_ => {
return Err(ParseError::ParseError(format!(
"invalid datatype {}",
datatype
)))
return Err(ParseError::ParseError(format!("invalid datatype {}", datatype)))
}
};
datatypes.push(datatype)
Expand Down Expand Up @@ -124,14 +106,12 @@ fn parse_sort(
datatypes: &Vec<Datatype>,
) -> Result<HashMap<Datatype, Option<Vec<String>>>, ParseError> {
match raw_sort {
None => Ok(HashMap::from_iter(datatypes.iter().map(|datatype| {
(*datatype, Some(datatype.dataset().default_sort()))
}))),
None => Ok(HashMap::from_iter(
datatypes.iter().map(|datatype| (*datatype, Some(datatype.dataset().default_sort()))),
)),
Some(raw_sort) => {
if (raw_sort.len() == 1) && (raw_sort[0] == "none") {
Ok(HashMap::from_iter(
datatypes.iter().map(|datatype| (*datatype, None)),
))
Ok(HashMap::from_iter(datatypes.iter().map(|datatype| (*datatype, None))))
} else if raw_sort.is_empty() {
Err(ParseError::ParseError(
"must specify columns to sort by, use `none` to disable sorting".to_string(),
Expand Down
6 changes: 2 additions & 4 deletions crates/cli/src/parse/source.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::env;

use ethers::prelude::*;
use governor::Quota;
use governor::RateLimiter;
use governor::{Quota, RateLimiter};
use polars::prelude::*;
use std::num::NonZeroU32;

use cryo_freeze::ParseError;
use cryo_freeze::Source;
use cryo_freeze::{ParseError, Source};

use crate::args::Args;

Expand Down
9 changes: 3 additions & 6 deletions crates/cli/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::time::SystemTime;

use crate::args;
use crate::parse;
use crate::summaries;
use cryo_freeze::FreezeError;
use cryo_freeze::FreezeSummary;
use crate::{args, parse, summaries};
use cryo_freeze::{FreezeError, FreezeSummary};

/// run freeze for given Args
pub async fn run(args: args::Args) -> Result<Option<FreezeSummary>, FreezeError> {
Expand Down Expand Up @@ -35,7 +32,7 @@ pub async fn run(args: args::Args) -> Result<Option<FreezeSummary>, FreezeError>
}
Err(e) => {
println!("{}", e);
return Err(e);
return Err(e)
}
}
};
Expand Down
Loading

0 comments on commit 2e79826

Please sign in to comment.