1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
pub type SymbolId = usize;
pub mod coefficient;
pub mod decimal;
pub mod integer;
pub mod magnitude;
pub mod timestamp;
#[cfg(feature = "ion_c")]
use {
crate::result::{illegal_operation, IonError},
ion_c_sys::ION_TYPE,
std::convert::TryFrom,
};
use std::fmt;
#[derive(Debug, PartialEq, Eq, PartialOrd, Copy, Clone)]
pub enum IonType {
Null,
Boolean,
Integer,
Float,
Decimal,
Timestamp,
Symbol,
String,
Clob,
Blob,
List,
SExpression,
Struct,
}
impl fmt::Display for IonType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
IonType::Null => "null",
IonType::Boolean => "boolean",
IonType::Integer => "integer",
IonType::Float => "float",
IonType::Decimal => "decimal",
IonType::Timestamp => "timestamp",
IonType::Symbol => "symbol",
IonType::String => "string",
IonType::Clob => "clob",
IonType::Blob => "blob",
IonType::List => "list",
IonType::SExpression => "sexp",
IonType::Struct => "struct",
}
)
}
}
impl IonType {
pub fn is_container(&self) -> bool {
use IonType::*;
matches!(self, List | SExpression | Struct)
}
}
#[derive(Debug, PartialEq)]
pub(crate) enum ContainerType {
TopLevel,
SExpression,
List,
Struct,
}
impl Default for ContainerType {
fn default() -> Self {
ContainerType::TopLevel
}
}
fn num_decimal_digits_in_u64(value: u64) -> u64 {
if value == 0 {
return 1;
}
let log_base_ten = (value as f64).log10();
let count = log_base_ten.ceil();
if log_base_ten == count {
count as u64 + 1
} else {
count as u64
}
}
#[cfg(feature = "ion_c")]
impl TryFrom<ION_TYPE> for IonType {
type Error = IonError;
fn try_from(value: ION_TYPE) -> Result<Self, Self::Error> {
use IonType::*;
match value {
ion_c_sys::ION_TYPE_NULL => Ok(Null),
ion_c_sys::ION_TYPE_BOOL => Ok(Boolean),
ion_c_sys::ION_TYPE_INT => Ok(Integer),
ion_c_sys::ION_TYPE_FLOAT => Ok(Float),
ion_c_sys::ION_TYPE_DECIMAL => Ok(Decimal),
ion_c_sys::ION_TYPE_TIMESTAMP => Ok(Timestamp),
ion_c_sys::ION_TYPE_SYMBOL => Ok(Symbol),
ion_c_sys::ION_TYPE_STRING => Ok(String),
ion_c_sys::ION_TYPE_CLOB => Ok(Clob),
ion_c_sys::ION_TYPE_BLOB => Ok(Blob),
ion_c_sys::ION_TYPE_LIST => Ok(List),
ion_c_sys::ION_TYPE_SEXP => Ok(SExpression),
ion_c_sys::ION_TYPE_STRUCT => Ok(Struct),
_ => illegal_operation(format!("Could not convert Ion C ION_TYPE: ${:?}", value)),
}
}
}
#[cfg(feature = "ion_c")]
impl From<IonType> for ION_TYPE {
fn from(ion_type: IonType) -> ION_TYPE {
use IonType::*;
match ion_type {
Null => ion_c_sys::ION_TYPE_NULL,
Boolean => ion_c_sys::ION_TYPE_BOOL,
Integer => ion_c_sys::ION_TYPE_INT,
Float => ion_c_sys::ION_TYPE_FLOAT,
Decimal => ion_c_sys::ION_TYPE_DECIMAL,
Timestamp => ion_c_sys::ION_TYPE_TIMESTAMP,
Symbol => ion_c_sys::ION_TYPE_SYMBOL,
String => ion_c_sys::ION_TYPE_STRING,
Clob => ion_c_sys::ION_TYPE_CLOB,
Blob => ion_c_sys::ION_TYPE_BLOB,
List => ion_c_sys::ION_TYPE_LIST,
SExpression => ion_c_sys::ION_TYPE_SEXP,
Struct => ion_c_sys::ION_TYPE_STRUCT,
}
}
}
#[cfg(all(test, feature = "ion_c"))]
mod type_test {
use super::*;
use crate::result::IonResult;
use ion_c_sys::reader::*;
use rstest::*;
use std::convert::TryInto;
use IonType::*;
#[rstest]
#[case::null("null", Null)]
#[case::bool("true", Boolean)]
#[case::int("-5", Integer)]
#[case::float("100e0", Float)]
#[case::decimal("1.1", Decimal)]
#[case::timestamp("2014-10-16T", Timestamp)]
#[case::symbol("foobar", Symbol)]
#[case::string("'''foosball'''", String)]
#[case::clob("{{'''moon'''}}", Clob)]
#[case::blob("{{Z29vZGJ5ZQ==}}", Blob)]
#[case::list("[1, 2, 3]", List)]
#[case::sexp("(a b c)", SExpression)]
#[case::structure("{a:1, b:2}", Struct)]
fn ionc_type_conversion(
#[case] data: &str,
#[case] expected_ion_type: IonType,
) -> IonResult<()> {
let mut reader: IonCReaderHandle = data.try_into()?;
let ion_type: IonType = reader.next()?.try_into()?;
assert_eq!(expected_ion_type, ion_type);
let eof_result: IonResult<IonType> = reader.next()?.try_into();
match eof_result {
Err(IonError::IllegalOperation { operation: _ }) => {}
otherwise => panic!("Unexpected conversion of EOF: {:?}", otherwise),
};
Ok(())
}
}