substrait_validator/output/
primitive_data.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Module for primitive data elements.
4//!
5//! The [`PrimitiveData`] enum is used to represent primitive data in the
6//! input, for use in the leaf nodes of the tree.
7
8/// Enumeration for representing any type of primitive data that can be stored
9/// in YAML or protobuf.
10#[derive(Clone, Debug, PartialEq)]
11pub enum PrimitiveData {
12    /// Used for nulls (YAML only).
13    Null,
14
15    /// Used for booleans.
16    Bool(bool),
17
18    /// Used for unsigned integers.
19    Unsigned(u64),
20
21    /// Used for signed integers.
22    Signed(i64),
23
24    /// Used for floating-point values.
25    Float(f64),
26
27    /// Used for UTF-8 strings.
28    String(String),
29
30    /// Used for bytestrings.
31    Bytes(Vec<u8>),
32
33    /// Used for enumerations (protobuf only).
34    Enum(&'static str),
35
36    /// Used for Any messages (protobuf only).
37    Any(prost_types::Any),
38}
39
40fn hexdump(f: &mut std::fmt::Formatter<'_>, x: &[u8]) -> std::fmt::Result {
41    for (i, b) in x.iter().enumerate() {
42        if i > 0 {
43            write!(f, " ")?;
44        }
45        write!(f, "{:02X}", b)?;
46    }
47    Ok(())
48}
49
50impl std::fmt::Display for PrimitiveData {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            PrimitiveData::Null => write!(f, "null"),
54            PrimitiveData::Bool(true) => write!(f, "true"),
55            PrimitiveData::Bool(false) => write!(f, "false"),
56            PrimitiveData::Unsigned(x) => write!(f, "{x}"),
57            PrimitiveData::Signed(x) => write!(f, "{x}"),
58            PrimitiveData::Float(x) => write!(f, "{x}"),
59            PrimitiveData::String(x) => write!(f, "{x:?}"),
60            PrimitiveData::Bytes(x) => hexdump(f, x),
61            PrimitiveData::Enum(x) => write!(f, "{x}"),
62            PrimitiveData::Any(x) => {
63                write!(f, "{}(", x.type_url)?;
64                hexdump(f, &x.value)?;
65                write!(f, ")")
66            }
67        }
68    }
69}