use uuid::Uuid;
use super::{ColumnError, ColumnReader, ColumnWriter};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Col {
I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),
U8(u8),
U16(u16),
U32(u32),
U64(u64),
U128(u128),
Text(String),
Bytes(Vec<u8>),
Uuid(Uuid),
}
impl Col {
const fn kind(&self) -> &'static str {
match self {
Self::I8(_) => "i8",
Self::I16(_) => "i16",
Self::I32(_) => "i32",
Self::I64(_) => "i64",
Self::I128(_) => "i128",
Self::U8(_) => "u8",
Self::U16(_) => "u16",
Self::U32(_) => "u32",
Self::U64(_) => "u64",
Self::U128(_) => "u128",
Self::Text(_) => "text",
Self::Bytes(_) => "bytes",
Self::Uuid(_) => "uuid",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Refusal {
End,
Kind {
wanted: &'static str,
found: &'static str,
},
Octets { wanted: usize, found: usize },
Custom(String),
Trailing(usize),
}
impl Refusal {
fn kind(wanted: &'static str, found: &Col) -> Self {
Self::Kind {
wanted,
found: found.kind(),
}
}
}
impl ColumnError for Refusal {
fn custom<T: core::fmt::Display>(msg: T) -> Self {
Self::Custom(msg.to_string())
}
}
#[derive(Debug, Default)]
pub(crate) struct Fallback(pub(crate) Vec<Col>);
impl Fallback {
pub(crate) fn read(self) -> FallbackRead {
FallbackRead(Cursor::new(self.0))
}
}
impl ColumnWriter for Fallback {
fn i64(&mut self, value: i64) {
self.0.push(Col::I64(value));
}
fn u64(&mut self, value: u64) {
self.0.push(Col::U64(value));
}
fn text(&mut self, value: &str) {
self.0.push(Col::Text(value.to_owned()));
}
fn bytes(&mut self, value: &[u8]) {
self.0.push(Col::Bytes(value.to_vec()));
}
fn uuid(&mut self, value: Uuid) {
self.0.push(Col::Uuid(value));
}
}
#[derive(Debug, Default)]
pub(crate) struct Native(pub(crate) Vec<Col>);
impl Native {
pub(crate) fn read(self) -> NativeRead {
NativeRead(Cursor::new(self.0))
}
}
impl ColumnWriter for Native {
fn i64(&mut self, value: i64) {
self.0.push(Col::I64(value));
}
fn u64(&mut self, value: u64) {
self.0.push(Col::U64(value));
}
fn text(&mut self, value: &str) {
self.0.push(Col::Text(value.to_owned()));
}
fn bytes(&mut self, value: &[u8]) {
self.0.push(Col::Bytes(value.to_vec()));
}
fn uuid(&mut self, value: Uuid) {
self.0.push(Col::Uuid(value));
}
fn i8(&mut self, value: i8) {
self.0.push(Col::I8(value));
}
fn i16(&mut self, value: i16) {
self.0.push(Col::I16(value));
}
fn i32(&mut self, value: i32) {
self.0.push(Col::I32(value));
}
fn i128(&mut self, value: i128) {
self.0.push(Col::I128(value));
}
fn u8(&mut self, value: u8) {
self.0.push(Col::U8(value));
}
fn u16(&mut self, value: u16) {
self.0.push(Col::U16(value));
}
fn u32(&mut self, value: u32) {
self.0.push(Col::U32(value));
}
fn u128(&mut self, value: u128) {
self.0.push(Col::U128(value));
}
}
#[derive(Debug)]
pub(crate) struct Cursor {
cols: Vec<Col>,
at: usize,
}
impl Cursor {
pub(crate) fn new(cols: Vec<Col>) -> Self {
Self { cols, at: 0 }
}
fn take(&mut self) -> Result<&Col, Refusal> {
let at = self.at;
if at >= self.cols.len() {
return Err(Refusal::End);
}
self.at = at + 1;
Ok(&self.cols[at])
}
fn text(&mut self) -> Result<&str, Refusal> {
match self.take()? {
Col::Text(value) => Ok(value),
found => Err(Refusal::kind("text", found)),
}
}
fn bytes(&mut self) -> Result<&[u8], Refusal> {
match self.take()? {
Col::Bytes(value) => Ok(value),
found => Err(Refusal::kind("bytes", found)),
}
}
fn uuid(&mut self) -> Result<Uuid, Refusal> {
match self.take()? {
Col::Uuid(value) => Ok(*value),
found => Err(Refusal::kind("uuid", found)),
}
}
fn fixed<const N: usize>(&mut self) -> Result<[u8; N], Refusal> {
let value = self.bytes()?;
let found = value.len();
<[u8; N]>::try_from(value).map_err(|_| Refusal::Octets { wanted: N, found })
}
fn remaining(&self) -> usize {
self.cols.len() - self.at
}
}
macro_rules! reads {
($($method:ident => $arm:ident as $ty:ty),* $(,)?) => {
impl Cursor {
$(fn $method(&mut self) -> Result<$ty, Refusal> {
match self.take()? {
Col::$arm(value) => Ok(*value),
found => Err(Refusal::kind(stringify!($method), found)),
}
})*
}
};
}
reads!(
i8 => I8 as i8,
i16 => I16 as i16,
i32 => I32 as i32,
i64 => I64 as i64,
i128 => I128 as i128,
u8 => U8 as u8,
u16 => U16 as u16,
u32 => U32 as u32,
u64 => U64 as u64,
u128 => U128 as u128,
);
#[derive(Debug)]
pub(crate) struct FallbackRead(pub(crate) Cursor);
impl ColumnReader for FallbackRead {
type Error = Refusal;
fn i64(&mut self) -> Result<i64, Self::Error> {
self.0.i64()
}
fn u64(&mut self) -> Result<u64, Self::Error> {
self.0.u64()
}
fn text(&mut self) -> Result<&str, Self::Error> {
self.0.text()
}
fn bytes(&mut self) -> Result<&[u8], Self::Error> {
self.0.bytes()
}
fn uuid(&mut self) -> Result<Uuid, Self::Error> {
self.0.uuid()
}
fn fixed<const N: usize>(&mut self) -> Result<[u8; N], Self::Error> {
self.0.fixed()
}
fn finish(self) -> Result<(), Self::Error> {
match self.0.remaining() {
0 => Ok(()),
surplus => Err(Refusal::Trailing(surplus)),
}
}
}
#[derive(Debug)]
pub(crate) struct NativeRead(pub(crate) Cursor);
impl ColumnReader for NativeRead {
type Error = Refusal;
fn i64(&mut self) -> Result<i64, Self::Error> {
self.0.i64()
}
fn u64(&mut self) -> Result<u64, Self::Error> {
self.0.u64()
}
fn text(&mut self) -> Result<&str, Self::Error> {
self.0.text()
}
fn bytes(&mut self) -> Result<&[u8], Self::Error> {
self.0.bytes()
}
fn uuid(&mut self) -> Result<Uuid, Self::Error> {
self.0.uuid()
}
fn fixed<const N: usize>(&mut self) -> Result<[u8; N], Self::Error> {
self.0.fixed()
}
fn i8(&mut self) -> Result<i8, Self::Error> {
self.0.i8()
}
fn i16(&mut self) -> Result<i16, Self::Error> {
self.0.i16()
}
fn i32(&mut self) -> Result<i32, Self::Error> {
self.0.i32()
}
fn i128(&mut self) -> Result<i128, Self::Error> {
self.0.i128()
}
fn u8(&mut self) -> Result<u8, Self::Error> {
self.0.u8()
}
fn u16(&mut self) -> Result<u16, Self::Error> {
self.0.u16()
}
fn u32(&mut self) -> Result<u32, Self::Error> {
self.0.u32()
}
fn u128(&mut self) -> Result<u128, Self::Error> {
self.0.u128()
}
fn finish(self) -> Result<(), Self::Error> {
match self.0.remaining() {
0 => Ok(()),
surplus => Err(Refusal::Trailing(surplus)),
}
}
}