use std::convert::TryFrom;
use crate::result::{decoding_error, IonError};
use crate::types::IonType;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum IonTypeCode {
NullOrNop, Boolean, PositiveInteger, NegativeInteger, Float, Decimal, Timestamp, Symbol, String, Clob, Blob, List, SExpression, Struct, AnnotationOrIvm, Reserved, }
impl TryFrom<IonTypeCode> for IonType {
type Error = IonError;
fn try_from(ion_type_code: IonTypeCode) -> Result<Self, Self::Error> {
use IonTypeCode::*;
let ion_type = match ion_type_code {
NullOrNop => IonType::Null,
Boolean => IonType::Boolean,
PositiveInteger | NegativeInteger => IonType::Integer,
Float => IonType::Float,
Decimal => IonType::Decimal,
Timestamp => IonType::Timestamp,
Symbol => IonType::Symbol,
String => IonType::String,
Clob => IonType::Clob,
Blob => IonType::Blob,
List => IonType::List,
SExpression => IonType::SExpression,
Struct => IonType::Struct,
_ => {
return decoding_error(format!(
"Attempted to make an IonType from an invalid type code: {:?}",
ion_type_code
));
}
};
Ok(ion_type)
}
}
impl TryFrom<u8> for IonTypeCode {
type Error = IonError;
fn try_from(type_code: u8) -> Result<Self, Self::Error> {
use IonTypeCode::*;
let ion_type_code = match type_code {
0 => NullOrNop,
1 => Boolean,
2 => PositiveInteger,
3 => NegativeInteger,
4 => Float,
5 => Decimal,
6 => Timestamp,
7 => Symbol,
8 => String,
9 => Clob,
10 => Blob,
11 => List,
12 => SExpression,
13 => Struct,
14 => AnnotationOrIvm,
15 => Reserved,
_ => {
return decoding_error(format!("{:?} is not a valid header type code.", type_code));
}
};
Ok(ion_type_code)
}
}
impl IonTypeCode {
pub const fn to_u8(self) -> u8 {
use IonTypeCode::*;
match self {
NullOrNop => 0,
Boolean => 1,
PositiveInteger => 2,
NegativeInteger => 3,
Float => 4,
Decimal => 5,
Timestamp => 6,
Symbol => 7,
String => 8,
Clob => 9,
Blob => 10,
List => 11,
SExpression => 12,
Struct => 13,
AnnotationOrIvm => 14,
Reserved => 15,
}
}
}