Skip to content

Commit

Permalink
Evolve format tools (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen authored Aug 19, 2024
1 parent 888a5e6 commit c30378c
Show file tree
Hide file tree
Showing 32 changed files with 1,999 additions and 576 deletions.
61 changes: 59 additions & 2 deletions module/core/format_tools/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ pub( crate ) mod private
{{
(
::core::stringify!( $key ),
$crate::MaybeAs::< 'a, str, $how >::from
$crate::MaybeAs::< '_, str, $how >::from
(
$crate::to_string_with_fallback!( $how, $fallback1, $fallback2, $src )
),
)
}};
}


/// Macro to create a field with optional fallbacks.
///
/// This macro helps to convert a field of a structure into one or another string representation
Expand Down Expand Up @@ -105,6 +104,55 @@ pub( crate ) mod private

}

/// Converting representations to a reference on a string slice,
/// but if not possible, to a display string, and if that is also not possible, then to a debug string.
///
/// Macros for converting fields to different string representations in a prioritized manner:
/// 1. Reference to a string slice.
/// 2. Display string.
/// 3. Debug string with miltiline.
pub mod ref_or_display_or_debug_multiline
{

/// Macro to create a field with key using reference, display, or debug formatting.
///
/// This macro attempts to convert the field to a reference to a string slice.
/// If that is not possible, it tries to use the Display trait for conversion.
/// If that also fails, it falls back to using the Debug trait with multiline.
#[ macro_export ]
macro_rules! ref_or_display_or_debug_multiline_field_with_key
{
(
$key : ident,
$src : expr
$(,)?
)
=>
{{
$crate::_field_with_key!( $key, $src, $crate::WithRef, $crate::WithDisplay, $crate::WithDebugMultiline )
}};
}

/// Macro to create a field using reference, display, or debug formatting.
///
/// This macro attempts to convert the field to a reference to a string slice.
/// If that is not possible, it tries to use the Display trait for conversion.
/// If that also fails, it falls back to using the Debug trait with multiline.
#[ macro_export ]
macro_rules! ref_or_display_or_debug_multiline_field
{
( $( $t:tt )+ )
=>
{{
$crate::_field!( $( $t )+, $crate::WithRef, $crate::WithDisplay, $crate::WithDebugMultiline )
}}
}

pub use ref_or_display_or_debug_multiline_field_with_key as field_with_key;
pub use ref_or_display_or_debug_multiline_field as field;

}

/// Converting representations to a reference on a string slice,
/// but if not possible, to a display string, and if that is also not possible, then to a debug string.
///
Expand Down Expand Up @@ -206,7 +254,9 @@ pub( crate ) mod private
pub mod to_string;
pub mod to_string_with_fallback;
pub mod as_table;
pub mod md_math;
pub mod print;
pub mod string;
pub mod table;

#[ doc( inline ) ]
Expand All @@ -225,7 +275,9 @@ pub mod own
to_string::orphan::*,
to_string_with_fallback::orphan::*,
as_table::orphan::*,
md_math::orphan::*,
print::orphan::*,
string::orphan::*,
table::orphan::*,
};

Expand All @@ -244,6 +296,7 @@ pub mod orphan
pub use private::
{
ref_or_display_or_debug,
ref_or_display_or_debug_multiline,
ref_or_debug,
};

Expand All @@ -264,7 +317,9 @@ pub mod exposed
to_string::exposed::*,
to_string_with_fallback::exposed::*,
as_table::exposed::*,
md_math::exposed::*,
print::exposed::*,
string::exposed::*,
table::exposed::*,
};

Expand All @@ -282,7 +337,9 @@ pub mod prelude
to_string::prelude::*,
to_string_with_fallback::prelude::*,
as_table::prelude::*,
md_math::prelude::*,
print::prelude::*,
string::prelude::*,
table::prelude::*,
};

Expand Down
183 changes: 134 additions & 49 deletions module/core/format_tools/src/format/as_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,103 +7,187 @@ pub( crate ) mod private
{

use crate::*;
use core::ops::{ Deref };
use core::marker::PhantomData;
use core::fmt;
use core::
{
ops::{ Deref },
marker::PhantomData,
fmt,
};

/// Transparent wrapper for table-like structures.
/// Transparent wrapper for interpreting data as a table.
///
/// `AsTable` provides a reference-based wrapper for table-like structures,
/// encapsulating type information needed to interpret data as a table.
///
#[ repr( transparent ) ]
#[ derive( Clone, Copy ) ]
pub struct AsTable< 'a, T, RowKey, Row, CellKey, Cell, Title >
pub struct AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
(
&'a T,
::core::marker::PhantomData< ( &'a (), fn () -> ( RowKey, Row, CellKey, Box< Cell >, Title ) ) >,
&'table Table,
::core::marker::PhantomData
<(
&'table (),
fn() -> ( &'table RowKey, Row, &'table CellKey, CellRepr ),
)>,
)
where
Row : Clone + for< 'cell > Cells< 'cell, CellKey, Cell, () > + 'a,
Title : fmt::Debug,
Cell : fmt::Debug + 'a,
Cell : std::borrow::ToOwned + ?Sized,
CellKey : fmt::Debug + Clone,
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr
;

impl< 'a, T, RowKey, Row, CellKey, Cell, Title > AsTable< 'a, T, RowKey, Row, CellKey, Cell, Title >
impl< 'table, Table, RowKey, Row, CellKey, CellRepr >
AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
where
Row : Clone + for< 'cell > Cells< 'cell, CellKey, Cell, () > + 'a,
Title : fmt::Debug,
Cell : fmt::Debug + 'a,
Cell : std::borrow::ToOwned + ?Sized,
CellKey : fmt::Debug + Clone,
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr,
{
/// Just a constructor.
pub fn new( src : &'a T ) -> Self
pub fn new( src : &'table Table ) -> Self
{
Self( src, Default::default() )
}
}

impl< 'a, T, RowKey, Row, CellKey, Cell, Title > AsRef< T > for AsTable< 'a, T, RowKey, Row, CellKey, Cell, Title >
impl< 'table, Table, RowKey, Row, CellKey, CellRepr > AsRef< Table >
for AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
where
Row : Clone + for< 'cell > Cells< 'cell, CellKey, Cell, () > + 'a,
Title : fmt::Debug,
Cell : fmt::Debug + 'a,
Cell : std::borrow::ToOwned + ?Sized,
CellKey : fmt::Debug + Clone,
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr,
{
fn as_ref( &self ) -> &T
fn as_ref( &self ) -> &Table
{
&self.0
}
}

impl< 'a, T, RowKey, Row, CellKey, Cell, Title > Deref for AsTable< 'a, T, RowKey, Row, CellKey, Cell, Title >
impl< 'table, Table, RowKey, Row, CellKey, CellRepr > Deref
for AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
where
Row : Clone + for< 'cell > Cells< 'cell, CellKey, Cell, () > + 'a,
Title : fmt::Debug,
Cell : fmt::Debug + 'a,
Cell : std::borrow::ToOwned + ?Sized,
CellKey : fmt::Debug + Clone,
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr,
{
type Target = T;
type Target = Table;

fn deref( &self ) -> &Self::Target
{
&self.0
}
}

impl< 'a, T, RowKey, Row, CellKey, Cell, Title > From< &'a T >
for AsTable< 'a, T, RowKey, Row, CellKey, Cell, Title >
impl< 'table, Table, RowKey, Row, CellKey, CellRepr > From< &'table Table >
for AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
where
Row : Clone + for< 'cell > Cells< 'cell, CellKey, Cell, () > + 'a,
Title : fmt::Debug,
Cell : fmt::Debug + 'a,
Cell : std::borrow::ToOwned + ?Sized,
CellKey : fmt::Debug + Clone,
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr,
{
fn from( table : &'a T ) -> Self
fn from( table : &'table Table ) -> Self
{
AsTable( table, PhantomData )
}
}

impl< 'a, T, RowKey, Row, CellKey, Cell, Title > fmt::Debug for AsTable< 'a, T, RowKey, Row, CellKey, Cell, Title >
impl< 'table, Table, RowKey, Row, CellKey, CellRepr > fmt::Debug
for AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
where
T : fmt::Debug,
Row : Clone + for< 'cell > Cells< 'cell, CellKey, Cell, () > + 'a,
Title : fmt::Debug,
Cell : std::borrow::ToOwned + ?Sized,
Cell : fmt::Debug + 'a,
CellKey : fmt::Debug + Clone,
Table : fmt::Debug,
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr,
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
f.debug_struct( "AsTable" )
f
.debug_struct( "AsTable" )
.field( "0", &self.0 )
.finish()
}
}

// =

/// Trait for converting data references into `AsTable` references.
///
/// `IntoAsTable` provides a way to interpret data as a table, encapsulating
/// the necessary type information for handling table-like structures.
///
pub trait IntoAsTable
{
/// The type representing the table.
type Table;

/// The type used to identify each row.
type RowKey : table::RowKey;

/// The type representing a row, must implement `Cells`.
type Row : Cells< Self::CellKey, Self::CellRepr >;

/// The type used to identify cells within a row, must implement `Key` and can be unsized.
type CellKey : table::CellKey + ?Sized;

/// The type representing the content of a cell, must implement `CellRepr`.
type CellRepr : table::CellRepr;

/// Converts the data reference into an `AsTable` reference.
fn as_table( &self ) -> AsTable< '_, Self::Table, Self::RowKey, Self::Row, Self::CellKey, Self::CellRepr >;
}

impl< 'table, Table, RowKey, Row, CellKey, CellRepr > IntoAsTable
for AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >
where
RowKey : table::RowKey,
Row : Cells< CellKey, CellRepr >,
CellKey : table::CellKey + ?Sized,
CellRepr : table::CellRepr,
Self : Copy,
{

type Table = Table;
type RowKey = RowKey;
type Row = Row;
type CellKey = CellKey;
type CellRepr = CellRepr;

fn as_table( &self ) -> AsTable< '_, Self::Table, Self::RowKey, Self::Row, Self::CellKey, Self::CellRepr >
{
*self
}

}

// impl< Row > IntoAsTable
// for Vec< Row >
// where
// Row : Cells< Self::CellKey, Self::CellRepr >,
// // CellKey : table::CellKey + ?Sized,
// // CellRepr : table::CellRepr,
// {
//
// type Table = Self;
// type RowKey = usize;
// type Row = Row;
// type CellKey = str;
// type CellRepr = WithRef;
//
// fn as_table( &self ) -> AsTable< '_, Self::Table, Self::RowKey, Self::Row, Self::CellKey, Self::CellRepr >
// {
// AsTable::from( self )
// }
//
// }

// pub struct AsTable< 'table, Table, RowKey, Row, CellKey, CellRepr >

}

#[ allow( unused_imports ) ]
Expand Down Expand Up @@ -138,6 +222,7 @@ pub mod exposed
pub use private::
{
AsTable,
IntoAsTable,
};

}
Expand Down
Loading

0 comments on commit c30378c

Please sign in to comment.