Skip to content

Commit

Permalink
add fmt crate
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Apr 14, 2024
1 parent 0634b18 commit c7a17c1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
52 changes: 51 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"bin",
"crates/args",
"crates/context-macro",
"crates/fmt",
"crates/ops",
"crates/rika",
"crates/waiter",
Expand All @@ -20,6 +21,7 @@ version = "0.1.0"

[workspace.dependencies]
rika-args = { path = "crates/args" }
rika-fmt = { path = "crates/fmt" }
rika-ops = { path = "crates/ops" }

chrono = "0.4.23"
Expand Down
14 changes: 14 additions & 0 deletions crates/fmt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
authors.workspace = true
description.workspace = true
edition.workspace = true
name = "rika-fmt"
repository.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono.workspace = true
prettytable-rs = "0.10.0"
starknet.workspace = true
52 changes: 52 additions & 0 deletions crates/fmt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::vec;

use chrono::{Local, TimeZone};
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::Table;
use starknet::core::types::{
DataAvailabilityMode, DeclareTransaction, DeployAccountTransaction, FieldElement,
MaybePendingBlockWithTxHashes, TransactionExecutionStatus, TransactionStatus,
};
use starknet::core::types::{
Event, InvokeTransaction, MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, MsgToL1,
Transaction, TransactionReceipt,
};

/// Display trait for pretty printing
pub trait Pretty {
fn prettify(&self) -> String;
}

impl<T: Tabular> Pretty for T {
fn prettify(&self) -> String {
self.tablify().to_string()
}
}

/// Display trait for types that can be tabulated
pub trait Tabular {
/// Convert the type to a prettytable::Table
fn tablify(&self) -> Table;

/// Get the default table format
fn with_default_table() -> Table {
let mut table = Table::new();
table.set_format(*FORMAT_NO_LINESEP_WITH_TITLE);
table
}
}

/// Macro for implementing the [Pretty] trait for types implement [LowerHex](std::fmt::LowerHex) trait.
macro_rules! pretty_for_lower_hex {
($($name:ty),*) => {
$(
impl Pretty for $name {
fn prettify(&self) -> String {
format!("{self:#x}")
}
}
)*
};
}

pretty_for_lower_hex!(FieldElement, u64);

0 comments on commit c7a17c1

Please sign in to comment.