Skip to content

Commit

Permalink
fixing format_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Oct 12, 2024
1 parent e67220c commit 8d66c4f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
2 changes: 2 additions & 0 deletions module/core/format_tools/src/format/output_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod private

mod table;
mod records;
mod keys;

#[ allow( unused_imports ) ]
pub use own::*;
Expand All @@ -99,6 +100,7 @@ pub mod own
{
table::Table,
records::Records,
keys::Keys,
};

#[ doc( inline ) ]
Expand Down
107 changes: 107 additions & 0 deletions module/core/format_tools/src/format/output_format/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! Implement keys list output format.
//!
//! # Example
//!
//! ```text
//! ```
//!
use crate::*;
use print::
{
InputExtract,
Context,
};
use core::
{
fmt,
};
use std::sync::OnceLock;

/// A struct representing the list of keys output format.
#[derive( Debug )]
pub struct Keys
{
// /// Prefix added to each row.
// pub table_prefix : String,
// /// Postfix added to each row.
// pub table_postfix : String,
// /// Separator used between rows.
// pub table_separator : String,
// /// Prefix added to each row.
// pub row_prefix : String,
// /// Postfix added to each row.
// pub row_postfix : String,
// /// Separator used between rows.
// pub row_separator : String,
// /// Prefix added to each cell.
// pub cell_prefix : String,
// /// Postfix added to each cell.
// pub cell_postfix : String,
// /// Separator used between table columns.
// pub cell_separator : String,
}

impl Keys
{
/// Returns a reference to a static instance of `Keys`.
pub fn instance() -> &'static dyn TableOutputFormat
{
static INSTANCE : OnceLock< Keys > = OnceLock::new();
INSTANCE.get_or_init( || Keys::default() )
}
}

impl Default for Keys
{
fn default() -> Self
{

// let cell_prefix = "".to_string();
// let cell_postfix = "".to_string();
// let cell_separator = " │ ".to_string();
// let row_prefix = "│ ".to_string();
// let row_postfix = " │".to_string();
// let row_separator = "\n".to_string();
// let table_prefix = "".to_string();
// let table_postfix = "".to_string();
// let table_separator = "\n".to_string();

Self
{
// table_prefix,
// table_postfix,
// table_separator,
// row_prefix,
// row_postfix,
// row_separator,
// cell_prefix,
// cell_postfix,
// cell_separator,
}
}
}

impl TableOutputFormat for Keys
{

fn extract_write< 'buf, 'data >(
&self,
x : &InputExtract< 'data >,
c : &mut Context< 'buf >,
) -> fmt::Result
{

// dbg!( &x );

for col in &x.col_descriptors
{
write!( c.buf, " - {}\n", col.label )?;
}

write!( c.buf, " {} fields\n", x.col_descriptors.len() )?;

Ok(())
}

}
5 changes: 4 additions & 1 deletion module/core/format_tools/src/format/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,10 @@ mod private
slices[ x.slices_dim.md_offset( md_index ) ] = s;
})
;
x.col_descriptors[ icol ].label = cell.0.as_ref();
if irow == 0
{
x.col_descriptors[ icol ].label = cell.0.as_ref();
}
}

}
Expand Down
4 changes: 3 additions & 1 deletion module/core/format_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ mod fundamental

mod table_test;
mod tabe_foreign_test;
mod format_ordinary_test;

mod format_table_test;
mod format_records_test;
// mod format_keys_test; // qqq : xxx : implement

mod collection_test;
mod fields_test;
Expand Down
13 changes: 12 additions & 1 deletion module/core/format_tools/tests/inc/tabe_foreign_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn iterator_over_objects_without_impl()
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );

let got = AsTable::new( &data ).table_to_string_with_format( &output_format::Table::default() );
println!( "{}", &got );
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
Expand All @@ -106,14 +107,24 @@ fn iterator_over_objects_without_impl()
let mut context = the_module::print::Context::new( &mut output, printer );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
println!( "{}", &output );

let got = AsTable::new( &data ).table_to_string_with_format( &output_format::Records::default() );
println!( "{}", &got );
assert!( got.contains( "│ id │ 1 │" ) );
assert!( got.contains( "│ created_at │ 1627845583 │" ) );
assert!( got.contains( "│ id │ 2 │" ) );
assert!( got.contains( "│ created_at │ 13 │" ) );

// = output as keys

let got = AsTable::new( &data ).table_to_string_with_format( &output_format::Keys::default() );
println!( "{}", &got );
assert!( got.contains( "- id" ) );
assert!( got.contains( "- created_at" ) );
assert!( got.contains( "- file_ids" ) );
assert!( got.contains( "- tools" ) );
assert!( got.contains( "4 fields" ) );

// assert!( false );

}

0 comments on commit 8d66c4f

Please sign in to comment.