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

feat: formatter utils #224

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ jobs:
- name: Step 1 - Check out main branch
uses: actions/checkout@v3
- name: Step 2 - Getting scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.4.0-rc2
uses: software-mansion/setup-scarb@v1.3.2
- name: Step 3 - Trying to build
run: scarb build

Expand All @@ -24,9 +22,7 @@ jobs:
- name: Step 1 - Check out main branch
uses: actions/checkout@v3
- name: Step 2 - Getting scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.4.0-rc2
uses: software-mansion/setup-scarb@v1.3.2
- name: Step 3 - Running tests
run: scarb test

Expand All @@ -38,8 +34,6 @@ jobs:
- name: Step 1 - Check out main branch
uses: actions/checkout@v3
- name: Step 2 - Getting scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.4.0-rc2
uses: software-mansion/setup-scarb@v1.3.2
- name: Step 3 - Checking formatting
run: scarb fmt --check
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.4.0-rc2
scarb 2.4.0
4 changes: 4 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ version = "0.1.0"
[[package]]
name = "alexandria_storage"
version = "0.3.0"

[[package]]
name = "alexandria_utils"
version = "0.1.0"
3 changes: 2 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ members = [
"src/storage",
"src/ascii",
"src/bytes",
"src/utils",
]
name = "alexandria"
version = "0.1.0"
description = "Community maintained Cairo and Starknet libraries"
homepage = "https://github.com/keep-starknet-strange/alexandria/"

[workspace.dependencies]
starknet = ">=2.4.0-rc2"
starknet = ">=2.4.0"

[workspace.tool.fmt]
sort-module-level-items = true
Expand Down
3 changes: 3 additions & 0 deletions src/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Utils

A set of utilities.
11 changes: 11 additions & 0 deletions src/utils/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "alexandria_utils"
version = "0.1.0"
description = "Cairo utilities"
homepage = "https://github.com/keep-starknet-strange/alexandria/tree/src/utils"

[tool]
fmt.workspace = true

[dependencies]
starknet.workspace = true
67 changes: 67 additions & 0 deletions src/utils/src/fmt.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use core::fmt::{Display, Debug, Formatter, Error};
use starknet::{ContractAddress, EthAddress, ClassHash, StorageAddress, StorageBaseAddress};


/// Display
mod display_felt252_based {
use core::fmt::{Display, Formatter, Error};
use core::to_byte_array::AppendFormattedToByteArray;
impl TDisplay<T, +Into<T, felt252>, +Copy<T>> of Display<T> {
fn fmt(self: @T, ref f: Formatter) -> Result<(), Error> {
let value: felt252 = (*self).into();
Display::<u256>::fmt(@value.into(), ref f)
}
}
}

impl EthAddressDisplay = display_felt252_based::TDisplay<EthAddress>;
impl ContractAddressDisplay = display_felt252_based::TDisplay<ContractAddress>;
impl ClassHashDisplay = display_felt252_based::TDisplay<ClassHash>;
impl StorageAddressDisplay = display_felt252_based::TDisplay<StorageAddress>;

/// Debug
mod debug_display_based {
use core::fmt::{Display, Debug, Formatter, Error};
use core::to_byte_array::AppendFormattedToByteArray;
impl TDebug<T, +Display<T>> of Debug<T> {
fn fmt(self: @T, ref f: Formatter) -> Result<(), Error> {
Display::fmt(self, ref f)
}
}
}


impl EthAddressDebug = debug_display_based::TDebug<EthAddress>;
impl ContractAddressDebug = debug_display_based::TDebug<ContractAddress>;
impl ClassHashDebug = debug_display_based::TDebug<ClassHash>;
impl StorageAddressDebug = debug_display_based::TDebug<StorageAddress>;

impl ArrayTDebug<T, +Display<T>, +Copy<T>> of Debug<Array<T>> {
fn fmt(self: @Array<T>, ref f: Formatter) -> Result<(), Error> {
Debug::fmt(@self.span(), ref f)
}
}

impl SpanTDebug<T, +Display<T>, +Copy<T>> of Debug<Span<T>> {
fn fmt(self: @Span<T>, ref f: Formatter) -> Result<(), Error> {
let mut self = *self;
write!(f, "[")?;
loop {
match self.pop_front() {
Option::Some(value) => {
if Display::fmt(value, ref f).is_err() {
break Result::Err(Error {});
};
if self.len() == 0 {
break Result::Ok(());
}
if write!(f, ", ").is_err() {
break Result::Err(Error {});
};
},
Option::None => { break Result::Ok(()); }
};
}?;
write!(f, "]")
}
}
1 change: 1 addition & 0 deletions src/utils/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod fmt;