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

Core Lib Documentation: fmt module #6652

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e5756a2
Array module
TAdev0 Oct 29, 2024
8290598
few additions
TAdev0 Oct 29, 2024
19e3322
fmt
TAdev0 Oct 29, 2024
7689558
fmt
TAdev0 Oct 29, 2024
f0d1476
last comment
TAdev0 Oct 29, 2024
1d5cf98
fmt
TAdev0 Oct 29, 2024
1095bd3
fmt
TAdev0 Oct 29, 2024
3c0db71
Update corelib/src/array.cairo
TAdev0 Oct 30, 2024
978fceb
Update corelib/src/array.cairo
TAdev0 Oct 30, 2024
010ca59
update
TAdev0 Oct 30, 2024
6fb6f83
Merge branch 'array' of https://github.com/cairo-book/cairo-core-lib …
TAdev0 Oct 30, 2024
5e6d5b9
address comments
TAdev0 Oct 30, 2024
a4281db
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
b661252
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
491a36e
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
0cb37c0
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
4ab4395
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
d93073d
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
8ca07d1
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
7d58c65
Update corelib/src/array.cairo
TAdev0 Nov 4, 2024
a56c766
typo
TAdev0 Nov 4, 2024
4467325
Merge branch 'main' of https://github.com/cairo-book/cairo-core-lib i…
TAdev0 Nov 4, 2024
5cfdcdf
address comments
TAdev0 Nov 4, 2024
2463e4a
more
TAdev0 Nov 4, 2024
ad4c1f8
address last comments
TAdev0 Nov 8, 2024
7ee6e1d
Merge remote-tracking branch 'upstream/main' into array
TAdev0 Nov 8, 2024
f1d15eb
address Orizi comments
TAdev0 Nov 11, 2024
6125b19
Merge pull request #1 from cairo-book/array
TAdev0 Nov 11, 2024
775954d
Merge remote-tracking branch 'upstream/main'
TAdev0 Nov 11, 2024
9f50b95
Merge remote-tracking branch 'upstream/main'
TAdev0 Nov 13, 2024
ae73123
fmt module
TAdev0 Nov 13, 2024
ba69884
fmt
TAdev0 Nov 13, 2024
08f767b
typo
TAdev0 Nov 13, 2024
0db093c
rename
TAdev0 Nov 13, 2024
5553d30
add internal link
TAdev0 Nov 13, 2024
15efec1
fmt
TAdev0 Nov 13, 2024
2cf8fcb
address Orizi comments
TAdev0 Nov 14, 2024
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
50 changes: 35 additions & 15 deletions corelib/src/fmt.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
//! Functionality for formatting values.
//!
//! The main components of this module are:
//!
//! - `Error`: A type representing formatting errors.
//! - `Formatter`: A struct that holds the configuration and buffer for formatting.
//! - `Display`: A trait for standard formatting using the empty format ("{}").
//! - `Debug`: A trait for debug formatting using the empty format ("{:?}").
//! - `LowerHex`: A trait for hex formatting in lower case.
//!
//! The module includes implementations of the [`Display`], [`Debug`] and [`LowerHex`] traits for
//! various types.

/// Dedicated type for representing formatting errors.
#[derive(Drop)]
pub struct Error {}

Expand All @@ -9,6 +23,13 @@ pub struct Formatter {
}

/// A trait for standard formatting, using the empty format ("{}").
///
/// # Examples
///
/// ```
/// let word: ByteArray = "123";
/// println!("{}", word);
/// ```
pub trait Display<T> {
fn fmt(self: @T, ref f: Formatter) -> Result<(), Error>;
}
Expand Down Expand Up @@ -72,6 +93,13 @@ impl DisplaySnapshot<T, +Display<T>> of Display<@T> {
}

/// A trait for debug formatting, using the empty format ("{:?}").
///
/// # Examples
///
/// ```
/// let word: ByteArray = "123";
/// println!("{:?}", word);
/// ```
pub trait Debug<T> {
fn fmt(self: @T, ref f: Formatter) -> Result<(), Error>;
}
Expand Down Expand Up @@ -125,7 +153,6 @@ impl DebugSnapshot<T, +Debug<T>> of Debug<@T> {
}
}

/// Tuple `Debug` implementation.
impl TupleDebug<
T,
impl TSF: crate::metaprogramming::TupleSnapForward<T>,
Expand All @@ -139,7 +166,6 @@ impl TupleDebug<
}
}

/// Fixed sized array `Debug` implementation.
impl FixedSizedArrayDebug<
T,
impl TSF: crate::metaprogramming::TupleSnapForward<T>,
Expand All @@ -158,21 +184,18 @@ trait TupleDebugHelper<T> {
fn fmt(value: T, ref f: Formatter) -> Result<(), Error>;
}

/// An implementation of `TupleDebugHelper` for snapshots of types with `Debug` implementations.
impl TupleDebugHelperFromDebug<T, +Debug<T>> of TupleDebugHelper<@T> {
fn fmt(value: @T, ref f: Formatter) -> Result<(), Error> {
Debug::fmt(value, ref f)
}
}

/// `Debug` impl for tuples of size 0.
impl TupleDebugHelperTuple0 of TupleDebugHelper<()> {
fn fmt(value: (), ref f: Formatter) -> Result<(), Error> {
Result::Ok(())
}
}

/// `Debug` impl for tuples of size 1.
impl TupleDebugHelperTuple1<E0, +TupleDebugHelper<@E0>> of TupleDebugHelper<(@E0,)> {
fn fmt(value: (@E0,), ref f: Formatter) -> Result<(), Error> {
let (e0,) = value;
Expand All @@ -181,7 +204,6 @@ impl TupleDebugHelperTuple1<E0, +TupleDebugHelper<@E0>> of TupleDebugHelper<(@E0
}
}

/// `Debug` impl for tuples of size 2.
impl TupleDebugHelperTuple2<
E0, E1, +TupleDebugHelper<@E0>, +TupleDebugHelper<@E1>,
> of TupleDebugHelper<(@E0, @E1)> {
Expand All @@ -193,8 +215,6 @@ impl TupleDebugHelperTuple2<
}
}

/// `Debug` impl for tuples of size 3 and above.
/// Not starting from size 1 since we have special cases for 0 and 1.
impl TupleDebugHelperTupleNext<
T,
impl TS: crate::metaprogramming::TupleSplit<T>,
Expand All @@ -211,23 +231,19 @@ impl TupleDebugHelperTupleNext<
}
}

/// `Debug` impl for fixed sized arrays of size 0.
impl TupleDebugHelperFixedSizedArray0<T> of TupleDebugHelper<[@T; 0]> {
fn fmt(value: [@T; 0], ref f: Formatter) -> Result<(), Error> {
Result::Ok(())
}
}

/// `Debug` impl for fixed sized arrays of size 1.
impl TupleDebugHelperFixedSizedArray1<T, +TupleDebugHelper<@T>> of TupleDebugHelper<[@T; 1]> {
fn fmt(value: [@T; 1], ref f: Formatter) -> Result<(), Error> {
let [e0] = value;
TupleDebugHelper::fmt(e0, ref f)
}
}

/// `Debug` impl for fixed sized arrays of size 2 and above.
/// Not starting from size 1 since we have a special case for 0.
impl TupleDebugHelperFixedSizedArrayNext<
T,
const N: usize,
Expand Down Expand Up @@ -289,10 +305,13 @@ impl SpanTDebug<T, +Debug<T>> of Debug<Span<T>> {
}
}

/// Impls for `Debug` and `LowerHex` for types that can be converted into `felt252` using the `Into`
/// Implementations for `Debug` and `LowerHex` for types that can be converted into `felt252` using
/// the `Into`
/// trait.
/// Usage example:
/// ```ignore
///
/// # Examples
///
/// ```
/// impl MyTypeDebug = crate::fmt::into_felt252_based::DebugImpl<MyType>;`
/// impl MyTypeLowerHex = crate::fmt::into_felt252_based::LowerHexImpl<MyType>;
/// ```
Expand All @@ -302,6 +321,7 @@ pub mod into_felt252_based {
crate::fmt::DebugInteger::<felt252>::fmt(@(*self).into(), ref f)
}
}

pub impl LowerHexImpl<T, +Into<T, felt252>, +Copy<T>> of core::fmt::LowerHex<T> {
fn fmt(self: @T, ref f: core::fmt::Formatter) -> Result<(), core::fmt::Error> {
core::fmt::LowerHexInteger::<felt252>::fmt(@(*self).into(), ref f)
Expand Down