From a2d493c8607875e11ac8dd320790cdd56ccc7bd6 Mon Sep 17 00:00:00 2001 From: kamille Date: Thu, 18 Jul 2024 01:22:11 +0800 Subject: [PATCH] remove repeated codes. --- arrow-cast/src/display.rs | 121 +++++++++++++------------------------- parquet-testing | 2 +- rust-toolchain | 1 + testing | 2 +- 4 files changed, 43 insertions(+), 83 deletions(-) create mode 100644 rust-toolchain diff --git a/arrow-cast/src/display.rs b/arrow-cast/src/display.rs index ab172cb240cf..312e7973963e 100644 --- a/arrow-cast/src/display.rs +++ b/arrow-cast/src/display.rs @@ -662,17 +662,17 @@ impl<'a> DisplayIndex for &'a PrimitiveArray { impl<'a> DisplayIndex for &'a PrimitiveArray { fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult { let value = self.value(idx); - let mut first_part = true; + let mut prefix = ""; if value.days != 0 { - write!(f, "{} days", value.days)?; - first_part = false; + write!(f, "{prefix}{} days", value.days)?; + prefix = " "; } if value.milliseconds != 0 { let millis_fmt = MillisecondsFormatter { milliseconds: value.milliseconds, - first_part, + prefix, }; f.write_fmt(format_args!("{millis_fmt}"))?; @@ -685,26 +685,22 @@ impl<'a> DisplayIndex for &'a PrimitiveArray { impl<'a> DisplayIndex for &'a PrimitiveArray { fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult { let value = self.value(idx); - let mut first_part = true; + let mut prefix = ""; if value.months != 0 { - write!(f, "{} mons", value.months)?; - first_part = false; + write!(f, "{prefix}{} mons", value.months)?; + prefix = " "; } if value.days != 0 { - if first_part { - write!(f, "{} days", value.days)?; - first_part = false; - } else { - write!(f, " {} days", value.days)?; - } + write!(f, "{prefix}{} days", value.days)?; + prefix = " "; } if value.nanoseconds != 0 { let nano_fmt = NanosecondsFormatter { nanoseconds: value.nanoseconds, - first_part, + prefix, }; f.write_fmt(format_args!("{nano_fmt}"))?; } @@ -713,14 +709,14 @@ impl<'a> DisplayIndex for &'a PrimitiveArray { } } -struct NanosecondsFormatter { +struct NanosecondsFormatter<'a> { nanoseconds: i64, - first_part: bool, + prefix: &'a str, } -impl Display for NanosecondsFormatter { +impl<'a> Display for NanosecondsFormatter<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let mut first_part = self.first_part; + let mut prefix = self.prefix; let secs = self.nanoseconds / 1_000_000_000; let mins = secs / 60; @@ -732,57 +728,38 @@ impl Display for NanosecondsFormatter { let nanoseconds = self.nanoseconds % 1_000_000_000; if hours != 0 { - if first_part { - write!(f, "{} hours", hours)?; - first_part = false; - } else { - write!(f, " {} hours", hours)?; - } + write!(f, "{prefix}{} hours", hours)?; + prefix = " "; } if mins != 0 { - if first_part { - write!(f, "{} mins", mins)?; - first_part = false; - } else { - write!(f, " {} mins", mins)?; - } + write!(f, "{prefix}{} mins", mins)?; + prefix = " "; } if secs != 0 || nanoseconds != 0 { let secs_sign = if secs < 0 || nanoseconds < 0 { "-" } else { "" }; - - if first_part { - write!( - f, - "{}{}.{:09} secs", - secs_sign, - secs.abs(), - nanoseconds.abs() - )?; - } else { - write!( - f, - " {}{}.{:09} secs", - secs_sign, - secs.abs(), - nanoseconds.abs() - )?; - } + write!( + f, + "{prefix}{}{}.{:09} secs", + secs_sign, + secs.abs(), + nanoseconds.abs() + )?; } Ok(()) } } -struct MillisecondsFormatter { +struct MillisecondsFormatter<'a> { milliseconds: i32, - first_part: bool, + prefix: &'a str, } -impl Display for MillisecondsFormatter { +impl<'a> Display for MillisecondsFormatter<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let mut first_part = self.first_part; + let mut prefix = self.prefix; let secs = self.milliseconds / 1_000; let mins = secs / 60; @@ -794,21 +771,13 @@ impl Display for MillisecondsFormatter { let milliseconds = self.milliseconds % 1_000; if hours != 0 { - if first_part { - write!(f, "{} hours", hours,)?; - first_part = false; - } else { - write!(f, " {} hours", hours,)?; - } + write!(f, "{prefix}{} hours", hours,)?; + prefix = " "; } if mins != 0 { - if first_part { - write!(f, "{} mins", mins,)?; - first_part = false; - } else { - write!(f, " {} mins", mins,)?; - } + write!(f, "{prefix}{} mins", mins,)?; + prefix = " "; } if secs != 0 || milliseconds != 0 { @@ -818,23 +787,13 @@ impl Display for MillisecondsFormatter { "" }; - if first_part { - write!( - f, - "{}{}.{:03} secs", - secs_sign, - secs.abs(), - milliseconds.abs() - )?; - } else { - write!( - f, - " {}{}.{:03} secs", - secs_sign, - secs.abs(), - milliseconds.abs() - )?; - } + write!( + f, + "{prefix}{}{}.{:03} secs", + secs_sign, + secs.abs(), + milliseconds.abs() + )?; } Ok(()) diff --git a/parquet-testing b/parquet-testing index 1ba34478f535..aafd3fc9df43 160000 --- a/parquet-testing +++ b/parquet-testing @@ -1 +1 @@ -Subproject commit 1ba34478f535c89382263c42c675a9af4f57f2dd +Subproject commit aafd3fc9df431c2625a514fb46626e5614f1d199 diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 000000000000..47b9dac1d6f2 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly-2024-07-05 \ No newline at end of file diff --git a/testing b/testing index e270341fb5f3..d315f7985207 160000 --- a/testing +++ b/testing @@ -1 +1 @@ -Subproject commit e270341fb5f3ff785410e6286cc42898e9d6a99c +Subproject commit d315f7985207d2d67fc2c8e41053e9d97d573f4b