1use std::collections::HashMap;
4use std::fs::File;
5use std::io::{BufWriter, Write, Seek, SeekFrom};
6use std::path::Path;
7use crate::types::ObjectMap;
8
9use crate::{Result, Value, Schema, Union, FieldType, TLType, MAGIC, VERSION_MAJOR, VERSION_MINOR, HEADER_SIZE,
10 MAX_STRING_LENGTH, MAX_OBJECT_FIELDS, MAX_ARRAY_LENGTH};
11
12pub struct Writer {
13 strings: Vec<String>,
14 string_map: HashMap<String, u32>,
15 schemas: Vec<Schema>,
16 schema_map: HashMap<String, u16>,
17 unions: Vec<Union>,
18 union_map: HashMap<String, u16>,
19 sections: Vec<Section>,
20 is_root_array: bool,
22}
23
24struct Section {
25 key: String,
26 data: Vec<u8>,
27 schema_idx: i32,
28 tl_type: TLType,
29 is_array: bool,
30 item_count: u32,
31}
32
33impl Writer {
34 pub fn new() -> Self {
35 Self {
36 strings: Vec::new(),
37 string_map: HashMap::new(),
38 schemas: Vec::new(),
39 schema_map: HashMap::new(),
40 unions: Vec::new(),
41 union_map: HashMap::new(),
42 sections: Vec::new(),
43 is_root_array: false,
44 }
45 }
46
47 pub fn set_root_array(&mut self, is_root_array: bool) {
49 self.is_root_array = is_root_array;
50 }
51
52 pub fn intern(&mut self, s: &str) -> u32 {
53 if let Some(&idx) = self.string_map.get(s) { return idx; }
54 let idx = self.strings.len() as u32;
55 self.strings.push(s.to_string());
56 self.string_map.insert(s.to_string(), idx);
57 idx
58 }
59
60 pub fn add_schema(&mut self, schema: Schema) -> u16 {
61 if let Some(&idx) = self.schema_map.get(&schema.name) { return idx; }
62 for field in &schema.fields { self.intern(&field.name); }
63 self.intern(&schema.name);
64 let idx = self.schemas.len() as u16;
65 self.schema_map.insert(schema.name.clone(), idx);
66 self.schemas.push(schema);
67 idx
68 }
69
70 pub fn add_union(&mut self, union: Union) -> u16 {
71 if let Some(&idx) = self.union_map.get(&union.name) { return idx; }
72 self.intern(&union.name);
73 for variant in &union.variants {
74 self.intern(&variant.name);
75 for field in &variant.fields {
76 self.intern(&field.name);
77 }
78 }
79 let idx = self.unions.len() as u16;
80 self.union_map.insert(union.name.clone(), idx);
81 self.unions.push(union);
82 idx
83 }
84
85 pub fn add_section(&mut self, key: &str, value: &Value, schema: Option<&Schema>) -> Result<()> {
86 self.intern(key);
87 let (data, tl_type, is_array, item_count) = self.encode_value(value, schema)?;
88 let schema_idx = schema.map(|s| self.schema_map.get(&s.name).copied().unwrap_or(0xFFFF) as i32).unwrap_or(-1);
90 self.sections.push(Section { key: key.to_string(), data, schema_idx, tl_type, is_array, item_count });
91 Ok(())
92 }
93
94 pub fn write<P: AsRef<Path>>(&self, path: P, compress: bool) -> Result<()> {
95 let file = File::create(path)?;
96 let mut w = BufWriter::new(file);
97 w.write_all(&[0u8; HEADER_SIZE])?;
98
99 let str_off = HEADER_SIZE as u64;
100 self.write_string_table(&mut w)?;
101 let sch_off = str_off + self.string_table_size() as u64;
102 self.write_schema_table(&mut w)?;
103 let idx_off = sch_off + self.schema_table_size() as u64;
104 let index_size = 8 + self.sections.len() * 32;
105 w.write_all(&vec![0u8; index_size])?;
106 let data_off = idx_off + index_size as u64;
107
108 let mut entries = Vec::new();
109 let mut cur_off = data_off;
110 for sec in &self.sections {
111 let (written, compressed) = if compress && sec.data.len() > 64 {
112 let c = compress_data(&sec.data)?;
113 if c.len() < (sec.data.len() as f64 * 0.9) as usize { (c, true) } else { (sec.data.clone(), false) }
114 } else { (sec.data.clone(), false) };
115 w.write_all(&written)?;
116 entries.push((self.string_map[&sec.key], cur_off, written.len() as u32, sec.data.len() as u32, sec.schema_idx, sec.tl_type, compressed, sec.is_array, sec.item_count));
117 cur_off += written.len() as u64;
118 }
119
120 w.seek(SeekFrom::Start(0))?;
121 w.write_all(&MAGIC)?;
122 w.write_all(&VERSION_MAJOR.to_le_bytes())?;
123 w.write_all(&VERSION_MINOR.to_le_bytes())?;
124 let mut flags: u32 = 0;
126 if compress { flags |= 0x01; }
127 if self.is_root_array { flags |= 0x02; }
128 w.write_all(&flags.to_le_bytes())?;
129 w.write_all(&0u32.to_le_bytes())?;
130 w.write_all(&str_off.to_le_bytes())?;
131 w.write_all(&sch_off.to_le_bytes())?;
132 w.write_all(&idx_off.to_le_bytes())?;
133 w.write_all(&data_off.to_le_bytes())?;
134 w.write_all(&(self.strings.len() as u32).to_le_bytes())?;
135 w.write_all(&(self.schemas.len() as u32).to_le_bytes())?;
136 w.write_all(&(self.sections.len() as u32).to_le_bytes())?;
137 w.write_all(&0u32.to_le_bytes())?;
138
139 w.seek(SeekFrom::Start(idx_off))?;
140 w.write_all(&(index_size as u32).to_le_bytes())?;
141 w.write_all(&(entries.len() as u32).to_le_bytes())?;
142 for (ki, off, sz, usz, si, pt, comp, arr, cnt) in entries {
143 w.write_all(&ki.to_le_bytes())?;
144 w.write_all(&off.to_le_bytes())?;
145 w.write_all(&sz.to_le_bytes())?;
146 w.write_all(&usz.to_le_bytes())?;
147 w.write_all(&(if si < 0 { 0xFFFFu16 } else { si as u16 }).to_le_bytes())?;
148 w.write_all(&[pt as u8])?;
149 w.write_all(&[(if comp { 1 } else { 0 }) | (if arr { 2 } else { 0 })])?;
150 w.write_all(&cnt.to_le_bytes())?;
151 w.write_all(&[0u8; 4])?;
152 }
153 w.flush()?;
154 Ok(())
155 }
156
157 fn string_table_size(&self) -> usize {
158 8 + self.strings.len() * 8 + self.strings.iter().map(|s| s.len()).sum::<usize>()
159 }
160
161 fn schema_table_size(&self) -> usize {
162 if self.schemas.is_empty() && self.unions.is_empty() { return 8; }
163 let struct_size = self.schemas.len() * 4
164 + self.schemas.iter().map(|s| 8 + s.fields.len() * 8).sum::<usize>();
165 let union_size = self.unions.len() * 4
166 + self.unions.iter().map(|u| {
167 8 + u.variants.iter().map(|v| 8 + v.fields.len() * 8).sum::<usize>()
168 }).sum::<usize>();
169 8 + struct_size + union_size
170 }
171
172 fn write_string_table<W: Write>(&self, w: &mut W) -> Result<()> {
173 let table_size = self.string_table_size();
174 if table_size > u32::MAX as usize {
175 return Err(crate::Error::ValueOutOfRange(
176 format!("String table size {} exceeds u32::MAX", table_size)));
177 }
178 let total_string_bytes: usize = self.strings.iter().map(|s| s.len()).sum();
179 if total_string_bytes > u32::MAX as usize {
180 return Err(crate::Error::ValueOutOfRange(
181 format!("Total string data {} bytes exceeds u32::MAX", total_string_bytes)));
182 }
183 let mut off = 0u32;
184 let offsets: Vec<u32> = self.strings.iter().map(|s| { let o = off; off += s.len() as u32; o }).collect();
185 w.write_all(&(table_size as u32).to_le_bytes())?;
186 w.write_all(&(self.strings.len() as u32).to_le_bytes())?;
187 for o in &offsets { w.write_all(&o.to_le_bytes())?; }
188 for s in &self.strings {
189 if s.len() > MAX_STRING_LENGTH {
190 return Err(crate::Error::ValueOutOfRange(
191 format!("String length {} exceeds maximum {}", s.len(), MAX_STRING_LENGTH)));
192 }
193 w.write_all(&(s.len() as u32).to_le_bytes())?;
194 }
195 for s in &self.strings { w.write_all(s.as_bytes())?; }
196 Ok(())
197 }
198
199 fn write_schema_table<W: Write>(&self, w: &mut W) -> Result<()> {
200 if self.schemas.is_empty() && self.unions.is_empty() {
201 w.write_all(&8u32.to_le_bytes())?;
202 w.write_all(&0u32.to_le_bytes())?;
203 return Ok(());
204 }
205
206 let mut struct_data = Vec::new();
208 let mut off = 0u32;
209 let struct_offsets: Vec<u32> = self.schemas.iter().map(|s| {
210 let o = off;
211 off += (8 + s.fields.len() * 8) as u32;
212 o
213 }).collect();
214 for schema in &self.schemas {
215 struct_data.extend_from_slice(&self.string_map[&schema.name].to_le_bytes());
216 struct_data.extend_from_slice(&(schema.fields.len() as u16).to_le_bytes());
217 struct_data.extend_from_slice(&0u16.to_le_bytes());
218 for f in &schema.fields {
219 struct_data.extend_from_slice(&self.string_map[&f.name].to_le_bytes());
220 let resolved_tl_type = if self.union_map.contains_key(&f.field_type.base) {
222 TLType::Tagged
223 } else {
224 f.field_type.to_tl_type()
225 };
226 struct_data.push(resolved_tl_type as u8);
227 let mut flags: u8 = 0;
228 if f.field_type.nullable { flags |= 0x01; }
229 if f.field_type.is_array { flags |= 0x02; }
230 struct_data.push(flags);
231 if resolved_tl_type == TLType::Struct {
233 let type_name_idx = self.string_map.get(&f.field_type.base)
234 .copied()
235 .map(|i| i as u16)
236 .unwrap_or(0xFFFF);
237 struct_data.extend_from_slice(&type_name_idx.to_le_bytes());
238 } else if resolved_tl_type == TLType::Tagged {
239 let type_name_idx = self.string_map.get(&f.field_type.base)
241 .copied()
242 .map(|i| i as u16)
243 .unwrap_or(0xFFFF);
244 struct_data.extend_from_slice(&type_name_idx.to_le_bytes());
245 } else {
246 struct_data.extend_from_slice(&0xFFFFu16.to_le_bytes());
247 }
248 }
249 }
250
251 let mut union_data = Vec::new();
253 let mut uoff = 0u32;
254 let union_offsets: Vec<u32> = self.unions.iter().map(|u| {
255 let o = uoff;
256 uoff += (8 + u.variants.iter().map(|v| 8 + v.fields.len() * 8).sum::<usize>()) as u32;
257 o
258 }).collect();
259 for union in &self.unions {
260 union_data.extend_from_slice(&self.string_map[&union.name].to_le_bytes());
261 union_data.extend_from_slice(&(union.variants.len() as u16).to_le_bytes());
262 union_data.extend_from_slice(&0u16.to_le_bytes()); for variant in &union.variants {
264 union_data.extend_from_slice(&self.string_map[&variant.name].to_le_bytes());
265 union_data.extend_from_slice(&(variant.fields.len() as u16).to_le_bytes());
266 union_data.extend_from_slice(&0u16.to_le_bytes()); for f in &variant.fields {
268 union_data.extend_from_slice(&self.string_map[&f.name].to_le_bytes());
269 let resolved_tl_type = if self.union_map.contains_key(&f.field_type.base) {
270 TLType::Tagged
271 } else {
272 f.field_type.to_tl_type()
273 };
274 union_data.push(resolved_tl_type as u8);
275 let mut flags: u8 = 0;
276 if f.field_type.nullable { flags |= 0x01; }
277 if f.field_type.is_array { flags |= 0x02; }
278 union_data.push(flags);
279 if resolved_tl_type == TLType::Struct || resolved_tl_type == TLType::Tagged {
280 let type_name_idx = self.string_map.get(&f.field_type.base)
281 .copied()
282 .map(|i| i as u16)
283 .unwrap_or(0xFFFF);
284 union_data.extend_from_slice(&type_name_idx.to_le_bytes());
285 } else {
286 union_data.extend_from_slice(&0xFFFFu16.to_le_bytes());
287 }
288 }
289 }
290 }
291
292 w.write_all(&(self.schema_table_size() as u32).to_le_bytes())?;
294 w.write_all(&(self.schemas.len() as u16).to_le_bytes())?;
295 w.write_all(&(self.unions.len() as u16).to_le_bytes())?; for o in &struct_offsets { w.write_all(&o.to_le_bytes())?; }
298 w.write_all(&struct_data)?;
299 for o in &union_offsets { w.write_all(&o.to_le_bytes())?; }
301 w.write_all(&union_data)?;
302 Ok(())
303 }
304
305 fn encode_value(&mut self, value: &Value, schema: Option<&Schema>) -> Result<(Vec<u8>, TLType, bool, u32)> {
306 match value {
307 Value::Null => Ok((vec![], TLType::Null, false, 0)),
308 Value::Bool(b) => Ok((vec![if *b { 1 } else { 0 }], TLType::Bool, false, 0)),
309 Value::Int(i) => Ok(encode_int(*i)),
310 Value::UInt(u) => Ok(encode_uint(*u)),
311 Value::Float(f) => Ok((f.to_le_bytes().to_vec(), TLType::Float64, false, 0)),
312 Value::String(s) => { let idx = self.intern(s); Ok((idx.to_le_bytes().to_vec(), TLType::String, false, 0)) }
313 Value::Bytes(b) => { let mut buf = Vec::new(); write_varint(&mut buf, b.len() as u64); buf.extend(b); Ok((buf, TLType::Bytes, false, 0)) }
314 Value::Array(arr) => self.encode_array(arr, schema),
315 Value::Object(obj) => self.encode_object(obj),
316 Value::Map(pairs) => self.encode_map(pairs),
317 Value::Ref(r) => { let idx = self.intern(r); Ok((idx.to_le_bytes().to_vec(), TLType::Ref, false, 0)) }
318 Value::Tagged(tag, inner) => {
319 let ti = self.intern(tag);
320 let (d, t, _, _) = self.encode_value(inner, None)?;
321 let mut buf = ti.to_le_bytes().to_vec();
322 buf.push(t as u8);
323 buf.extend(d);
324 Ok((buf, TLType::Tagged, false, 0))
325 }
326 Value::Timestamp(ts, tz) => {
327 let mut buf = ts.to_le_bytes().to_vec();
328 buf.extend(tz.to_le_bytes());
329 Ok((buf, TLType::Timestamp, false, 0))
330 }
331 Value::JsonNumber(s) => { let idx = self.intern(s); Ok((idx.to_le_bytes().to_vec(), TLType::JsonNumber, false, 0)) }
332 }
333 }
334
335 fn encode_map(&mut self, pairs: &[(Value, Value)]) -> Result<(Vec<u8>, TLType, bool, u32)> {
336 let mut buf = (pairs.len() as u32).to_le_bytes().to_vec();
337 for (k, v) in pairs {
338 match k {
340 Value::String(_) | Value::Int(_) | Value::UInt(_) => {}
341 _ => return Err(crate::Error::ParseError(
342 format!("Invalid map key type {:?}: map keys must be string, int, or uint per spec", k.tl_type())
343 )),
344 }
345 let (kd, kt, _, _) = self.encode_value(k, None)?;
346 let (vd, vt, _, _) = self.encode_value(v, None)?;
347 buf.push(kt as u8);
348 buf.extend(kd);
349 buf.push(vt as u8);
350 buf.extend(vd);
351 }
352 Ok((buf, TLType::Map, false, pairs.len() as u32))
353 }
354
355 fn encode_array(&mut self, arr: &[Value], schema: Option<&Schema>) -> Result<(Vec<u8>, TLType, bool, u32)> {
356 if arr.len() > MAX_ARRAY_LENGTH {
357 return Err(crate::Error::ValueOutOfRange(
358 format!("Array has {} elements, exceeds maximum {}", arr.len(), MAX_ARRAY_LENGTH)));
359 }
360 let mut buf = (arr.len() as u32).to_le_bytes().to_vec();
361 if arr.is_empty() { return Ok((buf, TLType::Array, true, 0)); }
362 if schema.is_some() && arr.iter().all(|v| matches!(v, Value::Object(_) | Value::Null)) {
363 return self.encode_struct_array(arr, schema.unwrap());
364 }
365 if arr.iter().all(|v| matches!(v, Value::Int(_))) {
369 let all_fit_i32 = arr.iter().all(|v| {
370 if let Value::Int(i) = v { *i >= i32::MIN as i64 && *i <= i32::MAX as i64 } else { false }
371 });
372 if all_fit_i32 {
373 buf.push(TLType::Int32 as u8);
374 for v in arr { if let Value::Int(i) = v { buf.extend((*i as i32).to_le_bytes()); } }
375 return Ok((buf, TLType::Array, true, arr.len() as u32));
376 }
377 }
379 if arr.iter().all(|v| matches!(v, Value::String(_))) {
380 buf.push(TLType::String as u8);
381 for v in arr { if let Value::String(s) = v { buf.extend(self.intern(s).to_le_bytes()); } }
382 return Ok((buf, TLType::Array, true, arr.len() as u32));
383 }
384 buf.push(0xFF);
385 for v in arr { let (d, t, _, _) = self.encode_value(v, None)?; buf.push(t as u8); buf.extend(d); }
386 Ok((buf, TLType::Array, true, arr.len() as u32))
387 }
388
389 fn encode_struct_array(&mut self, arr: &[Value], schema: &Schema) -> Result<(Vec<u8>, TLType, bool, u32)> {
390 let mut buf = (arr.len() as u32).to_le_bytes().to_vec();
391 let si = match self.schema_map.get(&schema.name) {
392 Some(&idx) => idx,
393 None => self.add_schema(schema.clone()),
394 };
395 buf.extend(si.to_le_bytes());
396 let bms = (schema.fields.len() + 7) / 8;
397 buf.extend((bms as u16).to_le_bytes());
398 let nested_schemas: Vec<Option<Schema>> = schema.fields.iter()
400 .map(|f| self.schemas.iter().find(|s| s.name == f.field_type.base).cloned())
401 .collect();
402 for v in arr {
403 if let Value::Object(obj) = v {
404 let mut bitmap = vec![0u8; bms];
405 for (i, f) in schema.fields.iter().enumerate() {
406 if obj.get(&f.name).map(|v| v.is_null()).unwrap_or(true) {
407 bitmap[i / 8] |= 1 << (i % 8);
408 }
409 }
410 buf.extend_from_slice(&bitmap);
411 for (i, f) in schema.fields.iter().enumerate() {
412 let is_null = bitmap[i / 8] & (1 << (i % 8)) != 0;
413 if !is_null {
414 if let Some(v) = obj.get(&f.name) {
415 let data = self.encode_typed_value(v, &f.field_type, nested_schemas[i].as_ref())?;
416 buf.extend(data);
417 }
418 }
419 }
420 } else {
421 let mut bitmap = vec![0u8; bms];
423 for i in 0..schema.fields.len() {
424 bitmap[i / 8] |= 1 << (i % 8);
425 }
426 buf.extend_from_slice(&bitmap);
427 }
428 }
429 Ok((buf, TLType::Struct, true, arr.len() as u32))
430 }
431
432 fn encode_typed_value(&mut self, value: &Value, field_type: &FieldType, nested_schema: Option<&Schema>) -> Result<Vec<u8>> {
434 use crate::TLType;
435
436 if field_type.is_array {
438 if let Value::Array(arr) = value {
439 let mut buf = (arr.len() as u32).to_le_bytes().to_vec();
440 if arr.is_empty() { return Ok(buf); }
441
442 let elem_type = FieldType::new(&field_type.base);
444 let elem_tl_type = if self.union_map.contains_key(&field_type.base) {
445 TLType::Tagged
446 } else {
447 elem_type.to_tl_type()
448 };
449
450 let elem_schema = self.schemas.iter()
452 .find(|s| s.name == field_type.base)
453 .cloned();
454
455 buf.push(elem_tl_type as u8);
457
458 for v in arr {
460 buf.extend(self.encode_typed_value(v, &elem_type, elem_schema.as_ref())?);
461 }
462 return Ok(buf);
463 }
464 return Ok((0u32).to_le_bytes().to_vec());
467 }
468
469 let tl_type = field_type.to_tl_type();
470 match tl_type {
471 TLType::Null => Ok(vec![]),
472 TLType::Bool => {
473 if let Value::Bool(b) = value { Ok(vec![if *b { 1 } else { 0 }]) }
474 else { Ok(vec![0]) }
475 }
476 TLType::Int8 => {
477 let i = checked_int_value(value, i8::MIN as i64, i8::MAX as i64, "int8")?;
478 Ok((i as i8).to_le_bytes().to_vec())
479 }
480 TLType::Int16 => {
481 let i = checked_int_value(value, i16::MIN as i64, i16::MAX as i64, "int16")?;
482 Ok((i as i16).to_le_bytes().to_vec())
483 }
484 TLType::Int32 => {
485 let i = checked_int_value(value, i32::MIN as i64, i32::MAX as i64, "int32")?;
486 Ok((i as i32).to_le_bytes().to_vec())
487 }
488 TLType::Int64 => {
489 let i = checked_int_value(value, i64::MIN, i64::MAX, "int64")?;
490 Ok(i.to_le_bytes().to_vec())
491 }
492 TLType::UInt8 => {
493 let u = checked_uint_value(value, u8::MAX as u64, "uint8")?;
494 Ok((u as u8).to_le_bytes().to_vec())
495 }
496 TLType::UInt16 => {
497 let u = checked_uint_value(value, u16::MAX as u64, "uint16")?;
498 Ok((u as u16).to_le_bytes().to_vec())
499 }
500 TLType::UInt32 => {
501 let u = checked_uint_value(value, u32::MAX as u64, "uint32")?;
502 Ok((u as u32).to_le_bytes().to_vec())
503 }
504 TLType::UInt64 => {
505 let u = checked_uint_value(value, u64::MAX, "uint64")?;
506 Ok(u.to_le_bytes().to_vec())
507 }
508 TLType::Float32 => {
509 let f = match value { Value::Float(f) => *f, Value::Int(i) => *i as f64, Value::UInt(u) => *u as f64, _ => 0.0 };
510 Ok((f as f32).to_le_bytes().to_vec())
511 }
512 TLType::Float64 => {
513 let f = match value { Value::Float(f) => *f, Value::Int(i) => *i as f64, Value::UInt(u) => *u as f64, _ => 0.0 };
514 Ok(f.to_le_bytes().to_vec())
515 }
516 TLType::String => {
517 if let Value::String(s) = value { Ok(self.intern(s).to_le_bytes().to_vec()) }
518 else { Ok(self.intern("").to_le_bytes().to_vec()) }
519 }
520 TLType::Bytes => {
521 if let Value::Bytes(b) = value {
522 let mut buf = Vec::new();
523 write_varint(&mut buf, b.len() as u64);
524 buf.extend(b);
525 Ok(buf)
526 } else { Ok(vec![0]) }
527 }
528 TLType::Timestamp => {
529 if let Value::Timestamp(ts, tz) = value {
530 let mut buf = ts.to_le_bytes().to_vec();
531 buf.extend(tz.to_le_bytes());
532 Ok(buf)
533 } else {
534 let mut buf = 0i64.to_le_bytes().to_vec();
535 buf.extend(0i16.to_le_bytes());
536 Ok(buf)
537 }
538 }
539 TLType::Struct => {
540 if self.union_map.contains_key(&field_type.base) {
542 let (d, _, _, _) = self.encode_value(value, None)?;
543 return Ok(d);
544 }
545 if let (Value::Object(obj), Some(schema)) = (value, nested_schema) {
547 let mut buf = Vec::new();
548
549 let schema_idx = *self.schema_map.get(&schema.name).unwrap_or(&0);
551 buf.extend(schema_idx.to_le_bytes());
552
553 let bms = (schema.fields.len() + 7) / 8;
554
555 let mut bitmap = vec![0u8; bms];
557 for (i, f) in schema.fields.iter().enumerate() {
558 if obj.get(&f.name).map(|v| v.is_null()).unwrap_or(true) {
559 bitmap[i / 8] |= 1 << (i % 8);
560 }
561 }
562 buf.extend_from_slice(&bitmap);
563
564 for (i, f) in schema.fields.iter().enumerate() {
566 let is_null = bitmap[i / 8] & (1 << (i % 8)) != 0;
567 if !is_null {
568 if let Some(v) = obj.get(&f.name) {
569 let nested = self.schemas.iter().find(|s| s.name == f.field_type.base).cloned();
570 buf.extend(self.encode_typed_value(v, &f.field_type, nested.as_ref())?);
571 }
572 }
573 }
574 Ok(buf)
575 } else {
576 Err(crate::Error::ParseError(
577 format!("schema-typed field '{}' requires a schema for encoding", field_type.base)
578 ))
579 }
580 }
581 _ => {
582 let (d, _, _, _) = self.encode_value(value, None)?;
584 Ok(d)
585 }
586 }
587 }
588
589 fn encode_object(&mut self, obj: &ObjectMap<String, Value>) -> Result<(Vec<u8>, TLType, bool, u32)> {
590 if obj.len() > MAX_OBJECT_FIELDS {
591 return Err(crate::Error::ValueOutOfRange(
592 format!("Object has {} fields, exceeds maximum {}", obj.len(), MAX_OBJECT_FIELDS)));
593 }
594 let mut buf = (obj.len() as u16).to_le_bytes().to_vec();
595 for (k, v) in obj {
596 buf.extend(self.intern(k).to_le_bytes());
597 let (d, t, _, _) = self.encode_value(v, None)?;
598 buf.push(t as u8);
599 buf.extend(d);
600 }
601 Ok((buf, TLType::Object, false, 0))
602 }
603}
604
605impl Default for Writer { fn default() -> Self { Self::new() } }
606
607fn checked_int_value(value: &Value, min: i64, max: i64, _type_name: &str) -> Result<i64> {
610 let i = match value {
611 Value::Int(i) => *i,
612 Value::UInt(u) if *u <= i64::MAX as u64 => *u as i64,
613 Value::UInt(_) => 0,
614 Value::Float(f) => {
615 let f = *f;
616 if f.is_finite() && f >= i64::MIN as f64 && f <= i64::MAX as f64 { f as i64 } else { 0 }
617 }
618 Value::JsonNumber(s) => s.parse::<i64>().unwrap_or(0),
619 _ => 0,
620 };
621 if i < min || i > max { Ok(0) } else { Ok(i) }
622}
623
624fn checked_uint_value(value: &Value, max: u64, _type_name: &str) -> Result<u64> {
627 let u = match value {
628 Value::UInt(u) => *u,
629 Value::Int(i) if *i >= 0 => *i as u64,
630 Value::Int(_) => 0,
631 Value::Float(f) => {
632 let f = *f;
633 if f.is_finite() && f >= 0.0 && f <= u64::MAX as f64 { f as u64 } else { 0 }
634 }
635 Value::JsonNumber(s) => s.parse::<u64>().unwrap_or(0),
636 _ => 0,
637 };
638 if u > max { Ok(0) } else { Ok(u) }
639}
640
641fn encode_int(i: i64) -> (Vec<u8>, TLType, bool, u32) {
642 if i >= i8::MIN as i64 && i <= i8::MAX as i64 { ((i as i8).to_le_bytes().to_vec(), TLType::Int8, false, 0) }
643 else if i >= i16::MIN as i64 && i <= i16::MAX as i64 { ((i as i16).to_le_bytes().to_vec(), TLType::Int16, false, 0) }
644 else if i >= i32::MIN as i64 && i <= i32::MAX as i64 { ((i as i32).to_le_bytes().to_vec(), TLType::Int32, false, 0) }
645 else { (i.to_le_bytes().to_vec(), TLType::Int64, false, 0) }
646}
647
648fn encode_uint(u: u64) -> (Vec<u8>, TLType, bool, u32) {
649 if u <= u8::MAX as u64 { ((u as u8).to_le_bytes().to_vec(), TLType::UInt8, false, 0) }
650 else if u <= u16::MAX as u64 { ((u as u16).to_le_bytes().to_vec(), TLType::UInt16, false, 0) }
651 else if u <= u32::MAX as u64 { ((u as u32).to_le_bytes().to_vec(), TLType::UInt32, false, 0) }
652 else { (u.to_le_bytes().to_vec(), TLType::UInt64, false, 0) }
653}
654
655fn write_varint(buf: &mut Vec<u8>, mut v: u64) {
656 while v >= 0x80 { buf.push(((v & 0x7F) | 0x80) as u8); v >>= 7; }
657 buf.push(v as u8);
658}
659
660fn compress_data(data: &[u8]) -> Result<Vec<u8>> {
661 use flate2::Compression;
662 use flate2::write::ZlibEncoder;
663 let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
664 e.write_all(data).map_err(crate::Error::Io)?;
665 e.finish().map_err(crate::Error::Io)
666}
667
668#[cfg(test)]
669mod tests {
670 use super::*;
671 use crate::reader::Reader;
672
673 #[test]
674 fn test_writer_default() {
675 let w = Writer::default();
676 assert_eq!(w.strings.len(), 0);
677 assert_eq!(w.schemas.len(), 0);
678 }
679
680 #[test]
681 fn test_encode_uint_ranges() {
682 let (data, tl_type, _, _) = encode_uint(42);
684 assert_eq!(tl_type, TLType::UInt8);
685 assert_eq!(data, vec![42u8]);
686
687 let (data, tl_type, _, _) = encode_uint(300);
689 assert_eq!(tl_type, TLType::UInt16);
690 assert_eq!(data, 300u16.to_le_bytes().to_vec());
691
692 let (data, tl_type, _, _) = encode_uint(100_000);
694 assert_eq!(tl_type, TLType::UInt32);
695 assert_eq!(data, 100_000u32.to_le_bytes().to_vec());
696
697 let (data, tl_type, _, _) = encode_uint(5_000_000_000);
699 assert_eq!(tl_type, TLType::UInt64);
700 assert_eq!(data, 5_000_000_000u64.to_le_bytes().to_vec());
701 }
702
703 #[test]
704 fn test_uint_value_roundtrip() {
705 let dir = std::env::temp_dir();
706 let path = dir.join("test_uint_roundtrip.tlbx");
707
708 let mut w = Writer::new();
709 w.add_section("small", &Value::UInt(42), None).unwrap();
710 w.add_section("medium", &Value::UInt(300), None).unwrap();
711 w.add_section("large", &Value::UInt(100_000), None).unwrap();
712 w.add_section("huge", &Value::UInt(5_000_000_000), None).unwrap();
713 w.write(&path, false).unwrap();
714
715 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
716 assert_eq!(r.get("small").unwrap().as_uint(), Some(42));
717 assert_eq!(r.get("medium").unwrap().as_uint(), Some(300));
718 assert_eq!(r.get("large").unwrap().as_uint(), Some(100_000));
719 assert_eq!(r.get("huge").unwrap().as_uint(), Some(5_000_000_000));
720 std::fs::remove_file(&path).ok();
721 }
722
723 #[test]
724 fn test_typed_schema_roundtrip() {
725 let dir = std::env::temp_dir();
727 let path = dir.join("test_typed_schema.tlbx");
728
729 let mut schema = Schema::new("TypedRecord");
730 schema.add_field("flag", FieldType::new("bool"));
731 schema.add_field("small_int", FieldType::new("int8"));
732 schema.add_field("med_int", FieldType::new("int16"));
733 schema.add_field("int32_val", FieldType::new("int"));
734 schema.add_field("int64_val", FieldType::new("int64"));
735 schema.add_field("small_uint", FieldType::new("uint8"));
736 schema.add_field("med_uint", FieldType::new("uint16"));
737 schema.add_field("uint32_val", FieldType::new("uint"));
738 schema.add_field("uint64_val", FieldType::new("uint64"));
739 schema.add_field("f32_val", FieldType::new("float32"));
740 schema.add_field("f64_val", FieldType::new("float"));
741 schema.add_field("name", FieldType::new("string"));
742 schema.add_field("data", FieldType::new("bytes"));
743
744 let mut w = Writer::new();
745 w.add_schema(schema.clone());
746
747 let mut obj = ObjectMap::new();
749 obj.insert("flag".to_string(), Value::Bool(true));
750 obj.insert("small_int".to_string(), Value::Int(42));
751 obj.insert("med_int".to_string(), Value::Int(1000));
752 obj.insert("int32_val".to_string(), Value::Int(50000));
753 obj.insert("int64_val".to_string(), Value::Int(1_000_000_000_000));
754 obj.insert("small_uint".to_string(), Value::UInt(200));
755 obj.insert("med_uint".to_string(), Value::UInt(40000));
756 obj.insert("uint32_val".to_string(), Value::UInt(3_000_000));
757 obj.insert("uint64_val".to_string(), Value::UInt(9_000_000_000));
758 obj.insert("f32_val".to_string(), Value::Float(3.14));
759 obj.insert("f64_val".to_string(), Value::Float(2.718281828));
760 obj.insert("name".to_string(), Value::String("test".into()));
761 obj.insert("data".to_string(), Value::Bytes(vec![0xDE, 0xAD]));
762
763 let arr = Value::Array(vec![Value::Object(obj)]);
764 w.add_section("records", &arr, Some(&schema)).unwrap();
765 w.write(&path, false).unwrap();
766
767 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
768 let records = r.get("records").unwrap();
769 let items = records.as_array().unwrap();
770 assert_eq!(items.len(), 1);
771
772 let rec = items[0].as_object().unwrap();
773 assert_eq!(rec.get("flag").unwrap().as_bool(), Some(true));
774 assert_eq!(rec.get("name").unwrap().as_str(), Some("test"));
775 std::fs::remove_file(&path).ok();
776 }
777
778 #[test]
779 fn test_typed_schema_array_field() {
780 let dir = std::env::temp_dir();
782 let path = dir.join("test_typed_array_field.tlbx");
783
784 let mut schema = Schema::new("WithArray");
785 schema.add_field("name", FieldType::new("string"));
786 schema.add_field("scores", FieldType::new("int").array());
787
788 let mut w = Writer::new();
789 w.add_schema(schema.clone());
790
791 let mut obj = ObjectMap::new();
792 obj.insert("name".to_string(), Value::String("Alice".into()));
793 obj.insert("scores".to_string(), Value::Array(vec![Value::Int(90), Value::Int(85)]));
794
795 let arr = Value::Array(vec![Value::Object(obj)]);
796 w.add_section("users", &arr, Some(&schema)).unwrap();
797 w.write(&path, false).unwrap();
798
799 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
800 let users = r.get("users").unwrap();
801 let items = users.as_array().unwrap();
802 assert_eq!(items.len(), 1);
803 let rec = items[0].as_object().unwrap();
804 assert_eq!(rec.get("name").unwrap().as_str(), Some("Alice"));
805 std::fs::remove_file(&path).ok();
806 }
807
808 #[test]
809 fn test_object_encoding_roundtrip() {
810 let dir = std::env::temp_dir();
812 let path = dir.join("test_object_enc.tlbx");
813
814 let mut obj = ObjectMap::new();
815 obj.insert("x".to_string(), Value::Int(10));
816 obj.insert("y".to_string(), Value::String("hello".into()));
817
818 let mut w = Writer::new();
819 w.add_section("data", &Value::Object(obj), None).unwrap();
820 w.write(&path, false).unwrap();
821
822 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
823 let val = r.get("data").unwrap();
824 let o = val.as_object().unwrap();
825 assert_eq!(o.get("x").unwrap().as_int(), Some(10));
826 assert_eq!(o.get("y").unwrap().as_str(), Some("hello"));
827 std::fs::remove_file(&path).ok();
828 }
829
830 #[test]
831 fn test_map_roundtrip_binary() {
832 let dir = std::env::temp_dir();
833 let path = dir.join("test_map_binary.tlbx");
834
835 let pairs = vec![
836 (Value::String("key1".into()), Value::Int(100)),
837 (Value::String("key2".into()), Value::Bool(true)),
838 ];
839
840 let mut w = Writer::new();
841 w.add_section("mapping", &Value::Map(pairs), None).unwrap();
842 w.write(&path, false).unwrap();
843
844 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
845 let val = r.get("mapping").unwrap();
846 if let Value::Map(pairs) = val {
847 assert_eq!(pairs.len(), 2);
848 } else {
849 panic!("Expected Map value");
850 }
851 std::fs::remove_file(&path).ok();
852 }
853
854 #[test]
855 fn test_ref_and_tagged_roundtrip() {
856 let dir = std::env::temp_dir();
857 let path = dir.join("test_ref_tagged.tlbx");
858
859 let mut w = Writer::new();
860 w.add_section("myref", &Value::Ref("some_ref".into()), None).unwrap();
861 w.add_section("mytag", &Value::Tagged("label".into(), Box::new(Value::Int(42))), None).unwrap();
862 w.add_section("myts", &Value::Timestamp(1700000000000, 0), None).unwrap();
863 w.write(&path, false).unwrap();
864
865 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
866
867 if let Value::Ref(s) = r.get("myref").unwrap() {
868 assert_eq!(s, "some_ref");
869 } else { panic!("Expected Ref"); }
870
871 if let Value::Tagged(tag, inner) = r.get("mytag").unwrap() {
872 assert_eq!(tag, "label");
873 assert_eq!(inner.as_int(), Some(42));
874 } else { panic!("Expected Tagged"); }
875
876 if let Value::Timestamp(ts, _) = r.get("myts").unwrap() {
877 assert_eq!(ts, 1700000000000);
878 } else { panic!("Expected Timestamp"); }
879
880 std::fs::remove_file(&path).ok();
881 }
882
883 #[test]
884 fn test_compressed_roundtrip() {
885 let dir = std::env::temp_dir();
886 let path = dir.join("test_compressed.tlbx");
887
888 let mut arr = Vec::new();
890 for i in 0..100 {
891 arr.push(Value::Int(i));
892 }
893
894 let mut w = Writer::new();
895 w.add_section("numbers", &Value::Array(arr), None).unwrap();
896 w.write(&path, true).unwrap();
897
898 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
899 let val = r.get("numbers").unwrap();
900 let items = val.as_array().unwrap();
901 assert_eq!(items.len(), 100);
902 assert_eq!(items[0].as_int(), Some(0));
903 assert_eq!(items[99].as_int(), Some(99));
904 std::fs::remove_file(&path).ok();
905 }
906
907 #[test]
908 fn test_root_array_flag() {
909 let dir = std::env::temp_dir();
910 let path = dir.join("test_root_array_flag.tlbx");
911
912 let mut w = Writer::new();
913 w.set_root_array(true);
914 w.add_section("root", &Value::Array(vec![Value::Int(1)]), None).unwrap();
915 w.write(&path, false).unwrap();
916
917 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
918 assert!(r.is_root_array());
919 std::fs::remove_file(&path).ok();
920 }
921
922 #[test]
923 fn test_bytes_value_roundtrip() {
924 let dir = std::env::temp_dir();
925 let path = dir.join("test_bytes_val.tlbx");
926
927 let mut w = Writer::new();
928 w.add_section("blob", &Value::Bytes(vec![1, 2, 3, 4, 5]), None).unwrap();
929 w.write(&path, false).unwrap();
930
931 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
932 let val = r.get("blob").unwrap();
933 assert_eq!(val.as_bytes(), Some(&[1u8, 2, 3, 4, 5][..]));
934 std::fs::remove_file(&path).ok();
935 }
936
937 #[test]
938 fn test_nested_struct_schema_roundtrip() {
939 let dir = std::env::temp_dir();
941 let path = dir.join("test_nested_struct.tlbx");
942
943 let mut inner_schema = Schema::new("Address");
944 inner_schema.add_field("city", FieldType::new("string"));
945 inner_schema.add_field("zip", FieldType::new("string"));
946
947 let mut outer_schema = Schema::new("Person");
948 outer_schema.add_field("name", FieldType::new("string"));
949 outer_schema.add_field("home", FieldType::new("Address"));
950
951 let mut w = Writer::new();
952 w.add_schema(inner_schema.clone());
953 w.add_schema(outer_schema.clone());
954
955 let mut addr = ObjectMap::new();
956 addr.insert("city".to_string(), Value::String("Seattle".into()));
957 addr.insert("zip".to_string(), Value::String("98101".into()));
958
959 let mut person = ObjectMap::new();
960 person.insert("name".to_string(), Value::String("Alice".into()));
961 person.insert("home".to_string(), Value::Object(addr));
962
963 let arr = Value::Array(vec![Value::Object(person)]);
964 w.add_section("people", &arr, Some(&outer_schema)).unwrap();
965 w.write(&path, false).unwrap();
966
967 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
968 let people = r.get("people").unwrap();
969 let items = people.as_array().unwrap();
970 assert_eq!(items.len(), 1);
971 let p = items[0].as_object().unwrap();
972 assert_eq!(p.get("name").unwrap().as_str(), Some("Alice"));
973 std::fs::remove_file(&path).ok();
974 }
975
976 #[test]
977 fn test_timestamp_typed_field() {
978 let dir = std::env::temp_dir();
980 let path = dir.join("test_ts_typed.tlbx");
981
982 let mut schema = Schema::new("Event");
983 schema.add_field("name", FieldType::new("string"));
984 schema.add_field("ts", FieldType::new("timestamp"));
985
986 let mut w = Writer::new();
987 w.add_schema(schema.clone());
988
989 let mut obj = ObjectMap::new();
990 obj.insert("name".to_string(), Value::String("deploy".into()));
991 obj.insert("ts".to_string(), Value::Timestamp(1700000000000, 0));
992
993 let arr = Value::Array(vec![Value::Object(obj)]);
994 w.add_section("events", &arr, Some(&schema)).unwrap();
995 w.write(&path, false).unwrap();
996
997 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
998 let events = r.get("events").unwrap();
999 let items = events.as_array().unwrap();
1000 assert_eq!(items.len(), 1);
1001 std::fs::remove_file(&path).ok();
1002 }
1003
1004 #[test]
1005 fn test_bytes_typed_field() {
1006 let dir = std::env::temp_dir();
1008 let path = dir.join("test_bytes_typed.tlbx");
1009
1010 let mut schema = Schema::new("Blob");
1011 schema.add_field("name", FieldType::new("string"));
1012 schema.add_field("data", FieldType::new("bytes"));
1013
1014 let mut w = Writer::new();
1015 w.add_schema(schema.clone());
1016
1017 let mut obj = ObjectMap::new();
1018 obj.insert("name".to_string(), Value::String("img".into()));
1019 obj.insert("data".to_string(), Value::Bytes(vec![0xDE, 0xAD, 0xBE, 0xEF]));
1020
1021 let arr = Value::Array(vec![Value::Object(obj)]);
1022 w.add_section("blobs", &arr, Some(&schema)).unwrap();
1023 w.write(&path, false).unwrap();
1024
1025 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
1026 let blobs = r.get("blobs").unwrap();
1027 let items = blobs.as_array().unwrap();
1028 assert_eq!(items.len(), 1);
1029 std::fs::remove_file(&path).ok();
1030 }
1031
1032 #[test]
1037 fn test_checked_int_value_in_range() {
1038 assert_eq!(checked_int_value(&Value::Int(42), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 42);
1039 assert_eq!(checked_int_value(&Value::Int(-128), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), -128);
1040 assert_eq!(checked_int_value(&Value::Int(127), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 127);
1041 assert_eq!(checked_int_value(&Value::UInt(100), i16::MIN as i64, i16::MAX as i64, "int16").unwrap(), 100);
1042 }
1043
1044 #[test]
1045 fn test_checked_int_value_overflow_defaults_to_zero() {
1046 assert_eq!(checked_int_value(&Value::Int(128), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1048 }
1049
1050 #[test]
1051 fn test_checked_int_value_underflow_defaults_to_zero() {
1052 assert_eq!(checked_int_value(&Value::Int(-129), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1053 }
1054
1055 #[test]
1056 fn test_checked_int_value_float_coercion() {
1057 assert_eq!(checked_int_value(&Value::Float(42.7), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 42);
1059 assert_eq!(checked_int_value(&Value::Float(-3.9), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), -3);
1060 assert_eq!(checked_int_value(&Value::Float(f64::NAN), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1062 assert_eq!(checked_int_value(&Value::Float(f64::INFINITY), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1063 assert_eq!(checked_int_value(&Value::Float(200.0), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1065 }
1066
1067 #[test]
1068 fn test_checked_uint_value_in_range() {
1069 assert_eq!(checked_uint_value(&Value::UInt(255), u8::MAX as u64, "uint8").unwrap(), 255);
1070 assert_eq!(checked_uint_value(&Value::Int(42), u8::MAX as u64, "uint8").unwrap(), 42);
1071 }
1072
1073 #[test]
1074 fn test_checked_uint_value_overflow_defaults_to_zero() {
1075 assert_eq!(checked_uint_value(&Value::UInt(256), u8::MAX as u64, "uint8").unwrap(), 0);
1077 }
1078
1079 #[test]
1080 fn test_checked_uint_value_negative_defaults_to_zero() {
1081 assert_eq!(checked_uint_value(&Value::Int(-1), u8::MAX as u64, "uint8").unwrap(), 0);
1083 }
1084
1085 #[test]
1086 fn test_checked_uint_value_float_coercion() {
1087 assert_eq!(checked_uint_value(&Value::Float(42.7), u8::MAX as u64, "uint8").unwrap(), 42);
1088 assert_eq!(checked_uint_value(&Value::Float(-1.0), u8::MAX as u64, "uint8").unwrap(), 0);
1089 assert_eq!(checked_uint_value(&Value::Float(f64::NAN), u8::MAX as u64, "uint8").unwrap(), 0);
1090 assert_eq!(checked_uint_value(&Value::Float(300.0), u8::MAX as u64, "uint8").unwrap(), 0);
1091 }
1092
1093 #[test]
1098 fn test_union_field_roundtrip() {
1099 let dir = std::env::temp_dir();
1101 let path = dir.join("test_union_field_rt.tlbx");
1102
1103 let mut w = Writer::new();
1104
1105 let mut union_def = crate::Union::new("Shape");
1107 union_def.add_variant(crate::Variant::new("Circle").field("radius", FieldType::new("float64")));
1108 union_def.add_variant(crate::Variant::new("Rect").field("w", FieldType::new("float64")).field("h", FieldType::new("float64")));
1109 w.add_union(union_def);
1110
1111 let tagged = Value::Tagged(
1113 "Circle".to_string(),
1114 Box::new(Value::Object({
1115 let mut m = ObjectMap::new();
1116 m.insert("radius".to_string(), Value::Float(5.0));
1117 m
1118 })),
1119 );
1120 w.add_section("shape", &tagged, None).unwrap();
1121 w.write(&path, false).unwrap();
1122
1123 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
1124 let shape = r.get("shape").unwrap();
1125 if let Value::Tagged(tag, inner) = &shape {
1126 assert_eq!(tag, "Circle");
1127 let obj = inner.as_object().unwrap();
1128 assert_eq!(obj.get("radius").unwrap().as_float(), Some(5.0));
1129 } else {
1130 panic!("Expected Tagged value, got {:?}", shape);
1131 }
1132 std::fs::remove_file(&path).ok();
1133 }
1134
1135 #[test]
1136 fn test_union_typed_schema_field_roundtrip() {
1137 let dir = std::env::temp_dir();
1139 let path = dir.join("test_union_schema_field.tlbx");
1140
1141 let mut w = Writer::new();
1142
1143 let mut union_def = crate::Union::new("Status");
1145 union_def.add_variant(crate::Variant::new("Ok").field("code", FieldType::new("int32")));
1146 union_def.add_variant(crate::Variant::new("Err").field("msg", FieldType::new("string")));
1147 w.add_union(union_def);
1148
1149 let mut schema = Schema::new("Response");
1151 schema.add_field("id", FieldType::new("int32"));
1152 schema.add_field("status", FieldType::new("Status")); let mut obj = ObjectMap::new();
1155 obj.insert("id".to_string(), Value::Int(1));
1156 obj.insert("status".to_string(), Value::Tagged(
1157 "Ok".to_string(),
1158 Box::new(Value::Object({
1159 let mut m = ObjectMap::new();
1160 m.insert("code".to_string(), Value::Int(200));
1161 m
1162 })),
1163 ));
1164
1165 let arr = Value::Array(vec![Value::Object(obj)]);
1166 w.add_section("responses", &arr, Some(&schema)).unwrap();
1167 w.write(&path, false).unwrap();
1168
1169 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
1170 assert!(!r.unions.is_empty(), "Reader should have unions");
1172 assert_eq!(r.unions[0].name, "Status");
1173 assert!(!r.schemas.is_empty(), "Reader should have schemas");
1174 assert_eq!(r.schemas[0].name, "Response");
1175 let responses = r.get("responses").unwrap();
1176 let items = responses.as_array().unwrap();
1177 assert_eq!(items.len(), 1);
1178 let resp = items[0].as_object().unwrap();
1179 assert_eq!(resp.get("id").unwrap().as_int(), Some(1));
1180 if let Value::Tagged(tag, inner) = resp.get("status").unwrap() {
1181 assert_eq!(tag, "Ok");
1182 let obj = inner.as_object().unwrap();
1183 assert_eq!(obj.get("code").unwrap().as_int(), Some(200));
1184 } else {
1185 panic!("Expected Tagged value for status field");
1186 }
1187 std::fs::remove_file(&path).ok();
1188 }
1189
1190 #[test]
1195 fn test_object_encoding_deterministic() {
1196 let mut obj = ObjectMap::new();
1198 obj.insert("zebra".to_string(), Value::Int(1));
1199 obj.insert("alpha".to_string(), Value::Int(2));
1200 obj.insert("middle".to_string(), Value::Int(3));
1201
1202 let mut w1 = Writer::new();
1203 let (bytes1, _, _, _) = w1.encode_object(&obj).unwrap();
1204
1205 let mut w2 = Writer::new();
1206 let (bytes2, _, _, _) = w2.encode_object(&obj).unwrap();
1207
1208 assert_eq!(bytes1, bytes2, "Object encoding should be deterministic");
1209 }
1210}