Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Refactored (internal) fmt #842

Merged
merged 2 commits into from Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/array/binary/fmt.rs
@@ -0,0 +1,26 @@
use std::fmt::{Debug, Formatter, Result, Write};

use super::super::fmt::write_vec;
use super::super::Offset;
use super::BinaryArray;

pub fn write_value<O: Offset, W: Write>(array: &BinaryArray<O>, index: usize, f: &mut W) -> Result {
let bytes = array.value(index);
let writer = |f: &mut W, index| write!(f, "{}", bytes[index]);

write_vec(f, writer, None, bytes.len(), "None", false)
}

impl<O: Offset> Debug for BinaryArray<O> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let writer = |f: &mut Formatter, index| write_value(self, index, f);

let head = if O::is_large() {
"LargeBinaryArray"
} else {
"BinaryArray"
};
write!(f, "{}", head)?;
write_vec(f, writer, self.validity(), self.len(), "None", false)
}
}
17 changes: 2 additions & 15 deletions src/array/binary/mod.rs
Expand Up @@ -6,12 +6,12 @@ use crate::{
};

use super::{
display_fmt, display_helper,
specification::{check_offsets_minimal, try_check_offsets},
Array, GenericBinaryArray, Offset,
};

mod ffi;
pub(super) mod fmt;
mod iterator;
pub use iterator::*;
mod from;
Expand All @@ -23,7 +23,7 @@ pub use mutable::*;
/// The following invariants hold:
/// * Two consecutives `offsets` casted (`as`) to `usize` are valid slices of `values`.
/// * `len` is equal to `validity.len()`, when defined.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct BinaryArray<O: Offset> {
data_type: DataType,
offsets: Buffer<O>,
Expand Down Expand Up @@ -274,19 +274,6 @@ impl<O: Offset> Array for BinaryArray<O> {
}
}

impl<O: Offset> std::fmt::Display for BinaryArray<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let a = |x: &[u8]| display_helper(x.iter().map(|x| Some(format!("{:b}", x)))).join(" ");
let iter = self.iter().map(|x| x.map(a));
let head = if O::is_large() {
"LargeBinaryArray"
} else {
"BinaryArray"
};
display_fmt(iter, head, f, false)
}
}

unsafe impl<O: Offset> GenericBinaryArray<O> for BinaryArray<O> {
#[inline]
fn values(&self) -> &[u8] {
Expand Down
17 changes: 17 additions & 0 deletions src/array/boolean/fmt.rs
@@ -0,0 +1,17 @@
use std::fmt::{Debug, Formatter, Result, Write};

use super::super::fmt::write_vec;
use super::BooleanArray;

pub fn write_value<W: Write>(array: &BooleanArray, index: usize, f: &mut W) -> Result {
write!(f, "{}", array.value(index))
}

impl Debug for BooleanArray {
fn fmt(&self, f: &mut Formatter) -> Result {
let writer = |f: &mut Formatter, index| write_value(self, index, f);

write!(f, "BooleanArray")?;
write_vec(f, writer, self.validity(), self.len(), "None", false)
}
}
9 changes: 2 additions & 7 deletions src/array/boolean/mod.rs
Expand Up @@ -4,9 +4,10 @@ use crate::{
};
use either::Either;

use super::{display_fmt, Array};
use super::Array;

mod ffi;
pub(super) mod fmt;
mod from;
mod iterator;
mod mutable;
Expand Down Expand Up @@ -204,9 +205,3 @@ impl Array for BooleanArray {
Box::new(self.with_validity(validity))
}
}

impl std::fmt::Debug for BooleanArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_fmt(self.iter(), "BooleanArray", f, false)
}
}
32 changes: 32 additions & 0 deletions src/array/dictionary/fmt.rs
@@ -0,0 +1,32 @@
use std::fmt::{Debug, Formatter, Result, Write};

use crate::array::Array;

use super::super::fmt::{get_display, write_vec};
use super::{DictionaryArray, DictionaryKey};

pub fn write_value<K: DictionaryKey, W: Write>(
array: &DictionaryArray<K>,
index: usize,
null: &'static str,
f: &mut W,
) -> Result {
let keys = array.keys();
let values = array.values();

if keys.is_valid(index) {
let key = keys.value(index).to_usize().unwrap();
get_display(values.as_ref(), null)(f, key)
} else {
write!(f, "{}", null)
}
}

impl<K: DictionaryKey> Debug for DictionaryArray<K> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let writer = |f: &mut Formatter, index| write_value(self, index, "None", f);

write!(f, "DictionaryArray")?;
write_vec(f, writer, self.validity(), self.len(), "None", false)
}
}
19 changes: 3 additions & 16 deletions src/array/dictionary/mod.rs
Expand Up @@ -8,13 +8,13 @@ use crate::{
};

mod ffi;
pub(super) mod fmt;
mod iterator;
mod mutable;
pub use iterator::*;
pub use mutable::*;

use super::display::get_value_display;
use super::{display_fmt, new_empty_array, primitive::PrimitiveArray, Array};
use super::{new_empty_array, primitive::PrimitiveArray, Array};
use crate::scalar::NullScalar;

/// Trait denoting [`NativeType`]s that can be used as keys of a dictionary.
Expand Down Expand Up @@ -50,7 +50,7 @@ impl DictionaryKey for u64 {

/// An [`Array`] whose values are encoded by keys. This [`Array`] is useful when the cardinality of
/// values is low compared to the length of the [`Array`].
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct DictionaryArray<K: DictionaryKey> {
data_type: DataType,
keys: PrimitiveArray<K>,
Expand Down Expand Up @@ -203,16 +203,3 @@ impl<K: DictionaryKey> Array for DictionaryArray<K> {
Box::new(self.with_validity(validity))
}
}

impl<K: DictionaryKey> std::fmt::Display for DictionaryArray<K>
where
PrimitiveArray<K>: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = get_value_display(self);
let new_lines = false;
let head = &format!("{:?}", self.data_type());
let iter = self.iter().enumerate().map(|(i, x)| x.map(|_| display(i)));
display_fmt(iter, head, f, new_lines)
}
}