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 let (d, _, _, _) = self.encode_value(value, None)?;
579 Ok(d)
580 }
581 }
582 _ => {
583 let (d, _, _, _) = self.encode_value(value, None)?;
585 Ok(d)
586 }
587 }
588 }
589
590 fn encode_object(&mut self, obj: &ObjectMap<String, Value>) -> Result<(Vec<u8>, TLType, bool, u32)> {
591 if obj.len() > MAX_OBJECT_FIELDS {
592 return Err(crate::Error::ValueOutOfRange(
593 format!("Object has {} fields, exceeds maximum {}", obj.len(), MAX_OBJECT_FIELDS)));
594 }
595 let mut buf = (obj.len() as u16).to_le_bytes().to_vec();
596 for (k, v) in obj {
597 buf.extend(self.intern(k).to_le_bytes());
598 let (d, t, _, _) = self.encode_value(v, None)?;
599 buf.push(t as u8);
600 buf.extend(d);
601 }
602 Ok((buf, TLType::Object, false, 0))
603 }
604}
605
606impl Default for Writer { fn default() -> Self { Self::new() } }
607
608fn checked_int_value(value: &Value, min: i64, max: i64, _type_name: &str) -> Result<i64> {
611 let i = match value {
612 Value::Int(i) => *i,
613 Value::UInt(u) if *u <= i64::MAX as u64 => *u as i64,
614 Value::UInt(_) => 0,
615 Value::Float(f) => {
616 let f = *f;
617 if f.is_finite() && f >= i64::MIN as f64 && f <= i64::MAX as f64 { f as i64 } else { 0 }
618 }
619 Value::JsonNumber(s) => s.parse::<i64>().unwrap_or(0),
620 _ => 0,
621 };
622 if i < min || i > max { Ok(0) } else { Ok(i) }
623}
624
625fn checked_uint_value(value: &Value, max: u64, _type_name: &str) -> Result<u64> {
628 let u = match value {
629 Value::UInt(u) => *u,
630 Value::Int(i) if *i >= 0 => *i as u64,
631 Value::Int(_) => 0,
632 Value::Float(f) => {
633 let f = *f;
634 if f.is_finite() && f >= 0.0 && f <= u64::MAX as f64 { f as u64 } else { 0 }
635 }
636 Value::JsonNumber(s) => s.parse::<u64>().unwrap_or(0),
637 _ => 0,
638 };
639 if u > max { Ok(0) } else { Ok(u) }
640}
641
642fn encode_int(i: i64) -> (Vec<u8>, TLType, bool, u32) {
643 if i >= i8::MIN as i64 && i <= i8::MAX as i64 { ((i as i8).to_le_bytes().to_vec(), TLType::Int8, false, 0) }
644 else if i >= i16::MIN as i64 && i <= i16::MAX as i64 { ((i as i16).to_le_bytes().to_vec(), TLType::Int16, false, 0) }
645 else if i >= i32::MIN as i64 && i <= i32::MAX as i64 { ((i as i32).to_le_bytes().to_vec(), TLType::Int32, false, 0) }
646 else { (i.to_le_bytes().to_vec(), TLType::Int64, false, 0) }
647}
648
649fn encode_uint(u: u64) -> (Vec<u8>, TLType, bool, u32) {
650 if u <= u8::MAX as u64 { ((u as u8).to_le_bytes().to_vec(), TLType::UInt8, false, 0) }
651 else if u <= u16::MAX as u64 { ((u as u16).to_le_bytes().to_vec(), TLType::UInt16, false, 0) }
652 else if u <= u32::MAX as u64 { ((u as u32).to_le_bytes().to_vec(), TLType::UInt32, false, 0) }
653 else { (u.to_le_bytes().to_vec(), TLType::UInt64, false, 0) }
654}
655
656fn write_varint(buf: &mut Vec<u8>, mut v: u64) {
657 while v >= 0x80 { buf.push(((v & 0x7F) | 0x80) as u8); v >>= 7; }
658 buf.push(v as u8);
659}
660
661fn compress_data(data: &[u8]) -> Result<Vec<u8>> {
662 use flate2::Compression;
663 use flate2::write::ZlibEncoder;
664 let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
665 e.write_all(data).map_err(crate::Error::Io)?;
666 e.finish().map_err(crate::Error::Io)
667}
668
669#[cfg(test)]
670mod tests {
671 use super::*;
672 use crate::reader::Reader;
673
674 #[test]
675 fn test_writer_default() {
676 let w = Writer::default();
677 assert_eq!(w.strings.len(), 0);
678 assert_eq!(w.schemas.len(), 0);
679 }
680
681 #[test]
682 fn test_encode_uint_ranges() {
683 let (data, tl_type, _, _) = encode_uint(42);
685 assert_eq!(tl_type, TLType::UInt8);
686 assert_eq!(data, vec![42u8]);
687
688 let (data, tl_type, _, _) = encode_uint(300);
690 assert_eq!(tl_type, TLType::UInt16);
691 assert_eq!(data, 300u16.to_le_bytes().to_vec());
692
693 let (data, tl_type, _, _) = encode_uint(100_000);
695 assert_eq!(tl_type, TLType::UInt32);
696 assert_eq!(data, 100_000u32.to_le_bytes().to_vec());
697
698 let (data, tl_type, _, _) = encode_uint(5_000_000_000);
700 assert_eq!(tl_type, TLType::UInt64);
701 assert_eq!(data, 5_000_000_000u64.to_le_bytes().to_vec());
702 }
703
704 #[test]
705 fn test_uint_value_roundtrip() {
706 let dir = std::env::temp_dir();
707 let path = dir.join("test_uint_roundtrip.tlbx");
708
709 let mut w = Writer::new();
710 w.add_section("small", &Value::UInt(42), None).unwrap();
711 w.add_section("medium", &Value::UInt(300), None).unwrap();
712 w.add_section("large", &Value::UInt(100_000), None).unwrap();
713 w.add_section("huge", &Value::UInt(5_000_000_000), None).unwrap();
714 w.write(&path, false).unwrap();
715
716 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
717 assert_eq!(r.get("small").unwrap().as_uint(), Some(42));
718 assert_eq!(r.get("medium").unwrap().as_uint(), Some(300));
719 assert_eq!(r.get("large").unwrap().as_uint(), Some(100_000));
720 assert_eq!(r.get("huge").unwrap().as_uint(), Some(5_000_000_000));
721 std::fs::remove_file(&path).ok();
722 }
723
724 #[test]
725 fn test_typed_schema_roundtrip() {
726 let dir = std::env::temp_dir();
728 let path = dir.join("test_typed_schema.tlbx");
729
730 let mut schema = Schema::new("TypedRecord");
731 schema.add_field("flag", FieldType::new("bool"));
732 schema.add_field("small_int", FieldType::new("int8"));
733 schema.add_field("med_int", FieldType::new("int16"));
734 schema.add_field("int32_val", FieldType::new("int"));
735 schema.add_field("int64_val", FieldType::new("int64"));
736 schema.add_field("small_uint", FieldType::new("uint8"));
737 schema.add_field("med_uint", FieldType::new("uint16"));
738 schema.add_field("uint32_val", FieldType::new("uint"));
739 schema.add_field("uint64_val", FieldType::new("uint64"));
740 schema.add_field("f32_val", FieldType::new("float32"));
741 schema.add_field("f64_val", FieldType::new("float"));
742 schema.add_field("name", FieldType::new("string"));
743 schema.add_field("data", FieldType::new("bytes"));
744
745 let mut w = Writer::new();
746 w.add_schema(schema.clone());
747
748 let mut obj = ObjectMap::new();
750 obj.insert("flag".to_string(), Value::Bool(true));
751 obj.insert("small_int".to_string(), Value::Int(42));
752 obj.insert("med_int".to_string(), Value::Int(1000));
753 obj.insert("int32_val".to_string(), Value::Int(50000));
754 obj.insert("int64_val".to_string(), Value::Int(1_000_000_000_000));
755 obj.insert("small_uint".to_string(), Value::UInt(200));
756 obj.insert("med_uint".to_string(), Value::UInt(40000));
757 obj.insert("uint32_val".to_string(), Value::UInt(3_000_000));
758 obj.insert("uint64_val".to_string(), Value::UInt(9_000_000_000));
759 obj.insert("f32_val".to_string(), Value::Float(3.14));
760 obj.insert("f64_val".to_string(), Value::Float(2.718281828));
761 obj.insert("name".to_string(), Value::String("test".into()));
762 obj.insert("data".to_string(), Value::Bytes(vec![0xDE, 0xAD]));
763
764 let arr = Value::Array(vec![Value::Object(obj)]);
765 w.add_section("records", &arr, Some(&schema)).unwrap();
766 w.write(&path, false).unwrap();
767
768 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
769 let records = r.get("records").unwrap();
770 let items = records.as_array().unwrap();
771 assert_eq!(items.len(), 1);
772
773 let rec = items[0].as_object().unwrap();
774 assert_eq!(rec.get("flag").unwrap().as_bool(), Some(true));
775 assert_eq!(rec.get("name").unwrap().as_str(), Some("test"));
776 std::fs::remove_file(&path).ok();
777 }
778
779 #[test]
780 fn test_typed_schema_array_field() {
781 let dir = std::env::temp_dir();
783 let path = dir.join("test_typed_array_field.tlbx");
784
785 let mut schema = Schema::new("WithArray");
786 schema.add_field("name", FieldType::new("string"));
787 schema.add_field("scores", FieldType::new("int").array());
788
789 let mut w = Writer::new();
790 w.add_schema(schema.clone());
791
792 let mut obj = ObjectMap::new();
793 obj.insert("name".to_string(), Value::String("Alice".into()));
794 obj.insert("scores".to_string(), Value::Array(vec![Value::Int(90), Value::Int(85)]));
795
796 let arr = Value::Array(vec![Value::Object(obj)]);
797 w.add_section("users", &arr, Some(&schema)).unwrap();
798 w.write(&path, false).unwrap();
799
800 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
801 let users = r.get("users").unwrap();
802 let items = users.as_array().unwrap();
803 assert_eq!(items.len(), 1);
804 let rec = items[0].as_object().unwrap();
805 assert_eq!(rec.get("name").unwrap().as_str(), Some("Alice"));
806 std::fs::remove_file(&path).ok();
807 }
808
809 #[test]
810 fn test_object_encoding_roundtrip() {
811 let dir = std::env::temp_dir();
813 let path = dir.join("test_object_enc.tlbx");
814
815 let mut obj = ObjectMap::new();
816 obj.insert("x".to_string(), Value::Int(10));
817 obj.insert("y".to_string(), Value::String("hello".into()));
818
819 let mut w = Writer::new();
820 w.add_section("data", &Value::Object(obj), None).unwrap();
821 w.write(&path, false).unwrap();
822
823 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
824 let val = r.get("data").unwrap();
825 let o = val.as_object().unwrap();
826 assert_eq!(o.get("x").unwrap().as_int(), Some(10));
827 assert_eq!(o.get("y").unwrap().as_str(), Some("hello"));
828 std::fs::remove_file(&path).ok();
829 }
830
831 #[test]
832 fn test_map_roundtrip_binary() {
833 let dir = std::env::temp_dir();
834 let path = dir.join("test_map_binary.tlbx");
835
836 let pairs = vec![
837 (Value::String("key1".into()), Value::Int(100)),
838 (Value::String("key2".into()), Value::Bool(true)),
839 ];
840
841 let mut w = Writer::new();
842 w.add_section("mapping", &Value::Map(pairs), None).unwrap();
843 w.write(&path, false).unwrap();
844
845 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
846 let val = r.get("mapping").unwrap();
847 if let Value::Map(pairs) = val {
848 assert_eq!(pairs.len(), 2);
849 } else {
850 panic!("Expected Map value");
851 }
852 std::fs::remove_file(&path).ok();
853 }
854
855 #[test]
856 fn test_ref_and_tagged_roundtrip() {
857 let dir = std::env::temp_dir();
858 let path = dir.join("test_ref_tagged.tlbx");
859
860 let mut w = Writer::new();
861 w.add_section("myref", &Value::Ref("some_ref".into()), None).unwrap();
862 w.add_section("mytag", &Value::Tagged("label".into(), Box::new(Value::Int(42))), None).unwrap();
863 w.add_section("myts", &Value::Timestamp(1700000000000, 0), None).unwrap();
864 w.write(&path, false).unwrap();
865
866 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
867
868 if let Value::Ref(s) = r.get("myref").unwrap() {
869 assert_eq!(s, "some_ref");
870 } else { panic!("Expected Ref"); }
871
872 if let Value::Tagged(tag, inner) = r.get("mytag").unwrap() {
873 assert_eq!(tag, "label");
874 assert_eq!(inner.as_int(), Some(42));
875 } else { panic!("Expected Tagged"); }
876
877 if let Value::Timestamp(ts, _) = r.get("myts").unwrap() {
878 assert_eq!(ts, 1700000000000);
879 } else { panic!("Expected Timestamp"); }
880
881 std::fs::remove_file(&path).ok();
882 }
883
884 #[test]
885 fn test_compressed_roundtrip() {
886 let dir = std::env::temp_dir();
887 let path = dir.join("test_compressed.tlbx");
888
889 let mut arr = Vec::new();
891 for i in 0..100 {
892 arr.push(Value::Int(i));
893 }
894
895 let mut w = Writer::new();
896 w.add_section("numbers", &Value::Array(arr), None).unwrap();
897 w.write(&path, true).unwrap();
898
899 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
900 let val = r.get("numbers").unwrap();
901 let items = val.as_array().unwrap();
902 assert_eq!(items.len(), 100);
903 assert_eq!(items[0].as_int(), Some(0));
904 assert_eq!(items[99].as_int(), Some(99));
905 std::fs::remove_file(&path).ok();
906 }
907
908 #[test]
909 fn test_root_array_flag() {
910 let dir = std::env::temp_dir();
911 let path = dir.join("test_root_array_flag.tlbx");
912
913 let mut w = Writer::new();
914 w.set_root_array(true);
915 w.add_section("root", &Value::Array(vec![Value::Int(1)]), None).unwrap();
916 w.write(&path, false).unwrap();
917
918 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
919 assert!(r.is_root_array());
920 std::fs::remove_file(&path).ok();
921 }
922
923 #[test]
924 fn test_bytes_value_roundtrip() {
925 let dir = std::env::temp_dir();
926 let path = dir.join("test_bytes_val.tlbx");
927
928 let mut w = Writer::new();
929 w.add_section("blob", &Value::Bytes(vec![1, 2, 3, 4, 5]), None).unwrap();
930 w.write(&path, false).unwrap();
931
932 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
933 let val = r.get("blob").unwrap();
934 assert_eq!(val.as_bytes(), Some(&[1u8, 2, 3, 4, 5][..]));
935 std::fs::remove_file(&path).ok();
936 }
937
938 #[test]
939 fn test_nested_struct_schema_roundtrip() {
940 let dir = std::env::temp_dir();
942 let path = dir.join("test_nested_struct.tlbx");
943
944 let mut inner_schema = Schema::new("Address");
945 inner_schema.add_field("city", FieldType::new("string"));
946 inner_schema.add_field("zip", FieldType::new("string"));
947
948 let mut outer_schema = Schema::new("Person");
949 outer_schema.add_field("name", FieldType::new("string"));
950 outer_schema.add_field("home", FieldType::new("Address"));
951
952 let mut w = Writer::new();
953 w.add_schema(inner_schema.clone());
954 w.add_schema(outer_schema.clone());
955
956 let mut addr = ObjectMap::new();
957 addr.insert("city".to_string(), Value::String("Seattle".into()));
958 addr.insert("zip".to_string(), Value::String("98101".into()));
959
960 let mut person = ObjectMap::new();
961 person.insert("name".to_string(), Value::String("Alice".into()));
962 person.insert("home".to_string(), Value::Object(addr));
963
964 let arr = Value::Array(vec![Value::Object(person)]);
965 w.add_section("people", &arr, Some(&outer_schema)).unwrap();
966 w.write(&path, false).unwrap();
967
968 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
969 let people = r.get("people").unwrap();
970 let items = people.as_array().unwrap();
971 assert_eq!(items.len(), 1);
972 let p = items[0].as_object().unwrap();
973 assert_eq!(p.get("name").unwrap().as_str(), Some("Alice"));
974 std::fs::remove_file(&path).ok();
975 }
976
977 #[test]
978 fn test_timestamp_typed_field() {
979 let dir = std::env::temp_dir();
981 let path = dir.join("test_ts_typed.tlbx");
982
983 let mut schema = Schema::new("Event");
984 schema.add_field("name", FieldType::new("string"));
985 schema.add_field("ts", FieldType::new("timestamp"));
986
987 let mut w = Writer::new();
988 w.add_schema(schema.clone());
989
990 let mut obj = ObjectMap::new();
991 obj.insert("name".to_string(), Value::String("deploy".into()));
992 obj.insert("ts".to_string(), Value::Timestamp(1700000000000, 0));
993
994 let arr = Value::Array(vec![Value::Object(obj)]);
995 w.add_section("events", &arr, Some(&schema)).unwrap();
996 w.write(&path, false).unwrap();
997
998 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
999 let events = r.get("events").unwrap();
1000 let items = events.as_array().unwrap();
1001 assert_eq!(items.len(), 1);
1002 std::fs::remove_file(&path).ok();
1003 }
1004
1005 #[test]
1006 fn test_bytes_typed_field() {
1007 let dir = std::env::temp_dir();
1009 let path = dir.join("test_bytes_typed.tlbx");
1010
1011 let mut schema = Schema::new("Blob");
1012 schema.add_field("name", FieldType::new("string"));
1013 schema.add_field("data", FieldType::new("bytes"));
1014
1015 let mut w = Writer::new();
1016 w.add_schema(schema.clone());
1017
1018 let mut obj = ObjectMap::new();
1019 obj.insert("name".to_string(), Value::String("img".into()));
1020 obj.insert("data".to_string(), Value::Bytes(vec![0xDE, 0xAD, 0xBE, 0xEF]));
1021
1022 let arr = Value::Array(vec![Value::Object(obj)]);
1023 w.add_section("blobs", &arr, Some(&schema)).unwrap();
1024 w.write(&path, false).unwrap();
1025
1026 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
1027 let blobs = r.get("blobs").unwrap();
1028 let items = blobs.as_array().unwrap();
1029 assert_eq!(items.len(), 1);
1030 std::fs::remove_file(&path).ok();
1031 }
1032
1033 #[test]
1038 fn test_checked_int_value_in_range() {
1039 assert_eq!(checked_int_value(&Value::Int(42), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 42);
1040 assert_eq!(checked_int_value(&Value::Int(-128), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), -128);
1041 assert_eq!(checked_int_value(&Value::Int(127), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 127);
1042 assert_eq!(checked_int_value(&Value::UInt(100), i16::MIN as i64, i16::MAX as i64, "int16").unwrap(), 100);
1043 }
1044
1045 #[test]
1046 fn test_checked_int_value_overflow_defaults_to_zero() {
1047 assert_eq!(checked_int_value(&Value::Int(128), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1049 }
1050
1051 #[test]
1052 fn test_checked_int_value_underflow_defaults_to_zero() {
1053 assert_eq!(checked_int_value(&Value::Int(-129), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1054 }
1055
1056 #[test]
1057 fn test_checked_int_value_float_coercion() {
1058 assert_eq!(checked_int_value(&Value::Float(42.7), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 42);
1060 assert_eq!(checked_int_value(&Value::Float(-3.9), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), -3);
1061 assert_eq!(checked_int_value(&Value::Float(f64::NAN), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1063 assert_eq!(checked_int_value(&Value::Float(f64::INFINITY), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1064 assert_eq!(checked_int_value(&Value::Float(200.0), i8::MIN as i64, i8::MAX as i64, "int8").unwrap(), 0);
1066 }
1067
1068 #[test]
1069 fn test_checked_uint_value_in_range() {
1070 assert_eq!(checked_uint_value(&Value::UInt(255), u8::MAX as u64, "uint8").unwrap(), 255);
1071 assert_eq!(checked_uint_value(&Value::Int(42), u8::MAX as u64, "uint8").unwrap(), 42);
1072 }
1073
1074 #[test]
1075 fn test_checked_uint_value_overflow_defaults_to_zero() {
1076 assert_eq!(checked_uint_value(&Value::UInt(256), u8::MAX as u64, "uint8").unwrap(), 0);
1078 }
1079
1080 #[test]
1081 fn test_checked_uint_value_negative_defaults_to_zero() {
1082 assert_eq!(checked_uint_value(&Value::Int(-1), u8::MAX as u64, "uint8").unwrap(), 0);
1084 }
1085
1086 #[test]
1087 fn test_checked_uint_value_float_coercion() {
1088 assert_eq!(checked_uint_value(&Value::Float(42.7), u8::MAX as u64, "uint8").unwrap(), 42);
1089 assert_eq!(checked_uint_value(&Value::Float(-1.0), u8::MAX as u64, "uint8").unwrap(), 0);
1090 assert_eq!(checked_uint_value(&Value::Float(f64::NAN), u8::MAX as u64, "uint8").unwrap(), 0);
1091 assert_eq!(checked_uint_value(&Value::Float(300.0), u8::MAX as u64, "uint8").unwrap(), 0);
1092 }
1093
1094 #[test]
1099 fn test_union_field_roundtrip() {
1100 let dir = std::env::temp_dir();
1102 let path = dir.join("test_union_field_rt.tlbx");
1103
1104 let mut w = Writer::new();
1105
1106 let mut union_def = crate::Union::new("Shape");
1108 union_def.add_variant(crate::Variant::new("Circle").field("radius", FieldType::new("float64")));
1109 union_def.add_variant(crate::Variant::new("Rect").field("w", FieldType::new("float64")).field("h", FieldType::new("float64")));
1110 w.add_union(union_def);
1111
1112 let tagged = Value::Tagged(
1114 "Circle".to_string(),
1115 Box::new(Value::Object({
1116 let mut m = ObjectMap::new();
1117 m.insert("radius".to_string(), Value::Float(5.0));
1118 m
1119 })),
1120 );
1121 w.add_section("shape", &tagged, None).unwrap();
1122 w.write(&path, false).unwrap();
1123
1124 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
1125 let shape = r.get("shape").unwrap();
1126 if let Value::Tagged(tag, inner) = &shape {
1127 assert_eq!(tag, "Circle");
1128 let obj = inner.as_object().unwrap();
1129 assert_eq!(obj.get("radius").unwrap().as_float(), Some(5.0));
1130 } else {
1131 panic!("Expected Tagged value, got {:?}", shape);
1132 }
1133 std::fs::remove_file(&path).ok();
1134 }
1135
1136 #[test]
1137 fn test_union_typed_schema_field_roundtrip() {
1138 let dir = std::env::temp_dir();
1140 let path = dir.join("test_union_schema_field.tlbx");
1141
1142 let mut w = Writer::new();
1143
1144 let mut union_def = crate::Union::new("Status");
1146 union_def.add_variant(crate::Variant::new("Ok").field("code", FieldType::new("int32")));
1147 union_def.add_variant(crate::Variant::new("Err").field("msg", FieldType::new("string")));
1148 w.add_union(union_def);
1149
1150 let mut schema = Schema::new("Response");
1152 schema.add_field("id", FieldType::new("int32"));
1153 schema.add_field("status", FieldType::new("Status")); let mut obj = ObjectMap::new();
1156 obj.insert("id".to_string(), Value::Int(1));
1157 obj.insert("status".to_string(), Value::Tagged(
1158 "Ok".to_string(),
1159 Box::new(Value::Object({
1160 let mut m = ObjectMap::new();
1161 m.insert("code".to_string(), Value::Int(200));
1162 m
1163 })),
1164 ));
1165
1166 let arr = Value::Array(vec![Value::Object(obj)]);
1167 w.add_section("responses", &arr, Some(&schema)).unwrap();
1168 w.write(&path, false).unwrap();
1169
1170 let r = Reader::from_bytes(std::fs::read(&path).unwrap()).unwrap();
1171 assert!(!r.unions.is_empty(), "Reader should have unions");
1173 assert_eq!(r.unions[0].name, "Status");
1174 assert!(!r.schemas.is_empty(), "Reader should have schemas");
1175 assert_eq!(r.schemas[0].name, "Response");
1176 let responses = r.get("responses").unwrap();
1177 let items = responses.as_array().unwrap();
1178 assert_eq!(items.len(), 1);
1179 let resp = items[0].as_object().unwrap();
1180 assert_eq!(resp.get("id").unwrap().as_int(), Some(1));
1181 if let Value::Tagged(tag, inner) = resp.get("status").unwrap() {
1182 assert_eq!(tag, "Ok");
1183 let obj = inner.as_object().unwrap();
1184 assert_eq!(obj.get("code").unwrap().as_int(), Some(200));
1185 } else {
1186 panic!("Expected Tagged value for status field");
1187 }
1188 std::fs::remove_file(&path).ok();
1189 }
1190
1191 #[test]
1196 fn test_object_encoding_deterministic() {
1197 let mut obj = ObjectMap::new();
1199 obj.insert("zebra".to_string(), Value::Int(1));
1200 obj.insert("alpha".to_string(), Value::Int(2));
1201 obj.insert("middle".to_string(), Value::Int(3));
1202
1203 let mut w1 = Writer::new();
1204 let (bytes1, _, _, _) = w1.encode_object(&obj).unwrap();
1205
1206 let mut w2 = Writer::new();
1207 let (bytes2, _, _, _) = w2.encode_object(&obj).unwrap();
1208
1209 assert_eq!(bytes1, bytes2, "Object encoding should be deterministic");
1210 }
1211}