From 5ea1a35270d64ec4e06b405691b1d4e7799a72b3 Mon Sep 17 00:00:00 2001 From: michal Date: Sun, 26 Jan 2025 20:06:45 +0100 Subject: [PATCH] rows_displayer: addressed comments - fixed Cargo.toml, added docstring for rows_displayer, made code more idiomatic --- examples/Cargo.toml | 3 +- examples/displayer.rs | 90 ++++++++++++++++++--------- scylla/Cargo.toml | 8 ++- scylla/src/response/query_result.rs | 3 + scylla/src/response/rows_displayer.rs | 89 ++++++++++++-------------- 5 files changed, 110 insertions(+), 83 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 89f18aabe..f4b8e09ee 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -132,5 +132,4 @@ path = "execution_profile.rs" [[example]] name = "displayer" -path = "displayer.rs" -futures = ["result-displayer"] \ No newline at end of file +path = "displayer.rs" \ No newline at end of file diff --git a/examples/displayer.rs b/examples/displayer.rs index e69c4b439..fae2ebaf1 100644 --- a/examples/displayer.rs +++ b/examples/displayer.rs @@ -1,7 +1,7 @@ use anyhow::Result; use scylla::{ client::{session::Session, session_builder::SessionBuilder}, - response::{query_result::QueryRowsResult, rows_displayer::ByteDisplaying}, + response::{query_result::QueryRowsResult, rows_displayer::ByteDisplaying, PagingState}, }; use std::env; @@ -51,10 +51,15 @@ async fn main() -> Result<()> { .await?; // example 1 - basic table - let result: QueryRowsResult = session - .query_unpaged("SELECT a, b, c FROM examples_ks.basic", &[]) - .await? - .into_rows_result()?; + + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page("SELECT a, b, c FROM examples_ks.basic", &[], paging_state) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + + let result = res.into_rows_result()?; let displayer = result.rows_displayer(); println!("\nlong text and special characters:"); @@ -78,10 +83,14 @@ async fn main() -> Result<()> { ) .await?; - let result: QueryRowsResult = session - .query_unpaged("SELECT * FROM examples_ks.basic4", &[]) - .await? - .into_rows_result()?; + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page("SELECT * FROM examples_ks.basic4", &[], paging_state) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + + let result = res.into_rows_result()?; let mut displayer = result.rows_displayer(); displayer.set_blob_displaying(ByteDisplaying::Ascii); @@ -143,10 +152,13 @@ async fn main() -> Result<()> { ) .await?; - let result: QueryRowsResult = session - .query_unpaged("SELECT * FROM examples_ks.basic6", &[]) - .await? - .into_rows_result()?; + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page("SELECT * FROM examples_ks.basic6", &[], paging_state) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + let result = res.into_rows_result()?; let mut displayer = result.rows_displayer(); println!("\ndate, duration, ip address, timeuuid:"); @@ -174,10 +186,17 @@ async fn main() -> Result<()> { ) .await?; - let result: QueryRowsResult = session - .query_unpaged("SELECT * FROM examples_ks.upcoming_calendar", &[]) - .await? - .into_rows_result()?; + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page( + "SELECT * FROM examples_ks.upcoming_calendar", + &[], + paging_state, + ) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + let result = res.into_rows_result()?; let displayer = result.rows_displayer(); println!("\nList:"); @@ -206,10 +225,13 @@ async fn main() -> Result<()> { ) .await?; - let result: QueryRowsResult = session - .query_unpaged("SELECT * FROM examples_ks.cyclist_teams", &[]) - .await? - .into_rows_result()?; + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page("SELECT * FROM examples_ks.cyclist_teams", &[], paging_state) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + let result = res.into_rows_result()?; let displayer = result.rows_displayer(); println!("\nMap:"); @@ -233,10 +255,17 @@ async fn main() -> Result<()> { ) .await?; - let result: QueryRowsResult = session - .query_unpaged("SELECT * FROM examples_ks.cyclist_career_teams", &[]) - .await? - .into_rows_result()?; + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page( + "SELECT * FROM examples_ks.cyclist_career_teams", + &[], + paging_state, + ) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + let result = res.into_rows_result()?; let displayer = result.rows_displayer(); println!("\nSet:"); @@ -302,10 +331,13 @@ async fn main() -> Result<()> { ) .await?; - let result: QueryRowsResult = session - .query_unpaged("SELECT * FROM examples_ks.route", &[]) - .await? - .into_rows_result()?; + // fetch the data + let paging_state: PagingState = PagingState::start(); + let (res, _) = session + .query_single_page("SELECT * FROM examples_ks.route", &[], paging_state) + .await?; + // all the data is in the first page, so there is no need to fetch more pages + let result = res.into_rows_result()?; let displayer = result.rows_displayer(); println!("\nTuples:"); diff --git a/scylla/Cargo.toml b/scylla/Cargo.toml index d4c87ed39..cb9a2b9e4 100644 --- a/scylla/Cargo.toml +++ b/scylla/Cargo.toml @@ -39,7 +39,7 @@ full-serialization = [ "num-bigint-04", "bigdecimal-04", ] -result-displayer = ["dep:tabled", "dep:inline_colorization"] +result-displayer = ["dep:tabled", "dep:inline_colorization", "dep:num-bigint-04", "dep:bigdecimal-04"] [dependencies] scylla-macros = { version = "0.7.0", path = "../scylla-macros" } @@ -80,10 +80,12 @@ socket2 = { version = "0.5.3", features = ["all"] } lazy_static = "1" tabled = { version = "0.17.0", features = ["std", "ansi"], optional = true } inline_colorization = { version = "0.1.6", optional = true } -num-bigint-04 = { package = "num-bigint", version = "0.4" } -bigdecimal-04 = { package = "bigdecimal", version = "0.4" } +num-bigint-04 = { package = "num-bigint", version = "0.4", optional = true } +bigdecimal-04 = { package = "bigdecimal", version = "0.4", optional = true } [dev-dependencies] +num-bigint-04 = { package = "num-bigint", version = "0.4" } +bigdecimal-04 = { package = "bigdecimal", version = "0.4" } num-bigint-03 = { package = "num-bigint", version = "0.3" } scylla-proxy = { version = "0.0.3", path = "../scylla-proxy" } ntest = "0.9.3" diff --git a/scylla/src/response/query_result.rs b/scylla/src/response/query_result.rs index 5d57ca98d..02374a9b6 100644 --- a/scylla/src/response/query_result.rs +++ b/scylla/src/response/query_result.rs @@ -447,6 +447,9 @@ impl QueryRowsResult { (raw_rows_with_metadata, tracing_id, warnings) } + /// Returns a displayer for the rows. + /// + /// This method is only available when the `result-displayer` feature is enabled. #[cfg(feature = "result-displayer")] pub fn rows_displayer(&self) -> RowsDisplayer<'_> { RowsDisplayer::new(self) diff --git a/scylla/src/response/rows_displayer.rs b/scylla/src/response/rows_displayer.rs index 2128d3c22..0c3de8a39 100644 --- a/scylla/src/response/rows_displayer.rs +++ b/scylla/src/response/rows_displayer.rs @@ -329,20 +329,20 @@ impl Default for RowsDisplayerSettings { #[derive(Clone, Debug)] struct ValueColors { - pub text: &'static str, - pub error: &'static str, - pub blob: &'static str, - pub timestamp: &'static str, - pub date: &'static str, - pub time: &'static str, - pub int: &'static str, - pub float: &'static str, - pub decimal: &'static str, - pub inet: &'static str, - pub boolean: &'static str, - pub uuid: &'static str, - pub duration: &'static str, - pub collection: &'static str, + text: &'static str, + error: &'static str, + blob: &'static str, + timestamp: &'static str, + date: &'static str, + time: &'static str, + int: &'static str, + float: &'static str, + decimal: &'static str, + inet: &'static str, + boolean: &'static str, + uuid: &'static str, + duration: &'static str, + collection: &'static str, } impl Default for ValueColors { @@ -808,55 +808,38 @@ impl fmt::Display for WrapperDisplay<'_, CqlDuration> { let seconds = (nanoseconds % 60_000_000_000) / 1_000_000_000; let nanoseconds = nanoseconds % 1_000_000_000; - // let mut result = String::new(); if years != 0 { - // result.push_str(&format!("{}y", years)); write_colored!(f, if_color, color, "{}y", years)?; } if months != 0 { - // result.push_str(&format!("{}mo", months)); write_colored!(f, if_color, color, "{}mo", months)?; } if weeks != 0 { - // result.push_str(&format!("{}w", weeks)); write_colored!(f, if_color, color, "{}w", weeks)?; } if days != 0 { - // result.push_str(&format!("{}d", days)); write_colored!(f, if_color, color, "{}d", days)?; } if hours != 0 { - // result.push_str(&format!("{}h", hours)); write_colored!(f, if_color, color, "{}h", hours)?; } if minutes != 0 { - // result.push_str(&format!("{}m", minutes)); write_colored!(f, if_color, color, "{}m", minutes)?; } if seconds != 0 { - // result.push_str(&format!("{}s", seconds)); write_colored!(f, if_color, color, "{}s", seconds)?; } if nanoseconds != 0 { - // result.push_str(&format!("{}ns", nanoseconds)); write_colored!(f, if_color, color, "{}ns", nanoseconds)?; } Ok(()) - // Format the time with 9 digits of nanoseconds - // write_colored!( - // f, - // self.settings.print_in_color, - // self.settings.colors.duration, - // "{}", - // result - // ) } } @@ -864,8 +847,11 @@ impl fmt::Display for WrapperDisplay<'_, CqlDuration> { impl fmt::Display for WrapperDisplay<'_, CqlDate> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // example of formating date 2021-12-31 + // CqlDate is Represented as number of days since -5877641-06-23 i.e. 2^31 days before unix epoch. let magic_constant = 2055453495; // it is number of days from -5877641-06-23 to -250000-01-01 // around -250000-01-01 is the limit of naive date + // without this constant we can't convert days to date + // because from_ymd_opt will return None let days = self.value.0 - magic_constant; let base_date = NaiveDate::from_ymd_opt(-250000, 1, 1).unwrap(); @@ -956,23 +942,25 @@ fn display_in_quotes_and_colored<'a>( impl fmt::Display for WrapperDisplay<'_, Vec> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // choose opening bracket - if matches!(self.cql_value, CqlValue::List(_)) { - write_colored!( + match self.cql_value { + CqlValue::List(_) => write_colored!( f, self.settings.print_in_color, self.settings.colors.collection, "[" - )?; - } else if matches!(self.cql_value, CqlValue::Set(_)) { - // it is a set - write_colored!( + )?, + CqlValue::Set(_) => write_colored!( f, self.settings.print_in_color, self.settings.colors.collection, "{{" - )?; - } else { - panic!("BUG"); // Describe the reason why it might happen. + )?, + _ => write_colored!( + f, + self.settings.print_in_color, + self.settings.colors.error, + "Invalid type of collection. Expected List or Set", + )?, } // print elements @@ -1002,24 +990,27 @@ impl fmt::Display for WrapperDisplay<'_, Vec> { } // choose closing bracket - if matches!(self.cql_value, CqlValue::List(_)) { - write_colored!( + match self.cql_value { + CqlValue::List(_) => write_colored!( f, self.settings.print_in_color, self.settings.colors.collection, "]" - ) - } else if matches!(self.cql_value, CqlValue::Set(_)) { - // it is a set - write_colored!( + )?, + CqlValue::Set(_) => write_colored!( f, self.settings.print_in_color, self.settings.colors.collection, "}}" - ) - } else { - panic!("BUG") // Describe the reason why it might happen. + )?, + _ => write_colored!( + f, + self.settings.print_in_color, + self.settings.colors.error, + "Invalid type of collection. Expected List or Set", + )?, } + Ok(()) } }