#![allow(dead_code)]
use crate::error::{RpcError, RpcResult};
use std::fmt::Debug;
#[derive(Clone)]
pub struct Decoder<'b> {
inner: minicbor::Decoder<'b>,
}
impl<'b> Decoder<'b> {
pub fn new(bytes: &'b [u8]) -> Self {
Self { inner: minicbor::Decoder::new(bytes) }
}
pub fn bool(&mut self) -> RpcResult<bool> {
Ok(self.inner.bool()?)
}
pub fn u8(&mut self) -> RpcResult<u8> {
Ok(self.inner.u8()?)
}
pub fn u16(&mut self) -> RpcResult<u16> {
Ok(self.inner.u16()?)
}
pub fn u32(&mut self) -> RpcResult<u32> {
Ok(self.inner.u32()?)
}
pub fn u64(&mut self) -> RpcResult<u64> {
Ok(self.inner.u64()?)
}
pub fn i8(&mut self) -> RpcResult<i8> {
Ok(self.inner.i8()?)
}
pub fn i16(&mut self) -> RpcResult<i16> {
Ok(self.inner.i16()?)
}
pub fn i32(&mut self) -> RpcResult<i32> {
Ok(self.inner.i32()?)
}
pub fn i64(&mut self) -> RpcResult<i64> {
Ok(self.inner.i64()?)
}
pub fn f32(&mut self) -> RpcResult<f32> {
Ok(self.inner.f32()?)
}
pub fn f64(&mut self) -> RpcResult<f64> {
Ok(self.inner.f64()?)
}
pub fn char(&mut self) -> RpcResult<char> {
Ok(self.inner.char()?)
}
pub fn str(&mut self) -> RpcResult<&'b str> {
Ok(self.inner.str()?)
}
pub fn null(&mut self) -> RpcResult<()> {
Ok(self.inner.null()?)
}
pub fn bytes(&mut self) -> RpcResult<&'b [u8]> {
Ok(self.inner.bytes()?)
}
pub fn array(&mut self) -> RpcResult<Option<u64>> {
Ok(self.inner.array()?)
}
pub fn fixed_array(&mut self) -> RpcResult<u64> {
self.inner
.array()?
.ok_or_else(|| RpcError::Deser("indefinite array not expected".to_string()))
}
pub fn map(&mut self) -> RpcResult<Option<u64>> {
Ok(self.inner.map()?)
}
pub fn fixed_map(&mut self) -> RpcResult<u64> {
self.inner
.map()?
.ok_or_else(|| RpcError::Deser("indefinite map not expected".to_string()))
}
pub fn datatype(&mut self) -> RpcResult<Type> {
Ok(self.inner.datatype()?.into())
}
pub fn skip(&mut self) -> RpcResult<()> {
Ok(self.inner.skip()?)
}
}
impl<'b> Debug for Decoder<'b> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}
pub trait Decode<'b>: Sized {
fn decode(d: &mut Decoder<'b>) -> Result<Self, RpcError>;
fn nil() -> Option<Self> {
None
}
}
pub trait MDecodeOwned<C>: for<'de> crate::minicbor::Decode<'de, C> {}
impl<T, C> MDecodeOwned<C> for T where T: for<'de> crate::minicbor::Decode<'de, C> {}
pub trait DecodeOwned: for<'de> crate::cbor::Decode<'de> {}
impl<T> DecodeOwned for T where T: for<'de> crate::cbor::Decode<'de> {}
pub use minicbor::encode::Write;
pub struct Encoder<W: Write> {
pub inner: minicbor::Encoder<W>,
}
pub fn vec_encoder(header: bool) -> Encoder<Vec<u8>> {
let mut buf = Vec::new();
if header {
crate::common::MessageFormat::Cbor.write_header(&mut buf).unwrap();
}
Encoder { inner: minicbor::Encoder::new(buf) }
}
impl<W> Encoder<W>
where
W::Error: std::fmt::Display,
W: Write,
{
pub fn new(writer: W) -> Self {
Self { inner: minicbor::Encoder::new(writer) }
}
pub fn bool(&mut self, x: bool) -> RpcResult<&mut Self> {
self.inner.bool(x)?;
Ok(self)
}
pub fn u8(&mut self, x: u8) -> RpcResult<&mut Self> {
self.inner.u8(x)?;
Ok(self)
}
pub fn u16(&mut self, x: u16) -> RpcResult<&mut Self> {
self.inner.u16(x)?;
Ok(self)
}
pub fn u32(&mut self, x: u32) -> RpcResult<&mut Self> {
self.inner.u32(x)?;
Ok(self)
}
pub fn u64(&mut self, x: u64) -> RpcResult<&mut Self> {
self.inner.u64(x)?;
Ok(self)
}
pub fn i8(&mut self, x: i8) -> RpcResult<&mut Self> {
self.inner.i8(x)?;
Ok(self)
}
pub fn i16(&mut self, x: i16) -> RpcResult<&mut Self> {
self.inner.i16(x)?;
Ok(self)
}
pub fn i32(&mut self, x: i32) -> RpcResult<&mut Self> {
self.inner.i32(x)?;
Ok(self)
}
pub fn i64(&mut self, x: i64) -> RpcResult<&mut Self> {
self.inner.i64(x)?;
Ok(self)
}
pub fn f32(&mut self, x: f32) -> RpcResult<&mut Self> {
self.inner.f32(x)?;
Ok(self)
}
pub fn f64(&mut self, x: f64) -> RpcResult<&mut Self> {
self.inner.f64(x)?;
Ok(self)
}
pub fn char(&mut self, x: char) -> RpcResult<&mut Self> {
self.inner.char(x)?;
Ok(self)
}
pub fn bytes(&mut self, x: &[u8]) -> RpcResult<&mut Self> {
self.inner.bytes(x)?;
Ok(self)
}
pub fn str(&mut self, x: &str) -> RpcResult<&mut Self> {
self.inner.str(x)?;
Ok(self)
}
pub fn null(&mut self) -> RpcResult<&mut Self> {
self.inner.null()?;
Ok(self)
}
pub fn array(&mut self, len: u64) -> RpcResult<&mut Self> {
self.inner.array(len)?;
Ok(self)
}
pub fn begin_array(&mut self) -> RpcResult<&mut Self> {
self.inner.begin_array()?;
Ok(self)
}
pub fn map(&mut self, len: u64) -> RpcResult<&mut Self> {
self.inner.map(len)?;
Ok(self)
}
pub fn begin_map(&mut self) -> RpcResult<&mut Self> {
self.inner.begin_map()?;
Ok(self)
}
pub fn begin_bytes(&mut self) -> RpcResult<&mut Self> {
self.inner.begin_bytes()?;
Ok(self)
}
pub fn begin_str(&mut self) -> RpcResult<&mut Self> {
self.inner.begin_str()?;
Ok(self)
}
pub fn end(&mut self) -> RpcResult<&mut Self> {
self.inner.end()?;
Ok(self)
}
pub fn into_inner(self) -> W {
self.inner.into_writer()
}
pub fn tag(&mut self, tag: u64) -> RpcResult<&mut Self> {
self.inner.tag(minicbor::data::Tag::Unassigned(tag))?;
Ok(self)
}
}
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub enum Type {
Bool,
Null,
Undefined,
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
Int,
F16,
F32,
F64,
Simple,
Bytes,
BytesIndef,
String,
StringIndef,
Array,
ArrayIndef,
Map,
MapIndef,
Break,
Tag,
Unknown(u8),
}
use minicbor::data::Type as MT;
impl From<MT> for Type {
fn from(t: MT) -> Type {
match t {
MT::Bool => Type::Bool,
MT::Null => Type::Null,
MT::Undefined => Type::Undefined,
MT::U8 => Type::U8,
MT::U16 => Type::U16,
MT::U32 => Type::U32,
MT::U64 => Type::U64,
MT::I8 => Type::I8,
MT::I16 => Type::I16,
MT::I32 => Type::I32,
MT::I64 => Type::I64,
MT::Int => Type::Int,
MT::F16 => Type::F16,
MT::F32 => Type::F32,
MT::F64 => Type::F64,
MT::Simple => Type::Simple,
MT::Bytes => Type::Bytes,
MT::BytesIndef => Type::BytesIndef,
MT::String => Type::String,
MT::StringIndef => Type::StringIndef,
MT::Array => Type::Array,
MT::ArrayIndef => Type::ArrayIndef,
MT::Map => Type::Map,
MT::MapIndef => Type::MapIndef,
MT::Tag => Type::Tag,
MT::Break => Type::Break,
MT::Unknown(x) => Type::Unknown(x),
}
}
}
impl std::fmt::Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Type::Bool => f.write_str("bool"),
Type::Null => f.write_str("null"),
Type::Undefined => f.write_str("undefined"),
Type::U8 => f.write_str("u8"),
Type::U16 => f.write_str("u16"),
Type::U32 => f.write_str("u32"),
Type::U64 => f.write_str("u64"),
Type::I8 => f.write_str("i8"),
Type::I16 => f.write_str("i16"),
Type::I32 => f.write_str("i32"),
Type::I64 => f.write_str("i64"),
Type::Int => f.write_str("int"),
Type::F16 => f.write_str("f16"),
Type::F32 => f.write_str("f32"),
Type::F64 => f.write_str("f64"),
Type::Simple => f.write_str("simple"),
Type::Bytes => f.write_str("bytes"),
Type::BytesIndef => f.write_str("indefinite bytes"),
Type::String => f.write_str("string"),
Type::StringIndef => f.write_str("indefinite string"),
Type::Array => f.write_str("array"),
Type::ArrayIndef => f.write_str("indefinite array"),
Type::Map => f.write_str("map"),
Type::MapIndef => f.write_str("indefinite map"),
Type::Tag => f.write_str("tag"),
Type::Break => f.write_str("break"),
Type::Unknown(n) => write!(f, "{n:#x}"),
}
}
}
#[inline]
pub fn decode_u8(d: &mut Decoder<'_>) -> RpcResult<u8> {
d.u8()
}
#[inline]
pub fn decode_u16(d: &mut Decoder<'_>) -> RpcResult<u16> {
d.u16()
}
#[inline]
pub fn decode_u32(d: &mut Decoder<'_>) -> RpcResult<u32> {
d.u32()
}
#[inline]
pub fn decode_u64(d: &mut Decoder<'_>) -> RpcResult<u64> {
d.u64()
}
#[inline]
pub fn decode_i8(d: &mut Decoder<'_>) -> RpcResult<i8> {
d.i8()
}
#[inline]
pub fn decode_i16(d: &mut Decoder<'_>) -> RpcResult<i16> {
d.i16()
}
#[inline]
pub fn decode_i32(d: &mut Decoder<'_>) -> RpcResult<i32> {
d.i32()
}
#[inline]
pub fn decode_i64(d: &mut Decoder<'_>) -> RpcResult<i64> {
d.i64()
}
#[inline]
pub fn decode_boolean(d: &mut Decoder<'_>) -> RpcResult<bool> {
d.bool()
}
#[inline]
pub fn decode_string(d: &mut Decoder<'_>) -> RpcResult<String> {
Ok(d.str()?.to_string())
}
#[inline]
pub fn decode_null(d: &mut Decoder<'_>) -> RpcResult<()> {
d.null()
}
#[inline]
pub fn decode_blob(d: &mut Decoder<'_>) -> RpcResult<Vec<u8>> {
Ok(d.bytes()?.to_vec())
}
#[inline]
pub fn decode_byte(d: &mut Decoder<'_>) -> RpcResult<u8> {
d.u8()
}
#[inline]
pub fn decode_char(d: &mut Decoder<'_>) -> RpcResult<char> {
d.char()
}
#[inline]
pub fn decode_integer(d: &mut Decoder<'_>) -> RpcResult<i32> {
d.i32()
}
#[inline]
pub fn decode_long(d: &mut Decoder<'_>) -> RpcResult<i64> {
d.i64()
}
#[inline]
pub fn decode_float(d: &mut Decoder<'_>) -> RpcResult<f32> {
d.f32()
}
#[inline]
pub fn decode_double(d: &mut Decoder<'_>) -> RpcResult<f64> {
d.f64()
}