Skip to content

Commit a0317ae

Browse files
committed
added more examples
1 parent 4986565 commit a0317ae

File tree

1 file changed

+120
-9
lines changed

1 file changed

+120
-9
lines changed

scylla/src/response/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;
@@ -482,7 +483,7 @@ impl<'a> RowsDisplayer<'a>
482483
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
483484
},
484485
Some(CqlValue::BigInt(value)) => {
485-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
486+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
486487
},
487488
Some(CqlValue::Blob(value)) => {
488489
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
@@ -515,7 +516,7 @@ impl<'a> RowsDisplayer<'a>
515516
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
516517
},
517518
Some(CqlValue::Inet(value)) => {
518-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
519+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
519520
},
520521
Some(CqlValue::List(value)) => { // TODO set formating for list
521522
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
@@ -533,16 +534,16 @@ impl<'a> RowsDisplayer<'a>
533534
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
534535
},
535536
Some(CqlValue::Date(value)) => { // TODO set formating for date
536-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
537+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
537538
},
538539
Some(CqlValue::Duration(value)) => {
539-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
540+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
540541
},
541542
Some(CqlValue::Empty) => {
542543
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
543544
},
544545
Some(CqlValue::SmallInt(value)) => {
545-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
546+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
546547
},
547548
Some(CqlValue::TinyInt(value)) => {
548549
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
@@ -554,7 +555,7 @@ impl<'a> RowsDisplayer<'a>
554555
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
555556
},
556557
Some(CqlValue::Timeuuid(value)) => {
557-
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
558+
Box::new(WrapperDisplay{value: value, settings: &self.display_settings})
558559
},
559560
None => {
560561
Box::new(WrapperDisplay{value: &None, settings: &self.display_settings})
@@ -690,6 +691,7 @@ impl fmt::Display for WrapperDisplay<'_, f64> {
690691
}
691692
}
692693

694+
693695
// blob
694696

695697
impl fmt::Display for WrapperDisplay<'_, Vec<u8>> {
@@ -765,6 +767,114 @@ impl fmt::Display for WrapperDisplay<'_, CqlTime> {
765767
}
766768
}
767769

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

769879

770880

@@ -801,7 +911,8 @@ impl fmt::Display for RowsDisplayer<'_> {
801911
table.with(Style::psql())
802912
// Width::wrap(self.terminal_width).priority(Priority::max(true)),
803913
.with(Colorization::columns([Color::FG_GREEN]))
804-
.with(Colorization::exact([Color::FG_MAGENTA], tabled::settings::object::Rows::first()));
914+
.with(Colorization::exact([Color::FG_MAGENTA], tabled::settings::object::Rows::first()))
915+
.with(Alignment::right());
805916

806917
write!(f, "{}", table)
807918
}

0 commit comments

Comments
 (0)