reifydb_codec/ffi/
cells.rs1use std::str::from_utf8;
5
6use bigdecimal::BigDecimal;
7use num_bigint::BigInt;
8use reifydb_value::value::{
9 Value, decimal::Decimal, dictionary::DictionaryEntryId, duration::Duration, int::Int, uint::Uint,
10};
11
12use crate::{
13 error::{DecodeError, EncodeError},
14 reader::Reader,
15 value::{decode_value, encode_value_into},
16};
17
18pub fn encode_int_cell(value: &Int, buf: &mut Vec<u8>) {
19 buf.extend_from_slice(&value.0.to_signed_bytes_le());
20}
21
22pub fn decode_int_cell(bytes: &[u8]) -> Int {
23 Int(BigInt::from_signed_bytes_le(bytes))
24}
25
26pub fn encode_uint_cell(value: &Uint, buf: &mut Vec<u8>) {
27 buf.extend_from_slice(&value.0.to_signed_bytes_le());
28}
29
30pub fn decode_uint_cell(bytes: &[u8]) -> Uint {
31 Uint(BigInt::from_signed_bytes_le(bytes))
32}
33
34pub fn encode_decimal_cell(value: &Decimal, buf: &mut Vec<u8>) {
35 buf.extend_from_slice(value.to_string().as_bytes());
36}
37
38pub fn decode_decimal_cell(bytes: &[u8]) -> Result<Decimal, DecodeError> {
39 let s = from_utf8(bytes).map_err(|e| DecodeError::InvalidData(format!("invalid decimal: {e}")))?;
40 let dec: BigDecimal = s.parse().map_err(|e| DecodeError::InvalidData(format!("invalid decimal: {e}")))?;
41 Ok(Decimal::new(dec))
42}
43
44pub fn encode_any_cell(value: &Value, buf: &mut Vec<u8>) -> Result<(), EncodeError> {
45 encode_value_into(value, buf)
46}
47
48pub fn decode_any_cell(bytes: &[u8]) -> Result<Value, DecodeError> {
49 decode_value(bytes)
50}
51
52pub fn encode_dictionary_id_cell(id: &DictionaryEntryId, buf: &mut Vec<u8>) {
53 match id {
54 DictionaryEntryId::U1(v) => {
55 buf.push(1);
56 buf.extend_from_slice(&v.to_le_bytes());
57 }
58 DictionaryEntryId::U2(v) => {
59 buf.push(2);
60 buf.extend_from_slice(&v.to_le_bytes());
61 }
62 DictionaryEntryId::U4(v) => {
63 buf.push(4);
64 buf.extend_from_slice(&v.to_le_bytes());
65 }
66 DictionaryEntryId::U8(v) => {
67 buf.push(8);
68 buf.extend_from_slice(&v.to_le_bytes());
69 }
70 DictionaryEntryId::U16(v) => {
71 buf.push(16);
72 buf.extend_from_slice(&v.to_le_bytes());
73 }
74 }
75}
76
77pub fn decode_dictionary_id_cell(bytes: &[u8]) -> Result<DictionaryEntryId, DecodeError> {
78 let mut r = Reader::new(bytes);
79 let width = r.u8()?;
80 let id = match width {
81 1 => DictionaryEntryId::U1(r.u8()?),
82 2 => DictionaryEntryId::U2(r.u16()?),
83 4 => DictionaryEntryId::U4(r.u32()?),
84 8 => DictionaryEntryId::U8(r.u64()?),
85 16 => DictionaryEntryId::U16(r.u128()?),
86 other => {
87 return Err(DecodeError::InvalidData(format!("invalid dictionary id width: {other}")));
88 }
89 };
90 if !r.is_empty() {
91 return Err(DecodeError::TrailingBytes(r.remaining()));
92 }
93 Ok(id)
94}
95
96pub fn encode_duration_cell(value: &Duration, buf: &mut Vec<u8>) {
97 buf.extend_from_slice(&value.get_months().to_le_bytes());
98 buf.extend_from_slice(&value.get_days().to_le_bytes());
99 buf.extend_from_slice(&value.get_nanos().to_le_bytes());
100}
101
102pub fn decode_duration_cell(bytes: &[u8]) -> Result<Duration, DecodeError> {
103 let mut r = Reader::new(bytes);
104 let months = r.i32()?;
105 let days = r.i32()?;
106 let nanos = r.i64()?;
107 if !r.is_empty() {
108 return Err(DecodeError::TrailingBytes(r.remaining()));
109 }
110 Duration::new(months, days, nanos).map_err(|e| DecodeError::InvalidData(format!("invalid duration: {e}")))
111}