|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
| 4 | +use std::fmt::{Display, Formatter}; |
4 | 5 | use std::hash::Hash; |
5 | 6 | use std::sync::Arc; |
6 | 7 |
|
@@ -134,7 +135,7 @@ struct FieldDTypeDeVisitor; |
134 | 135 | impl<'de> serde::de::Visitor<'de> for FieldDTypeDeVisitor { |
135 | 136 | type Value = FieldDTypeInner; |
136 | 137 |
|
137 | | - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 138 | + fn expecting(&self, f: &mut Formatter) -> std::fmt::Result { |
138 | 139 | write!(f, "variant identifier") |
139 | 140 | } |
140 | 141 |
|
@@ -190,14 +191,28 @@ impl<'de> serde::de::Visitor<'de> for FieldDTypeDeVisitor { |
190 | 191 | pub struct StructFields(Arc<StructFieldsInner>); |
191 | 192 |
|
192 | 193 | 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 { |
194 | 195 | f.debug_struct("StructFields") |
195 | 196 | .field("names", &self.0.names) |
196 | 197 | .field("dtypes", &self.0.dtypes) |
197 | 198 | .finish() |
198 | 199 | } |
199 | 200 | } |
200 | 201 |
|
| 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 | + |
201 | 216 | #[derive(PartialEq, Eq, Hash, Default)] |
202 | 217 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
203 | 218 | struct StructFieldsInner { |
@@ -501,4 +516,29 @@ mod test { |
501 | 516 | let err = StructFields::disjoint_merge(&sf1, &sf1).err().unwrap(); |
502 | 517 | assert!(err.to_string().contains("duplicate names"),); |
503 | 518 | } |
| 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 | + } |
504 | 544 | } |
0 commit comments