Skip to content

Commit

Permalink
rows_displayer: addressed comments - fixed Cargo.toml, added docstrin…
Browse files Browse the repository at this point in the history
…g for rows_displayer, made code more idiomatic
  • Loading branch information
Michu1596 committed Jan 26, 2025
1 parent 9b05d0e commit 5ea1a35
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 83 deletions.
3 changes: 1 addition & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,4 @@ path = "execution_profile.rs"

[[example]]
name = "displayer"
path = "displayer.rs"
futures = ["result-displayer"]
path = "displayer.rs"
90 changes: 61 additions & 29 deletions examples/displayer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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:");
Expand All @@ -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);
Expand Down Expand Up @@ -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:");
Expand Down Expand Up @@ -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:");
Expand Down Expand Up @@ -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:");
Expand All @@ -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:");
Expand Down Expand Up @@ -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:");
Expand Down
8 changes: 5 additions & 3 deletions scylla/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions scylla/src/response/query_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
89 changes: 40 additions & 49 deletions scylla/src/response/rows_displayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -808,64 +808,50 @@ 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
// )
}
}

// date
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();
Expand Down Expand Up @@ -956,23 +942,25 @@ fn display_in_quotes_and_colored<'a>(
impl fmt::Display for WrapperDisplay<'_, Vec<CqlValue>> {
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
Expand Down Expand Up @@ -1002,24 +990,27 @@ impl fmt::Display for WrapperDisplay<'_, Vec<CqlValue>> {
}

// 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(())
}
}

Expand Down

0 comments on commit 5ea1a35

Please sign in to comment.