alamb commented on code in PR #8290:
URL: https://github.com/apache/arrow-rs/pull/8290#discussion_r2337588153


##########
arrow-schema/src/datatype_format.rs:
##########


Review Comment:
   Minor suggestion is to rename this `arrow-schema/src/datatype_display.rs` to 
reflect what it contains



##########
arrow-schema/src/datatype_format.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::{collections::HashMap, fmt};
+
+use crate::DataType;
+
+impl fmt::Display for DataType {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        // NOTE: `Display` and `Debug` formatting are ALWAYS the same,
+        // because we want BOTH to be easy to read AND reversible!
+        write!(f, "{self:?}")
+    }
+}
+
+impl fmt::Debug for DataType {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fn format_metadata(metadata: &HashMap<String, String>) -> String {
+            if metadata.is_empty() {
+                String::new()
+            } else {
+                format!(", metadata: {metadata:?}")
+            }
+        }
+
+        // A lot of these can still be improved a lot.
+        // _Some_ of these can be parsed with `FromStr`, but not all (YET!).
+        // The goal is that the formatting should always be
+        // * Terse and teadable
+        // * Reversible (contain all necessary information to reverse it 
perfectly)
+
+        match &self {
+            Self::Null => write!(f, "Null"),
+            Self::Boolean => write!(f, "Boolean"),
+            Self::Int8 => write!(f, "Int8"),
+            Self::Int16 => write!(f, "Int16"),
+            Self::Int32 => write!(f, "Int32"),
+            Self::Int64 => write!(f, "Int64"),
+            Self::UInt8 => write!(f, "UInt8"),
+            Self::UInt16 => write!(f, "UInt16"),
+            Self::UInt32 => write!(f, "UInt32"),
+            Self::UInt64 => write!(f, "UInt64"),
+            Self::Float16 => write!(f, "Float16"),
+            Self::Float32 => write!(f, "Float32"),
+            Self::Float64 => write!(f, "Float64"),
+            Self::Timestamp(time_unit, timezone) => {
+                write!(f, "Timestamp({time_unit:?}, {timezone:?})")
+            }
+            Self::Date32 => write!(f, "Date32"),
+            Self::Date64 => write!(f, "Date64"),
+            Self::Time32(time_unit) => write!(f, "Time32({time_unit:?})"),
+            Self::Time64(time_unit) => write!(f, "Time64({time_unit:?})"),
+            Self::Duration(time_unit) => write!(f, "Duration({time_unit:?})"),
+            Self::Interval(interval_unit) => write!(f, 
"Interval({interval_unit:?})"),
+            Self::Binary => write!(f, "Binary"),
+            Self::FixedSizeBinary(bytes_per_value) => {
+                write!(f, "FixedSizeBinary({bytes_per_value:?})")
+            }
+            Self::LargeBinary => write!(f, "LargeBinary"),
+            Self::BinaryView => write!(f, "BinaryView"),
+            Self::Utf8 => write!(f, "Utf8"),
+            Self::LargeUtf8 => write!(f, "LargeUtf8"),
+            Self::Utf8View => write!(f, "Utf8View"),
+            Self::ListView(field) => write!(f, "ListView({field:?})"), // 
TODO: make more readable
+            Self::LargeListView(field) => write!(f, 
"LargeListView({field:?})"), // TODO: make more readable
+            Self::List(field) | Self::LargeList(field) => {
+                let type_name = if matches!(self, Self::List(_)) {
+                    "List"
+                } else {
+                    "LargeList"
+                };
+
+                let name = field.name();
+                let maybe_nullable = if field.is_nullable() { "nullable " } 
else { "" };

Review Comment:
   I like this pattern



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to