Skip to content

Commit e4f2e5b

Browse files
committed
added configuration methods for RowsDisplayer, doc strings and smoke test
1 parent 7046b59 commit e4f2e5b

File tree

1 file changed

+68
-14
lines changed

1 file changed

+68
-14
lines changed

scylla/src/response/query_result.rs

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Debug;
22
use std::fmt;
33
use chrono::{Duration, NaiveDate, NaiveTime, TimeZone, Utc};
4-
use scylla_cql::frame::value::{CqlDate, CqlDecimal, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid};
4+
use scylla_cql::frame::value::{CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid};
55
use tabled::settings::peaker::Priority;
66
use tabled::settings::{Alignment, Width};
77
use tabled::{builder::Builder, settings::Style, settings::themes::Colorization, settings::Color};
@@ -490,6 +490,15 @@ impl QueryRowsResult {
490490
///
491491
/// - `fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result`
492492
/// Formats the rows as a table and writes it to the given formatter.
493+
///
494+
/// - `set_blob_displaying(&mut self, byte_displaying: ByteDisplaying)`
495+
/// Sets the byte display format for blobs.
496+
///
497+
/// - `set_exponent_displaying_integers(&mut self, exponent_displaying_integers: bool)`
498+
/// Sets the exponent display for integers.
499+
///
500+
/// - `set_floating_point_precision(&mut self, precision: usize)`
501+
/// Sets the exponent display for floats and doubles.
493502
494503
pub struct RowsDisplayer<'a> {
495504
query_result: &'a QueryRowsResult,
@@ -498,21 +507,52 @@ pub struct RowsDisplayer<'a> {
498507

499508
impl<'a> RowsDisplayer<'a>
500509
{
510+
/// Creates a new `RowsDisplayer` for the given `QueryRowsResult`.
511+
/// The default settings are:
512+
/// - terminal width: 0,
513+
/// - color usage: true,
514+
/// - byte displaying: `ByteDisplaying::Hex`
501515
pub fn new(query_result: &'a QueryRowsResult) -> Self {
502516
Self {
503517
query_result,
504518
display_settings: RowsDisplayerSettings::new(),
505519
}
506520
}
507521

522+
/// Sets the terminal width for wrapping the table output.
523+
/// The table will be wrapped to the given width, if possible.
524+
/// If the width is set to 0, the table will not be wrapped.
525+
/// The default value is 0.
508526
pub fn set_terminal_width(&mut self, terminal_width: usize) {
509527
self.display_settings.terminal_width = terminal_width;
510528
}
511529

530+
/// Enables or disables color in the table output.
512531
pub fn use_color(&mut self, use_color: bool) {
513532
self.display_settings.print_in_color = use_color;
514533
}
515534

535+
/// Sets the byte display format for blobs.
536+
/// The default value is `ByteDisplaying::Hex`.
537+
/// Possible values are:
538+
/// - `ByteDisplaying::Ascii` - display bytes as ASCII characters,
539+
/// - `ByteDisplaying::Hex` - display bytes as hexadecimal numbers,
540+
/// - `ByteDisplaying::Dec` - display bytes as decimal numbers.
541+
pub fn set_blob_displaying(&mut self, byte_displaying: ByteDisplaying) {
542+
self.display_settings.byte_displaying = byte_displaying;
543+
}
544+
545+
/// Sets the exponent display for integers.
546+
/// If set to true, integers will be displayed in scientific notation.
547+
/// The default value is false.
548+
pub fn set_exponent_displaying_integers(&mut self, exponent_displaying_integers: bool) {
549+
self.display_settings.exponent_displaying_integers = exponent_displaying_integers;
550+
}
551+
552+
/// Sets the exponent display for floats and doubles.
553+
pub fn set_floating_point_precision(&mut self, precision: usize) {
554+
self.display_settings.double_precision = precision;
555+
}
516556

517557
fn get_item_wrapper(&'a self, item: &'a std::option::Option<CqlValue>) -> Box<dyn StringConvertible<'a>> {
518558
match item {
@@ -526,7 +566,7 @@ impl<'a> RowsDisplayer<'a>
526566
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
527567
},
528568
Some(CqlValue::Boolean(value)) => {
529-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
569+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
530570
},
531571
Some(CqlValue::Counter(value)) => {
532572
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
@@ -692,7 +732,7 @@ impl RowsDisplayerSettings {
692732

693733

694734
#[derive(PartialEq)]
695-
enum ByteDisplaying {
735+
pub enum ByteDisplaying {
696736
Ascii,
697737
Hex,
698738
Dec,
@@ -786,7 +826,6 @@ impl fmt::Display for WrapperDisplay<'_, i32> {
786826
}
787827

788828
// bigint
789-
790829
impl fmt::Display for WrapperDisplay<'_, i64> {
791830
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
792831
if self.settings.exponent_displaying_floats {
@@ -810,7 +849,6 @@ impl fmt::Display for WrapperDisplay<'_, f32> {
810849
}
811850

812851
// double
813-
814852
impl fmt::Display for WrapperDisplay<'_, f64> {
815853
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
816854
if self.settings.exponent_displaying_floats {
@@ -823,7 +861,6 @@ impl fmt::Display for WrapperDisplay<'_, f64> {
823861

824862

825863
// blob
826-
827864
impl fmt::Display for WrapperDisplay<'_, Vec<u8>> {
828865
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
829866
let mut result = String::new();
@@ -848,15 +885,13 @@ impl fmt::Display for WrapperDisplay<'_, Vec<u8>> {
848885
}
849886

850887
// string
851-
852888
impl fmt::Display for WrapperDisplay<'_, String> {
853889
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
854890
write!(f, "{}", self.value)
855891
}
856892
}
857893

858894
// timestamp
859-
860895
impl fmt::Display for WrapperDisplay<'_, CqlTimestamp> {
861896
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
862897
// egzample of formating timestamp 14:30:00.000000000
@@ -872,7 +907,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlTimestamp> {
872907
}
873908

874909
// time
875-
876910
impl fmt::Display for WrapperDisplay<'_, CqlTime> {
877911
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
878912
// egzample of formating time 14:30:00.000000000
@@ -898,7 +932,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlTime> {
898932
}
899933

900934
// timeuuid
901-
902935
impl fmt::Display for WrapperDisplay<'_, CqlTimeuuid> {
903936
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
904937
let uuid = self.value.as_bytes();
@@ -913,7 +946,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlTimeuuid> {
913946
}
914947

915948
// duration
916-
917949
impl fmt::Display for WrapperDisplay<'_, CqlDuration> {
918950
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
919951
let months = self.value.months;
@@ -969,7 +1001,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlDuration> {
9691001

9701002

9711003
// date
972-
9731004
impl fmt::Display for WrapperDisplay<'_, CqlDate> {
9741005
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9751006
// egzample of formating date 2021-12-31
@@ -988,7 +1019,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlDate> {
9881019
}
9891020

9901021
// inet
991-
9921022
impl fmt::Display for WrapperDisplay<'_, std::net::IpAddr> {
9931023
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9941024
let ip = self.value;
@@ -1003,6 +1033,13 @@ impl fmt::Display for WrapperDisplay<'_, std::net::IpAddr> {
10031033
}
10041034
}
10051035

1036+
// boolean
1037+
impl fmt::Display for WrapperDisplay<'_, bool> {
1038+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1039+
write!(f, "{}", self.value)
1040+
}
1041+
}
1042+
10061043

10071044
/// An error returned by [`QueryResult::into_rows_result`]
10081045
///
@@ -1086,7 +1123,6 @@ mod tests {
10861123
use itertools::Itertools as _;
10871124
use scylla_cql::frame::response::result::ResultMetadata;
10881125
use scylla_cql::frame::types;
1089-
use std::fmt::Write;
10901126

10911127
use super::*;
10921128

@@ -1374,6 +1410,24 @@ use std::fmt::Write;
13741410
}
13751411
}
13761412
}
1413+
1414+
// use of QueryRowsResult after use of displayer
1415+
{
1416+
let rr = sample_raw_rows(2, 1);
1417+
let rqr = QueryResult::new(Some(rr), None, Vec::new());
1418+
let qr : QueryRowsResult = rqr.into_rows_result().unwrap();
1419+
let mut displayer = qr.rows_displayer();
1420+
displayer.set_terminal_width(80);
1421+
displayer.set_blob_displaying(ByteDisplaying::Hex);
1422+
displayer.use_color(true);
1423+
let _ = format!("{}", displayer);
1424+
let rows = qr.rows::<(&str, bool)>();
1425+
1426+
let mut rows_data = rows.unwrap();
1427+
let row = rows_data.next().unwrap().unwrap();
1428+
1429+
assert_eq!(row, ("MOCK", true));
1430+
}
13771431
}
13781432

13791433
#[test]

0 commit comments

Comments
 (0)