• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16269921526

14 Jul 2025 02:40PM UTC coverage: 81.518% (+0.05%) from 81.464%
16269921526

Pull #3866

github

web-flow
Merge c0af6ecae into b1a8d1d4f
Pull Request #3866: feat: display

139 of 148 new or added lines in 3 files covered. (93.92%)

87 existing lines in 1 file now uncovered.

46256 of 56743 relevant lines covered (81.52%)

146241.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.25
/vortex-array/src/array/display.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::fmt::Display;
5

6
use itertools::Itertools as _;
7
use vortex_error::VortexExpect as _;
8

9
use crate::Array;
10

11
pub struct DisplayArray<'a>(pub &'a dyn Array);
12

13
impl Display for DisplayArray<'_> {
14
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150✔
15
        self.0.fmt_as(f, &DisplayOptions::default())
150✔
16
    }
150✔
17
}
18

19
pub enum DisplayOptions {
20
    /// `EncodingId(dtype, len=123)`
21
    MetadataOnly,
22
    /// `[1i32, 2i32, 3i32]`
23
    CommaSeparatedScalars { space_after_comma: bool },
24
}
25

26
impl Default for DisplayOptions {
27
    fn default() -> Self {
150✔
28
        Self::CommaSeparatedScalars {
150✔
29
            space_after_comma: true,
150✔
30
        }
150✔
31
    }
150✔
32
}
33

34
pub struct DisplayArrayAs<'a>(pub &'a dyn Array, pub &'a DisplayOptions);
35

36
impl Display for DisplayArrayAs<'_> {
NEW
37
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
NEW
38
        self.0.fmt_as(f, self.1)
×
NEW
39
    }
×
40
}
41

42
impl dyn Array + '_ {
43
    pub fn fmt_as(
980✔
44
        &self,
980✔
45
        f: &mut std::fmt::Formatter,
980✔
46
        options: &DisplayOptions,
980✔
47
    ) -> std::fmt::Result {
980✔
48
        match options {
980✔
49
            DisplayOptions::MetadataOnly => {
50
                write!(
830✔
51
                    f,
830✔
52
                    "{}({}, len={})",
830✔
53
                    self.encoding_id(),
830✔
54
                    self.dtype(),
830✔
55
                    self.len()
830✔
56
                )
830✔
57
            }
58
            DisplayOptions::CommaSeparatedScalars { space_after_comma } => {
150✔
59
                write!(f, "[")?;
150✔
60
                let sep = if *space_after_comma { ", " } else { "," };
150✔
61
                write!(
150✔
62
                    f,
150✔
63
                    "{}",
150✔
64
                    (0..self.len())
150✔
65
                        .map(|i| self.scalar_at(i).vortex_expect("index is in bounds"))
989✔
66
                        .format(sep)
150✔
67
                )?;
150✔
68
                write!(f, "]")
150✔
69
            }
70
        }
71
    }
980✔
72
}
73

74
#[cfg(test)]
75
mod test {
76
    use vortex_buffer::{Buffer, buffer};
77
    use vortex_dtype::FieldNames;
78

79
    use crate::IntoArray as _;
80
    use crate::array::Array;
81
    use crate::arrays::{BoolArray, ListArray, StructArray};
82
    use crate::validity::Validity;
83

84
    #[test]
85
    fn test_primitive() {
1✔
86
        let x = Buffer::<u32>::empty().into_array();
1✔
87
        assert_eq!(x.display().to_string(), "[]");
1✔
88

89
        let x = buffer![1].into_array();
1✔
90
        assert_eq!(x.display().to_string(), "[1i32]");
1✔
91

92
        let x = buffer![1, 2, 3, 4].into_array();
1✔
93
        assert_eq!(x.display().to_string(), "[1i32, 2i32, 3i32, 4i32]");
1✔
94
    }
1✔
95

96
    #[test]
97
    fn test_empty_struct() {
1✔
98
        let s = StructArray::try_new(
1✔
99
            FieldNames::from(vec![]),
1✔
100
            vec![],
1✔
101
            3,
1✔
102
            Validity::Array(BoolArray::from_iter([true, false, true]).into_array()),
1✔
103
        )
1✔
104
        .unwrap()
1✔
105
        .into_array();
1✔
106
        assert_eq!(s.display().to_string(), "[{}, null, {}]");
1✔
107
    }
1✔
108

109
    #[test]
110
    fn test_simple_struct() {
1✔
111
        let s = StructArray::from_fields(&[
1✔
112
            ("x", buffer![1, 2, 3, 4].into_array()),
1✔
113
            ("y", buffer![-1, -2, -3, -4].into_array()),
1✔
114
        ])
1✔
115
        .unwrap()
1✔
116
        .into_array();
1✔
117
        assert_eq!(
1✔
118
            s.display().to_string(),
1✔
119
            "[{x: 1i32, y: -1i32}, {x: 2i32, y: -2i32}, {x: 3i32, y: -3i32}, {x: 4i32, y: -4i32}]"
1✔
120
        );
1✔
121
    }
1✔
122

123
    #[test]
124
    fn test_list() {
1✔
125
        let x = ListArray::try_new(
1✔
126
            buffer![1, 2, 3, 4].into_array(),
1✔
127
            buffer![0, 0, 1, 1, 2, 4].into_array(),
1✔
128
            Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()),
1✔
129
        )
1✔
130
        .unwrap()
1✔
131
        .into_array();
1✔
132
        assert_eq!(
1✔
133
            x.display().to_string(),
1✔
134
            "[[], [1i32], null, [2i32], [3i32, 4i32]]"
1✔
135
        );
1✔
136
    }
1✔
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc