use super::primitives::*;
#[derive(Debug)]
pub enum DecoderLit {
Bytes(Vec<u8>),
Bool(Vec<u8>),
BoolVec(Vec<u8>),
Int32(Vec<u8>),
Int32Vec(Vec<u8>),
Int64(Vec<u8>),
Int64Vec(Vec<u8>),
UInt32(Vec<u8>),
UInt32Vec(Vec<u8>),
UInt64(Vec<u8>),
UInt64Vec(Vec<u8>),
Float(Vec<u8>),
FloatVec(Vec<u8>),
Double(Vec<u8>),
DoubleVec(Vec<u8>),
SInt32(Vec<u8>),
SInt32Vec(Vec<u8>),
SInt64(Vec<u8>),
SInt64Vec(Vec<u8>),
Fixed32(Vec<u8>),
Fixed32Vec(Vec<u8>),
Fixed64(Vec<u8>),
Fixed64Vec(Vec<u8>),
SFixed32(Vec<u8>),
SFixed32Vec(Vec<u8>),
SFixed64(Vec<u8>),
SFixed64Vec(Vec<u8>),
}
impl From<DecoderLit> for bool {
fn from(lit: DecoderLit) -> Self {
let mut dst = false;
match lit {
DecoderLit::Bool(byt) => decode_bool(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<bool> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::BoolVec(byt) => decode_bool_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for i32 {
fn from(lit: DecoderLit) -> Self {
let mut dst = 0i32;
match lit {
DecoderLit::Int32(byt) => decode_int32(&byt, &mut dst).unwrap_or(0),
DecoderLit::SInt32(byt) => decode_sint32(&byt, &mut dst).unwrap_or(0),
DecoderLit::SFixed32(byt) => decode_sfixed32(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<i32> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::Int32Vec(byt) => decode_int32_vec(&byt, &mut dst).unwrap_or(0),
DecoderLit::SInt32Vec(byt) => decode_sint32_vec(&byt, &mut dst).unwrap_or(0),
DecoderLit::SFixed32Vec(byt) => decode_sfixed32_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for i64 {
fn from(lit: DecoderLit) -> Self {
let mut dst = 0i64;
match lit {
DecoderLit::Int64(byt) => decode_int64(&byt, &mut dst).unwrap_or(0),
DecoderLit::SInt64(byt) => decode_sint64(&byt, &mut dst).unwrap_or(0),
DecoderLit::SFixed64(byt) => decode_sfixed64(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<i64> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::Int64Vec(byt) => decode_int64_vec(&byt, &mut dst).unwrap_or(0),
DecoderLit::SInt64Vec(byt) => decode_sint64_vec(&byt, &mut dst).unwrap_or(0),
DecoderLit::SFixed64Vec(byt) => decode_sfixed64_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for u32 {
fn from(lit: DecoderLit) -> Self {
let mut dst = 0u32;
match lit {
DecoderLit::UInt32(byt) => decode_uint32(&byt, &mut dst).unwrap_or(0),
DecoderLit::Fixed32(byt) => decode_fixed32(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<u32> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::UInt32Vec(byt) => decode_uint32_vec(&byt, &mut dst).unwrap_or(0),
DecoderLit::Fixed32Vec(byt) => decode_fixed32_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for u64 {
fn from(lit: DecoderLit) -> Self {
let mut dst = 0u64;
match lit {
DecoderLit::UInt64(byt) => decode_uint64(&byt, &mut dst).unwrap_or(0),
DecoderLit::Fixed64(byt) => decode_fixed64(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<u64> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::UInt64Vec(byt) => decode_uint64_vec(&byt, &mut dst).unwrap_or(0),
DecoderLit::Fixed64Vec(byt) => decode_fixed64_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for f32 {
fn from(lit: DecoderLit) -> Self {
let mut dst = 0.0f32;
match lit {
DecoderLit::Float(byt) => decode_float(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<f32> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::FloatVec(byt) => decode_float_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for f64 {
fn from(lit: DecoderLit) -> Self {
let mut dst = 0.0f64;
match lit {
DecoderLit::Double(byt) => decode_double(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<f64> {
fn from(lit: DecoderLit) -> Self {
let mut dst = vec![];
match lit {
DecoderLit::DoubleVec(byt) => decode_double_vec(&byt, &mut dst).unwrap_or(0),
_ => return dst,
};
dst
}
}
impl From<DecoderLit> for Vec<u8> {
fn from(lit: DecoderLit) -> Self {
match lit {
DecoderLit::Bytes(byt) => byt,
_ => Vec::new(),
}
}
}
impl From<DecoderLit> for String {
fn from(lit: DecoderLit) -> Self {
match lit {
DecoderLit::Bytes(byt) => {
match String::from_utf8(byt) {
Ok(s) => s,
_ => String::new(),
}
},
_ => String::new(),
}
}
}