Skip to content

Commit 7dc04c3

Browse files
blagininclaude
andauthored
feat: implement Display for StructFields (#4255)
Add Display trait implementation for StructFields that formats struct fields as {field1=type1, field2=type2, ...}, matching the format used by DType::Struct. Includes test coverage for empty, simple, and nested struct fields. 🤖 Generated with [Claude Code](https://claude.ai/code) Signed-off-by: blaginin <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent 4a23f65 commit 7dc04c3

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

vortex-dtype/src/struct_.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::fmt::{Display, Formatter};
45
use std::hash::Hash;
56
use std::sync::Arc;
67

@@ -134,7 +135,7 @@ struct FieldDTypeDeVisitor;
134135
impl<'de> serde::de::Visitor<'de> for FieldDTypeDeVisitor {
135136
type Value = FieldDTypeInner;
136137

137-
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
138+
fn expecting(&self, f: &mut Formatter) -> std::fmt::Result {
138139
write!(f, "variant identifier")
139140
}
140141

@@ -190,14 +191,28 @@ impl<'de> serde::de::Visitor<'de> for FieldDTypeDeVisitor {
190191
pub struct StructFields(Arc<StructFieldsInner>);
191192

192193
impl std::fmt::Debug for StructFields {
193-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
194+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
194195
f.debug_struct("StructFields")
195196
.field("names", &self.0.names)
196197
.field("dtypes", &self.0.dtypes)
197198
.finish()
198199
}
199200
}
200201

202+
impl Display for StructFields {
203+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
204+
write!(
205+
f,
206+
"{{{}}}",
207+
self.names()
208+
.iter()
209+
.zip(self.fields())
210+
.map(|(n, dt)| format!("{n}={dt}"))
211+
.join(", ")
212+
)
213+
}
214+
}
215+
201216
#[derive(PartialEq, Eq, Hash, Default)]
202217
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
203218
struct StructFieldsInner {
@@ -501,4 +516,29 @@ mod test {
501516
let err = StructFields::disjoint_merge(&sf1, &sf1).err().unwrap();
502517
assert!(err.to_string().contains("duplicate names"),);
503518
}
519+
520+
#[test]
521+
fn test_display() {
522+
let fields = StructFields::from_iter([
523+
("name", DType::Utf8(Nullability::NonNullable)),
524+
("age", DType::Primitive(PType::I32, Nullability::Nullable)),
525+
("active", DType::Bool(Nullability::NonNullable)),
526+
]);
527+
528+
assert_eq!(fields.to_string(), "{name=utf8, age=i32?, active=bool}");
529+
530+
// Test empty struct
531+
let empty = StructFields::empty();
532+
assert_eq!(empty.to_string(), "{}");
533+
534+
// Test nested struct
535+
let nested = StructFields::from_iter([
536+
("id", DType::Primitive(PType::U64, Nullability::NonNullable)),
537+
("data", DType::Struct(fields, Nullability::Nullable)),
538+
]);
539+
assert_eq!(
540+
nested.to_string(),
541+
"{id=u64, data={name=utf8, age=i32?, active=bool}?}"
542+
);
543+
}
504544
}

0 commit comments

Comments
 (0)