Skip to content

Commit f8458d6

Browse files
committed
added more examples
1 parent dac2022 commit f8458d6

File tree

1 file changed

+120
-9
lines changed

1 file changed

+120
-9
lines changed

scylla/src/transport/query_result.rs

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::fmt::Debug;
22
use std::fmt;
3-
use chrono::{NaiveTime, TimeZone, Utc};
4-
use scylla_cql::frame::value::{CqlTime, CqlTimestamp};
3+
use chrono::{Duration, NaiveDate, NaiveTime, TimeZone, Utc};
4+
use scylla_cql::frame::value::{CqlDate, CqlDecimal, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid};
5+
use tabled::settings::Alignment;
56
use tabled::{builder::Builder, settings::Style, settings::themes::Colorization, settings::Color};
67

78
use thiserror::Error;
@@ -471,7 +472,7 @@ impl<'a> RowsDisplayer<'a>
471472
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
472473
},
473474
Some(CqlValue::BigInt(value)) => {
474-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
475+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
475476
},
476477
Some(CqlValue::Blob(value)) => {
477478
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
@@ -504,7 +505,7 @@ impl<'a> RowsDisplayer<'a>
504505
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
505506
},
506507
Some(CqlValue::Inet(value)) => {
507-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
508+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
508509
},
509510
Some(CqlValue::List(value)) => { // TODO set formating for list
510511
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
@@ -522,16 +523,16 @@ impl<'a> RowsDisplayer<'a>
522523
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
523524
},
524525
Some(CqlValue::Date(value)) => { // TODO set formating for date
525-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
526+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
526527
},
527528
Some(CqlValue::Duration(value)) => {
528-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
529+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
529530
},
530531
Some(CqlValue::Empty) => {
531532
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
532533
},
533534
Some(CqlValue::SmallInt(value)) => {
534-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
535+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
535536
},
536537
Some(CqlValue::TinyInt(value)) => {
537538
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
@@ -543,7 +544,7 @@ impl<'a> RowsDisplayer<'a>
543544
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
544545
},
545546
Some(CqlValue::Timeuuid(value)) => {
546-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
547+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
547548
},
548549
None => {
549550
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
@@ -679,6 +680,7 @@ impl fmt::Display for WrapperDisplay<'_, f64> {
679680
}
680681
}
681682

683+
682684
// blob
683685

684686
impl fmt::Display for WrapperDisplay<'_, Vec<u8>> {
@@ -754,6 +756,114 @@ impl fmt::Display for WrapperDisplay<'_, CqlTime> {
754756
}
755757
}
756758

759+
// timeuuid
760+
761+
impl fmt::Display for WrapperDisplay<'_, CqlTimeuuid> {
762+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
763+
let uuid = self.value.as_bytes();
764+
765+
write!(f, "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
766+
uuid[0], uuid[1], uuid[2], uuid[3],
767+
uuid[4], uuid[5],
768+
uuid[6], uuid[7],
769+
uuid[8], uuid[9],
770+
uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15])
771+
}
772+
}
773+
774+
// duration
775+
776+
impl fmt::Display for WrapperDisplay<'_, CqlDuration> {
777+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
778+
let months = self.value.months;
779+
let days = self.value.days;
780+
let nanoseconds = self.value.nanoseconds;
781+
782+
let years = months / 12;
783+
let months = months % 12;
784+
let weeks = days / 7;
785+
let days = days % 7;
786+
let hours = nanoseconds / 3_600_000_000_000;
787+
let minutes = (nanoseconds % 3_600_000_000_000) / 60_000_000_000;
788+
let seconds = (nanoseconds % 60_000_000_000) / 1_000_000_000;
789+
let nanoseconds = nanoseconds % 1_000_000_000;
790+
791+
let mut result = String::new();
792+
if years != 0 {
793+
result.push_str(&format!("{}y", years));
794+
}
795+
796+
if months != 0 {
797+
result.push_str(&format!("{}mo", months));
798+
}
799+
800+
if weeks != 0 {
801+
result.push_str(&format!("{}w", weeks));
802+
}
803+
804+
if days != 0 {
805+
result.push_str(&format!("{}d", days));
806+
}
807+
808+
if hours != 0 {
809+
result.push_str(&format!("{}h", hours));
810+
}
811+
812+
if minutes != 0 {
813+
result.push_str(&format!("{}m", minutes));
814+
}
815+
816+
if seconds != 0 {
817+
result.push_str(&format!("{}s", seconds));
818+
}
819+
820+
if nanoseconds != 0 {
821+
result.push_str(&format!("{}ns", nanoseconds));
822+
}
823+
824+
// Format the time with 9 digits of nanoseconds
825+
write!(f, "{}", result)
826+
}
827+
}
828+
829+
830+
// date
831+
832+
impl fmt::Display for WrapperDisplay<'_, CqlDate> {
833+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
834+
// egzample of formating date 2021-12-31
835+
let magic_constant = 2055453495; // it is number of days from -5877641-06-23 to -250000-01-01
836+
// around -250000-01-01 is the limit of naive date
837+
838+
let days = self.value.0 - magic_constant;
839+
println!("days: {}", days);
840+
let base_date = NaiveDate::from_ymd_opt(-250000, 1, 1).unwrap();
841+
842+
// Add the number of days
843+
println!("days as i64: {}", days as i64);
844+
let target_date = base_date + Duration::days(days as i64);
845+
846+
// Format the date
847+
write!(f, "{}", target_date.format("%Y-%m-%d"))
848+
}
849+
}
850+
851+
// inet
852+
853+
impl fmt::Display for WrapperDisplay<'_, std::net::IpAddr> {
854+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
855+
let ip = self.value;
856+
match ip {
857+
std::net::IpAddr::V4(ipv4) => {
858+
write!(f, "{}", ipv4)
859+
},
860+
std::net::IpAddr::V6(ipv6) => {
861+
write!(f, "{}", ipv6)
862+
},
863+
}
864+
}
865+
}
866+
757867

758868

759869

@@ -790,7 +900,8 @@ impl fmt::Display for RowsDisplayer<'_> {
790900
table.with(Style::psql())
791901
// Width::wrap(self.terminal_width).priority(Priority::max(true)),
792902
.with(Colorization::columns([Color::FG_GREEN]))
793-
.with(Colorization::exact([Color::FG_MAGENTA], tabled::settings::object::Rows::first()));
903+
.with(Colorization::exact([Color::FG_MAGENTA], tabled::settings::object::Rows::first()))
904+
.with(Alignment::right());
794905

795906
write!(f, "{}", table)
796907
}

0 commit comments

Comments
 (0)