mod decode;
mod encode;
mod geometry;
mod map;
mod read;
pub mod schema;
#[cfg(test)]
mod tests;
pub mod varint;
pub use decode::{
bigdecimal_payload, decimal_payload, decode, decode_datetime_payload, decode_duration_payload,
decode_key, decode_key_nested, decode_nested, validate, validate_entries_nested,
validate_key_nested, validate_nested,
};
pub use encode::{
Encoder, Open, encode, put_bigdecimal_payload, put_decimal_payload, with_encoder,
};
pub use map::{Item, ListRef, MapRef};
pub use read::{ArrayIter, ArrayRef, Kind, ObjectIter, ObjectRef, OrderedObjectRef, ValueRef};
use crate::val::MAX_VALUE_DEPTH;
pub mod tag {
pub const NONE: u8 = 0x00;
pub const NULL: u8 = 0x01;
pub const FALSE: u8 = 0x02;
pub const TRUE: u8 = 0x03;
pub const INT: u8 = 0x04;
pub const FLOAT: u8 = 0x05;
pub const DECIMAL: u8 = 0x06;
pub const STRING: u8 = 0x07;
pub const DURATION: u8 = 0x08;
pub const DATETIME: u8 = 0x09;
pub const UUID: u8 = 0x0A;
pub const BYTES: u8 = 0x0B;
pub const TABLE: u8 = 0x0C;
pub const RECORD_ID: u8 = 0x0D;
pub const FILE: u8 = 0x0E;
pub const REGEX: u8 = 0x0F;
pub const RANGE: u8 = 0x10;
pub const GEOMETRY: u8 = 0x11;
pub const F64_ARRAY: u8 = 0x12;
pub const F32_ARRAY: u8 = 0x13;
pub const ARRAY: u8 = 0x14;
pub const SET: u8 = 0x17;
pub const OBJECT: u8 = 0x1A;
pub const I8_ARRAY: u8 = 0x1D;
pub const I16_ARRAY: u8 = 0x1E;
pub const I32_ARRAY: u8 = 0x1F;
pub const I64_ARRAY: u8 = 0x20;
pub const MAP: u8 = 0x26;
pub const I8: u8 = 0x29;
pub const U8: u8 = 0x2A;
pub const I16: u8 = 0x2B;
pub const U16: u8 = 0x2C;
pub const I32: u8 = 0x2D;
pub const U32: u8 = 0x2E;
pub const U64: u8 = 0x2F;
pub const F16: u8 = 0x30;
pub const F32: u8 = 0x31;
pub const DATE: u8 = 0x32;
pub const TIME: u8 = 0x33;
pub const TIMESTAMP: u8 = 0x34;
pub const INTERVAL: u8 = 0x35;
pub const BIGDECIMAL: u8 = 0x36;
pub const SMALL_INT: u8 = 0x80;
pub const SMALL_INT_MAX: i64 = 126;
pub const UNBOUNDED: u8 = 0;
pub const INCLUDED: u8 = 1;
pub const EXCLUDED: u8 = 2;
pub const GEO_POINT: u8 = 0;
pub const GEO_LINE: u8 = 1;
pub const GEO_POLYGON: u8 = 2;
pub const GEO_MULTIPOINT: u8 = 3;
pub const GEO_MULTILINE: u8 = 4;
pub const GEO_MULTIPOLYGON: u8 = 5;
pub const GEO_COLLECTION: u8 = 6;
pub mod reserved {
pub const U8_ARRAY: u8 = 0x21;
pub const U16_ARRAY: u8 = 0x22;
pub const U32_ARRAY: u8 = 0x23;
pub const F16_ARRAY: u8 = 0x24;
pub const BF16_ARRAY: u8 = 0x25;
}
#[inline(always)]
pub const fn width(tag: u8, base: u8) -> usize {
1 << (tag - base)
}
}
pub const DEPTH_LIMIT: usize = MAX_VALUE_DEPTH + 16;
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("a stored value ends before its encoding does")]
Truncated,
#[error("a stored value has the unknown tag {0:#04x}")]
UnknownTag(u8),
#[error("a stored value is malformed: {0}")]
Malformed(&'static str),
#[error("a stored value nests deeper than {0} levels")]
TooDeep(usize),
#[error("the value cannot be stored: {0}")]
Unstorable(&'static str),
#[error("a closure cannot be stored")]
Closure,
}
#[inline(always)]
pub fn read_clen(b: &[u8]) -> Result<(usize, usize), Error> {
match b.first() {
Some(&n) if n < 0xFF => Ok((n as usize, 1)),
Some(_) => match b.get(1..).and_then(<[u8]>::first_chunk::<4>) {
Some(n) => Ok((u32::from_le_bytes(*n) as usize, 5)),
None => Err(Error::Truncated),
},
None => Err(Error::Truncated),
}
}
#[inline(always)]
pub fn split_clen(b: &[u8]) -> Result<(&[u8], &[u8]), Error> {
let (n, used) = read_clen(b)?;
let end = used.checked_add(n).ok_or(Error::Truncated)?;
let s = b.get(used..end).ok_or(Error::Truncated)?;
let rest = b.get(end..).ok_or(Error::Truncated)?;
Ok((s, rest))
}
#[inline(always)]
pub(crate) fn utf8(b: &[u8]) -> Result<&str, Error> {
std::str::from_utf8(b).map_err(|_| Error::Malformed("a string is not valid UTF-8"))
}
#[inline(always)]
pub(crate) fn read_width(b: &[u8], at: usize, w: usize) -> Option<usize> {
Some(match w {
1 => *b.get(at)? as usize,
2 => u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?) as usize,
_ => u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?) as usize,
})
}
#[inline(always)]
pub(crate) fn read_int(p: &[u8]) -> Result<i64, Error> {
let wrong = Error::Malformed("an integer of the wrong width");
let last = p.last().ok_or(wrong)?;
let fill = if last & 0x80 != 0 {
0xFF
} else {
0
};
let mut b = [fill; 8];
b.get_mut(..p.len()).ok_or(wrong)?.copy_from_slice(p);
Ok(i64::from_le_bytes(b))
}
#[inline(always)]
pub(crate) const fn int_width(v: i64) -> usize {
((65 - (v ^ (v >> 63)).leading_zeros()) as usize).div_ceil(8)
}
#[inline(always)]
pub(crate) fn read_uint(p: &[u8]) -> Result<u64, Error> {
let wrong = Error::Malformed("an unsigned integer of the wrong width");
if p.is_empty() || p.len() > 8 {
return Err(wrong);
}
let mut b = [0u8; 8];
b[..p.len()].copy_from_slice(p);
Ok(u64::from_le_bytes(b))
}
#[inline(always)]
pub(crate) const fn uint_width(v: u64) -> usize {
let bits = 64 - v.leading_zeros() as usize;
if bits == 0 {
1
} else {
bits.div_ceil(8)
}
}