From 9370621db77f3ba83f445bd4f80b65093dafad20 Mon Sep 17 00:00:00 2001 From: Ruslan Date: Thu, 14 Nov 2024 19:28:08 +0200 Subject: [PATCH] Fix for `format_tools` for overflow subtract (#1490) * Refert previous fix * Add test * Add test * Add fix * Revert wrong fix * Add correct test * Add fix --- .../src/format/output_format/table.rs | 15 +++---------- module/core/format_tools/src/format/print.rs | 2 ++ .../tests/inc/format_table_test.rs | 22 +++++++++++++++++++ .../format_tools/tests/inc/test_object.rs | 14 ++++++++++++ .../tests/inc/to_string_with_fallback_test.rs | 4 ++-- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/module/core/format_tools/src/format/output_format/table.rs b/module/core/format_tools/src/format/output_format/table.rs index e96af36f20..81a16fb0f2 100644 --- a/module/core/format_tools/src/format/output_format/table.rs +++ b/module/core/format_tools/src/format/output_format/table.rs @@ -247,19 +247,10 @@ impl TableOutputFormat for Table write!( c.buf, "{}", cell_prefix )?; - println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | slice.len() : {}", slice.len() ); + // println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | slice.len() : {}", slice.len() ); - let lspaces = if cell_width > width { - 0 - } else { - ( width - cell_width ) / 2 - }; - - let rspaces = if (cell_width > width) || (slice.len() > cell_width) { - 0 - } else { - ( width - cell_width + 1 ) / 2 + cell_width - slice.len() - }; + let lspaces = ( width - cell_width ) / 2; + let rspaces = ( width - cell_width + 1 ) / 2 + cell_width - slice.chars().count(); // println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | lspaces : {lspaces} | rspaces : {rspaces}" ); diff --git a/module/core/format_tools/src/format/print.rs b/module/core/format_tools/src/format/print.rs index 78ac91b294..f3c5d2acc6 100644 --- a/module/core/format_tools/src/format/print.rs +++ b/module/core/format_tools/src/format/print.rs @@ -617,6 +617,8 @@ pub mod own Context, Printer, InputExtract, + RowDescriptor, + ColDescriptor, }; } diff --git a/module/core/format_tools/tests/inc/format_table_test.rs b/module/core/format_tools/tests/inc/format_table_test.rs index eb8a3b17dd..4f4d6694d2 100644 --- a/module/core/format_tools/tests/inc/format_table_test.rs +++ b/module/core/format_tools/tests/inc/format_table_test.rs @@ -8,6 +8,9 @@ use the_module:: filter, print, output_format, + print::{ InputExtract, RowDescriptor, ColDescriptor }, + TableOutputFormat, + filter::LineType }; use std:: @@ -326,3 +329,22 @@ fn filter_row_callback() // // xxx : implement test for vector of vectors + +// + +#[ test ] +fn no_subtract_with_overflow() +{ + let test_objects = test_object::test_objects_gen_with_unicode(); + + let format = output_format::Table::default(); + let printer = print::Printer::with_format( &format ); + + let as_table = AsTable::new( &test_objects ); + let mut output = String::new(); + let mut context = print::Context::new( &mut output, printer ); + + let result = the_module::TableFormatter::fmt( &as_table, &mut context ); + + assert!( result.is_ok() ); +} \ No newline at end of file diff --git a/module/core/format_tools/tests/inc/test_object.rs b/module/core/format_tools/tests/inc/test_object.rs index f710266a4d..70c702d035 100644 --- a/module/core/format_tools/tests/inc/test_object.rs +++ b/module/core/format_tools/tests/inc/test_object.rs @@ -200,3 +200,17 @@ pub fn test_objects_gen() -> Vec< TestObject > ] } + +pub fn test_objects_gen_with_unicode() -> Vec< TestObject > +{ + vec! + [ + TestObject + { + id : "Юнікод".to_string(), + created_at : 100, + file_ids : vec![], + tools : None, + } + ] +} \ No newline at end of file diff --git a/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs b/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs index bd9947cd71..e0c39527c3 100644 --- a/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs +++ b/module/core/format_tools/tests/inc/to_string_with_fallback_test.rs @@ -9,12 +9,12 @@ use the_module:: WithDebug, WithDisplay, // the_module::to_string_with_fallback::Ref, - to_string_with_fallback, + to_string_with_fallback }; use std:: { - // fmt, + fmt, // collections::HashMap, borrow::Cow, };