use std::borrow::Borrow;
use std::ops::Deref;
#[allow(missing_docs)]
pub mod codes {
pub const DESCRIBED: u8 = 0x00;
pub const NULL: u8 = 0x40;
pub const BOOL_TRUE: u8 = 0x41;
pub const BOOL_FALSE: u8 = 0x42;
pub const UINT_0: u8 = 0x43;
pub const ULONG_0: u8 = 0x44;
pub const LIST_0: u8 = 0x45;
pub const BOOL: u8 = 0x56;
pub const UBYTE: u8 = 0x50;
pub const BYTE: u8 = 0x51;
pub const SMALL_UINT: u8 = 0x52;
pub const SMALL_ULONG: u8 = 0x53;
pub const SMALL_INT: u8 = 0x54;
pub const SMALL_LONG: u8 = 0x55;
pub const USHORT: u8 = 0x60;
pub const SHORT: u8 = 0x61;
pub const UINT: u8 = 0x70;
pub const INT: u8 = 0x71;
pub const FLOAT: u8 = 0x72;
pub const CHAR: u8 = 0x73;
pub const DECIMAL32: u8 = 0x74;
pub const ULONG: u8 = 0x80;
pub const LONG: u8 = 0x81;
pub const DOUBLE: u8 = 0x82;
pub const TIMESTAMP: u8 = 0x83;
pub const DECIMAL64: u8 = 0x84;
pub const DECIMAL128: u8 = 0x94;
pub const UUID: u8 = 0x98;
pub const VBIN8: u8 = 0xa0;
pub const STR8: u8 = 0xa1;
pub const SYM8: u8 = 0xa3;
pub const VBIN32: u8 = 0xb0;
pub const STR32: u8 = 0xb1;
pub const SYM32: u8 = 0xb3;
pub const LIST8: u8 = 0xc0;
pub const MAP8: u8 = 0xc1;
pub const LIST32: u8 = 0xd0;
pub const MAP32: u8 = 0xd1;
pub const ARRAY8: u8 = 0xe0;
pub const ARRAY32: u8 = 0xf0;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct Symbol(pub String);
impl Symbol {
pub fn new(s: impl Into<String>) -> Self {
Symbol(s.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for Symbol {
fn from(s: &str) -> Self {
Symbol(s.to_owned())
}
}
impl From<String> for Symbol {
fn from(s: String) -> Self {
Symbol(s)
}
}
impl Deref for Symbol {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl AsRef<str> for Symbol {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for Symbol {
fn borrow(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OrderedMap<K, V>(Vec<(K, V)>);
impl<K, V> OrderedMap<K, V> {
pub fn new() -> Self {
OrderedMap(Vec::new())
}
pub fn with_capacity(n: usize) -> Self {
OrderedMap(Vec::with_capacity(n))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
self.0.iter().map(|(k, v)| (k, v))
}
pub fn push(&mut self, k: K, v: V) {
self.0.push((k, v));
}
pub fn as_slice(&self) -> &[(K, V)] {
&self.0
}
}
impl<K: PartialEq, V> OrderedMap<K, V> {
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
if let Some(slot) = self.0.iter_mut().find(|(ek, _)| *ek == k) {
Some(std::mem::replace(&mut slot.1, v))
} else {
self.0.push((k, v));
None
}
}
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: PartialEq + ?Sized,
{
self.0
.iter()
.find(|(k, _)| k.borrow() == key)
.map(|(_, v)| v)
}
}
impl<K, V> From<Vec<(K, V)>> for OrderedMap<K, V> {
fn from(v: Vec<(K, V)>) -> Self {
OrderedMap(v)
}
}
impl<K, V> FromIterator<(K, V)> for OrderedMap<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
OrderedMap(iter.into_iter().collect())
}
}
impl<K, V> IntoIterator for OrderedMap<K, V> {
type Item = (K, V);
type IntoIter = std::vec::IntoIter<(K, V)>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}