Skip to content

Commit 0cbcba0

Browse files
committed
added configuration methods for RowsDisplayer, doc strings and smoke test
1 parent 68786d7 commit 0cbcba0

File tree

1 file changed

+68
-14
lines changed

1 file changed

+68
-14
lines changed

scylla/src/transport/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};
@@ -479,6 +479,15 @@ impl QueryRowsResult {
479479
///
480480
/// - `fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result`
481481
/// Formats the rows as a table and writes it to the given formatter.
482+
///
483+
/// - `set_blob_displaying(&mut self, byte_displaying: ByteDisplaying)`
484+
/// Sets the byte display format for blobs.
485+
///
486+
/// - `set_exponent_displaying_integers(&mut self, exponent_displaying_integers: bool)`
487+
/// Sets the exponent display for integers.
488+
///
489+
/// - `set_floating_point_precision(&mut self, precision: usize)`
490+
/// Sets the exponent display for floats and doubles.
482491
483492
pub struct RowsDisplayer<'a> {
484493
query_result: &'a QueryRowsResult,
@@ -487,21 +496,52 @@ pub struct RowsDisplayer<'a> {
487496

488497
impl<'a> RowsDisplayer<'a>
489498
{
499+
/// Creates a new `RowsDisplayer` for the given `QueryRowsResult`.
500+
/// The default settings are:
501+
/// - terminal width: 0,
502+
/// - color usage: true,
503+
/// - byte displaying: `ByteDisplaying::Hex`
490504
pub fn new(query_result: &'a QueryRowsResult) -> Self {
491505
Self {
492506
query_result,
493507
display_settings: RowsDisplayerSettings::new(),
494508
}
495509
}
496510

511+
/// Sets the terminal width for wrapping the table output.
512+
/// The table will be wrapped to the given width, if possible.
513+
/// If the width is set to 0, the table will not be wrapped.
514+
/// The default value is 0.
497515
pub fn set_terminal_width(&mut self, terminal_width: usize) {
498516
self.display_settings.terminal_width = terminal_width;
499517
}
500518

519+
/// Enables or disables color in the table output.
501520
pub fn use_color(&mut self, use_color: bool) {
502521
self.display_settings.print_in_color = use_color;
503522
}
504523

524+
/// Sets the byte display format for blobs.
525+
/// The default value is `ByteDisplaying::Hex`.
526+
/// Possible values are:
527+
/// - `ByteDisplaying::Ascii` - display bytes as ASCII characters,
528+
/// - `ByteDisplaying::Hex` - display bytes as hexadecimal numbers,
529+
/// - `ByteDisplaying::Dec` - display bytes as decimal numbers.
530+
pub fn set_blob_displaying(&mut self, byte_displaying: ByteDisplaying) {
531+
self.display_settings.byte_displaying = byte_displaying;
532+
}
533+
534+
/// Sets the exponent display for integers.
535+
/// If set to true, integers will be displayed in scientific notation.
536+
/// The default value is false.
537+
pub fn set_exponent_displaying_integers(&mut self, exponent_displaying_integers: bool) {
538+
self.display_settings.exponent_displaying_integers = exponent_displaying_integers;
539+
}
540+
541+
/// Sets the exponent display for floats and doubles.
542+
pub fn set_floating_point_precision(&mut self, precision: usize) {
543+
self.display_settings.double_precision = precision;
544+
}
505545

506546
fn get_item_wrapper(&'a self, item: &'a std::option::Option<CqlValue>) -> Box<dyn StringConvertible<'a>> {
507547
match item {
@@ -515,7 +555,7 @@ impl<'a> RowsDisplayer<'a>
515555
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
516556
},
517557
Some(CqlValue::Boolean(value)) => {
518-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
558+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
519559
},
520560
Some(CqlValue::Counter(value)) => {
521561
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
@@ -681,7 +721,7 @@ impl RowsDisplayerSettings {
681721

682722

683723
#[derive(PartialEq)]
684-
enum ByteDisplaying {
724+
pub enum ByteDisplaying {
685725
Ascii,
686726
Hex,
687727
Dec,
@@ -775,7 +815,6 @@ impl fmt::Display for WrapperDisplay<'_, i32> {
775815
}
776816

777817
// bigint
778-
779818
impl fmt::Display for WrapperDisplay<'_, i64> {
780819
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
781820
if self.settings.exponent_displaying_floats {
@@ -799,7 +838,6 @@ impl fmt::Display for WrapperDisplay<'_, f32> {
799838
}
800839

801840
// double
802-
803841
impl fmt::Display for WrapperDisplay<'_, f64> {
804842
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
805843
if self.settings.exponent_displaying_floats {
@@ -812,7 +850,6 @@ impl fmt::Display for WrapperDisplay<'_, f64> {
812850

813851

814852
// blob
815-
816853
impl fmt::Display for WrapperDisplay<'_, Vec<u8>> {
817854
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
818855
let mut result = String::new();
@@ -837,15 +874,13 @@ impl fmt::Display for WrapperDisplay<'_, Vec<u8>> {
837874
}
838875

839876
// string
840-
841877
impl fmt::Display for WrapperDisplay<'_, String> {
842878
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
843879
write!(f, "{}", self.value)
844880
}
845881
}
846882

847883
// timestamp
848-
849884
impl fmt::Display for WrapperDisplay<'_, CqlTimestamp> {
850885
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
851886
// egzample of formating timestamp 14:30:00.000000000
@@ -861,7 +896,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlTimestamp> {
861896
}
862897

863898
// time
864-
865899
impl fmt::Display for WrapperDisplay<'_, CqlTime> {
866900
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
867901
// egzample of formating time 14:30:00.000000000
@@ -887,7 +921,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlTime> {
887921
}
888922

889923
// timeuuid
890-
891924
impl fmt::Display for WrapperDisplay<'_, CqlTimeuuid> {
892925
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
893926
let uuid = self.value.as_bytes();
@@ -902,7 +935,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlTimeuuid> {
902935
}
903936

904937
// duration
905-
906938
impl fmt::Display for WrapperDisplay<'_, CqlDuration> {
907939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
908940
let months = self.value.months;
@@ -958,7 +990,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlDuration> {
958990

959991

960992
// date
961-
962993
impl fmt::Display for WrapperDisplay<'_, CqlDate> {
963994
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
964995
// egzample of formating date 2021-12-31
@@ -977,7 +1008,6 @@ impl fmt::Display for WrapperDisplay<'_, CqlDate> {
9771008
}
9781009

9791010
// inet
980-
9811011
impl fmt::Display for WrapperDisplay<'_, std::net::IpAddr> {
9821012
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9831013
let ip = self.value;
@@ -992,6 +1022,13 @@ impl fmt::Display for WrapperDisplay<'_, std::net::IpAddr> {
9921022
}
9931023
}
9941024

1025+
// boolean
1026+
impl fmt::Display for WrapperDisplay<'_, bool> {
1027+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1028+
write!(f, "{}", self.value)
1029+
}
1030+
}
1031+
9951032

9961033
/// An error returned by [`QueryResult::into_rows_result`]
9971034
///
@@ -1075,7 +1112,6 @@ mod tests {
10751112
use itertools::Itertools as _;
10761113
use scylla_cql::frame::response::result::ResultMetadata;
10771114
use scylla_cql::frame::types;
1078-
use std::fmt::Write;
10791115

10801116
use super::*;
10811117

@@ -1363,6 +1399,24 @@ use std::fmt::Write;
13631399
}
13641400
}
13651401
}
1402+
1403+
// use of QueryRowsResult after use of displayer
1404+
{
1405+
let rr = sample_raw_rows(2, 1);
1406+
let rqr = QueryResult::new(Some(rr), None, Vec::new());
1407+
let qr : QueryRowsResult = rqr.into_rows_result().unwrap();
1408+
let mut displayer = qr.rows_displayer();
1409+
displayer.set_terminal_width(80);
1410+
displayer.set_blob_displaying(ByteDisplaying::Hex);
1411+
displayer.use_color(true);
1412+
let _ = format!("{}", displayer);
1413+
let rows = qr.rows::<(&str, bool)>();
1414+
1415+
let mut rows_data = rows.unwrap();
1416+
let row = rows_data.next().unwrap().unwrap();
1417+
1418+
assert_eq!(row, ("MOCK", true));
1419+
}
13661420
}
13671421

13681422
#[test]

0 commit comments

Comments
 (0)