1use std::collections::hash_map::{IntoIter, Iter};
74use std::collections::HashMap;
75use std::ops::{Deref, DerefMut};
76
77use bincode::config::{standard, Configuration, Fixint, LittleEndian};
78use bincode::{Decode as BincodeDecode, Encode as BincodeEncode};
79use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
80use serde_json::Value as SerdeJsonValue;
81
82use crate::rline_error::RlineError;
83use crate::rline_error::RlineError::RuntimeError;
84use crate::value::Value;
85
86type WrappedType = HashMap<String, Value>;
87
88const BINCODE_CONFIG: Configuration<LittleEndian, Fixint> = standard().with_fixed_int_encoding();
89
90#[derive(
92 SerdeSerialize, SerdeDeserialize, BincodeDecode, BincodeEncode, Clone, Debug, PartialEq,
93)]
94pub struct Row(WrappedType);
95
96impl Row {
97 pub fn new() -> Self {
101 Row(HashMap::new())
102 }
103
104 pub fn with_capacity(capacity: usize) -> Self {
109 Row(HashMap::with_capacity(capacity))
110 }
111}
112
113impl Default for Row {
114 fn default() -> Self {
116 Self::new()
117 }
118}
119
120impl Deref for Row {
121 type Target = WrappedType;
122 fn deref(&self) -> &Self::Target {
123 &self.0
124 }
125}
126
127impl DerefMut for Row {
128 fn deref_mut(&mut self) -> &mut WrappedType {
129 &mut self.0
130 }
131}
132
133type ConsumedItem = (String, Value);
134type BorrowedItem<'a> = (&'a String, &'a Value);
135
136impl IntoIterator for Row {
137 type Item = ConsumedItem;
138 type IntoIter = TableIntoIterator;
139
140 fn into_iter(self) -> Self::IntoIter {
144 TableIntoIterator {
145 iterator: self.0.into_iter(),
146 }
147 }
148}
149
150pub struct TableIntoIterator {
151 iterator: IntoIter<String, Value>,
152}
153
154impl Iterator for TableIntoIterator {
155 type Item = ConsumedItem;
156
157 fn next(&mut self) -> Option<ConsumedItem> {
160 self.iterator.next()
161 }
162}
163
164impl<'a> IntoIterator for &'a Row {
165 type Item = BorrowedItem<'a>;
166 type IntoIter = TableIterator<'a>;
167
168 fn into_iter(self) -> Self::IntoIter {
170 TableIterator {
171 iterator: self.0.iter(),
172 }
173 }
174}
175
176pub struct TableIterator<'a> {
177 iterator: Iter<'a, String, Value>,
178}
179
180impl<'a> Iterator for TableIterator<'a> {
181 type Item = BorrowedItem<'a>;
182
183 fn next(&mut self) -> Option<BorrowedItem<'a>> {
186 self.iterator.next()
187 }
188}
189
190#[derive(SerdeDeserialize)]
192pub enum SerializationFormat {
193 #[serde(alias = "json", alias = "JSON")]
194 Json,
195 #[serde(alias = "bincode", alias = "BINCODE")]
196 Bincode,
197}
198
199impl Row {
200 pub fn to_bytes_json(&self) -> Result<Vec<u8>, RlineError> {
202 serde_json::to_vec(self).map_err(|e| RlineError::runtime_error(&e))
203 }
204
205 pub fn to_bytes_bincode(&self) -> Result<Vec<u8>, RlineError> {
207 bincode::encode_to_vec(self, BINCODE_CONFIG).map_err(|e| RlineError::runtime_error(&e))
208 }
209
210 pub fn to_bytes_bincode_result(result: Result<Row, String>) -> Result<Vec<u8>, RlineError> {
212 bincode::encode_to_vec(result, BINCODE_CONFIG).map_err(|e| RlineError::runtime_error(&e))
213 }
214
215 pub fn to_bytes_json_result(result: Result<Row, String>) -> Result<Vec<u8>, RlineError> {
217 serde_json::to_vec(&result).map_err(|e| RlineError::runtime_error(&e))
218 }
219
220 pub fn from_bytes_json(from: &[u8]) -> Result<Self, RlineError> {
222 serde_json::from_slice(from).map_err(|e| RlineError::runtime_error(&e))
223 }
224
225 pub fn from_bytes_bincode(from: &[u8]) -> Result<Self, RlineError> {
227 let (decoded, _) = bincode::decode_from_slice(from, BINCODE_CONFIG)
228 .map_err(|e| RlineError::runtime_error(&e))?;
229 Ok(decoded)
230 }
231
232 pub fn from_bytes_json_result(from: &[u8]) -> Result<Self, RlineError> {
234 let decoded: Result<Self, String> =
235 serde_json::from_slice(from).map_err(|e| RlineError::runtime_error(&e))?;
236 decoded.map_err(RuntimeError)
237 }
238
239 pub fn from_bytes_bincode_result(from: &[u8]) -> Result<Self, RlineError> {
241 let (decoded, _): (Result<Self, String>, usize) =
242 bincode::decode_from_slice(from, BINCODE_CONFIG)
243 .map_err(|e| RlineError::runtime_error(&e))?;
244 decoded.map_err(RuntimeError)
245 }
246
247 fn json_value_to_row(json_value: SerdeJsonValue) -> Result<Row, RlineError> {
248 match json_value {
249 SerdeJsonValue::Null
250 | SerdeJsonValue::Bool(_)
251 | SerdeJsonValue::Number(_)
252 | SerdeJsonValue::String(_)
253 | SerdeJsonValue::Array(_) => Err(RuntimeError(
254 format!("Json value {} does not represent a row. A row is a map associating column names to their values.", json_value))
255 ),
256 SerdeJsonValue::Object(map_key_value) => {
257 let mut row = Row::with_capacity(map_key_value.len());
258 for (column_name, json_value) in map_key_value {
259 row.insert(column_name, Value::from_json_value(json_value)?);
260 }
261 Ok(row)
262 }
263 }
264 }
265
266 pub fn from_bytes_json_infer_types(from: &[u8]) -> Result<Vec<Row>, RlineError> {
268 let json_value: SerdeJsonValue =
269 serde_json::from_slice(from).map_err(|e| RlineError::runtime_error(&e))?;
270
271 match json_value {
272 SerdeJsonValue::Null
273 | SerdeJsonValue::Bool(_)
274 | SerdeJsonValue::Number(_)
275 | SerdeJsonValue::String(_)
276 | SerdeJsonValue::Object(_) => Err(RuntimeError(format!(
277 "Json value {} does not represent a list of ros.",
278 json_value
279 ))),
280 SerdeJsonValue::Array(values) => {
281 values.into_iter().map(Row::json_value_to_row).collect()
282 }
283 }
284 }
285}
286
287impl<const N: usize> From<[(String, Value); N]> for Row {
288 fn from(arr: [(String, Value); N]) -> Self {
305 Row(HashMap::from_iter(arr))
306 }
307}
308
309impl From<HashMap<String, Value>> for Row {
310 fn from(map: HashMap<String, Value>) -> Self {
311 Row(map)
312 }
313}