1#![no_std]
26
27extern crate alloc;
28
29mod encoder;
30mod layout_cache;
31mod schema_layout;
32
33pub use encoder::BinaryEncoder;
34pub use layout_cache::SchemaLayoutCache;
35pub use schema_layout::{ColumnLayout, SchemaLayout};
36
37use alloc::vec::Vec;
38
39pub const HEADER_SIZE: usize = 16;
41
42pub mod flags {
44 pub const HAS_NULLS: u32 = 1 << 0;
45}
46
47#[repr(u8)]
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum BinaryDataType {
51 Boolean = 0,
52 Int32 = 1,
53 Int64 = 2,
54 Float64 = 3,
55 String = 4,
56 DateTime = 5,
57 Bytes = 6,
58 Jsonb = 7,
59}
60
61impl BinaryDataType {
62 pub fn fixed_size(self) -> usize {
64 match self {
65 BinaryDataType::Boolean => 1,
66 BinaryDataType::Int32 => 4,
67 BinaryDataType::Int64 => 8, BinaryDataType::Float64 => 8,
69 BinaryDataType::String => 8, BinaryDataType::DateTime => 8,
71 BinaryDataType::Bytes => 8, BinaryDataType::Jsonb => 8, }
74 }
75
76 pub fn is_variable_length(self) -> bool {
78 matches!(
79 self,
80 BinaryDataType::String | BinaryDataType::Bytes | BinaryDataType::Jsonb
81 )
82 }
83}
84
85impl From<cynos_core::DataType> for BinaryDataType {
86 fn from(dt: cynos_core::DataType) -> Self {
87 match dt {
88 cynos_core::DataType::Boolean => BinaryDataType::Boolean,
89 cynos_core::DataType::Int32 => BinaryDataType::Int32,
90 cynos_core::DataType::Int64 => BinaryDataType::Int64,
91 cynos_core::DataType::Float64 => BinaryDataType::Float64,
92 cynos_core::DataType::String => BinaryDataType::String,
93 cynos_core::DataType::DateTime => BinaryDataType::DateTime,
94 cynos_core::DataType::Bytes => BinaryDataType::Bytes,
95 cynos_core::DataType::Jsonb => BinaryDataType::Jsonb,
96 }
97 }
98}
99
100#[cfg_attr(feature = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
102pub struct BinaryResult {
103 buffer: Vec<u8>,
104}
105
106#[cfg(feature = "wasm")]
107use wasm_bindgen::JsCast;
108
109#[cfg(feature = "wasm")]
110#[wasm_bindgen::prelude::wasm_bindgen]
111impl BinaryResult {
112 pub fn ptr(&self) -> usize {
114 self.buffer.as_ptr() as usize
115 }
116
117 pub fn len(&self) -> usize {
119 self.buffer.len()
120 }
121
122 #[wasm_bindgen(js_name = isEmpty)]
124 pub fn is_empty(&self) -> bool {
125 self.buffer.is_empty()
126 }
127
128 #[wasm_bindgen(js_name = toUint8Array)]
131 pub fn to_uint8_array(&self) -> js_sys::Uint8Array {
132 js_sys::Uint8Array::from(&self.buffer[..])
133 }
134
135 #[wasm_bindgen(js_name = asView)]
139 pub fn as_view(&self) -> js_sys::Uint8Array {
140 let memory = wasm_bindgen::memory();
141 let buffer = memory.dyn_ref::<js_sys::WebAssembly::Memory>()
142 .expect("wasm_bindgen::memory() should return WebAssembly.Memory")
143 .buffer();
144
145 js_sys::Uint8Array::new_with_byte_offset_and_length(
146 &buffer,
147 self.buffer.as_ptr() as u32,
148 self.buffer.len() as u32,
149 )
150 }
151
152 pub fn free(self) {
154 drop(self);
155 }
156}
157
158#[cfg(not(feature = "wasm"))]
159impl BinaryResult {
160 pub fn ptr(&self) -> usize {
162 self.buffer.as_ptr() as usize
163 }
164
165 pub fn len(&self) -> usize {
167 self.buffer.len()
168 }
169
170 pub fn is_empty(&self) -> bool {
172 self.buffer.is_empty()
173 }
174}
175
176impl BinaryResult {
177 pub fn new(buffer: Vec<u8>) -> Self {
179 Self { buffer }
180 }
181}