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
pub type SymbolId = usize;
pub mod coefficient;
pub mod decimal;
pub mod magnitude;
pub mod timestamp;
use crate::result::{illegal_operation, IonError};
use ion_c_sys::ION_TYPE;
use std::{convert::TryFrom, 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)
}
}
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)),
}
}
}
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(test)]
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(())
}
}