cel_cxx/types/
display.rs

1use super::*;
2use itertools::Itertools;
3use std::fmt::Display;
4
5impl std::fmt::Display for ValueType {
6    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7        match self {
8            Self::Null
9            | Self::Unknown
10            | Self::Error
11            | Self::Any
12            | Self::Dyn
13            | Self::Bool
14            | Self::Int
15            | Self::Uint
16            | Self::Double
17            | Self::String
18            | Self::Bytes
19            | Self::BoolWrapper
20            | Self::IntWrapper
21            | Self::UintWrapper
22            | Self::DoubleWrapper
23            | Self::StringWrapper
24            | Self::BytesWrapper
25            | Self::Duration
26            | Self::Timestamp => Display::fmt(&self.kind(), f),
27            Self::Struct(struct_) => Display::fmt(struct_, f),
28            Self::List(list) => Display::fmt(list, f),
29            Self::Map(map) => Display::fmt(map, f),
30            Self::Type(type_type) => Display::fmt(type_type, f),
31            Self::Opaque(opaque) => Display::fmt(opaque, f),
32            Self::Optional(optional) => Display::fmt(optional, f),
33            Self::TypeParam(type_param) => Display::fmt(type_param, f),
34            Self::Function(function) => Display::fmt(function, f),
35            Self::Enum(enum_) => Display::fmt(enum_, f),
36        }
37    }
38}
39
40impl std::fmt::Display for StructType {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{}", self.name)
43    }
44}
45
46impl std::fmt::Display for ListType {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "list<{}>", self.element)
49    }
50}
51
52impl std::fmt::Display for MapType {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        write!(f, "map<{}, {}>", self.key, self.value)
55    }
56}
57
58impl std::fmt::Display for TypeType {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        if let Some(parameter) = &self.parameter {
61            write!(f, "type<{parameter}>")
62        } else {
63            write!(f, "type")
64        }
65    }
66}
67
68impl std::fmt::Display for OpaqueType {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        if self.parameters().is_empty() {
71            write!(f, "{}", self.name())
72        } else {
73            write!(
74                f,
75                "{}<{}>",
76                self.name(),
77                self.parameters().iter().format(", ")
78            )
79        }
80    }
81}
82
83impl std::fmt::Display for OptionalType {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(f, "optional<{}>", self.parameter)
86    }
87}
88
89impl std::fmt::Display for TypeParamType {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        write!(f, "{}", self.name)
92    }
93}
94
95impl std::fmt::Display for FunctionType {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        write!(
98            f,
99            "({}) -> {}",
100            self.arguments.iter().format(", "),
101            self.result
102        )
103    }
104}
105
106impl std::fmt::Display for EnumType {
107    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108        write!(f, "{}", self.name)
109    }
110}
111
112impl std::fmt::Display for MapKeyType {
113    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
114        match self {
115            Self::Bool => f.write_str("bool"),
116            Self::Int => f.write_str("int"),
117            Self::Uint => f.write_str("uint"),
118            Self::String => f.write_str("string"),
119            Self::Dyn => f.write_str("dyn"),
120            Self::TypeParam(type_param) => Display::fmt(type_param, f),
121        }
122    }
123}