sheeter-engine 1.1.0

Convert, inspect, and transform data files across ten formats with streaming where it matters and auto-detection where it can.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
use std::error::Error;
use std::io::Cursor;

/// Format-agnostic intermediate value used by the Sheeter hub.
///
/// Every reader (CSV, TSV, JSON, YAML, XML) produces a `CanonicalValue`, and
/// every writer consumes one. Adding a format requires exactly one reader and
/// one writer, not `N-1` pairwise converters, because every spoke composes
/// through this hub.
///
/// Tabular data is represented as `Array<Object>`. XML documents are
/// represented as a single-key `Object` whose key is the root element name,
/// with attributes prefixed by `@` and text stored under `#text`. Repeated
/// sibling elements become `Array`.
///
/// # Examples
///
/// ```
/// use sheeter_engine::CanonicalValue;
///
/// let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
/// assert_eq!(v.to_csv().unwrap(), "a\n1\n");
/// ```
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum CanonicalValue {
    Null,
    Bool(bool),
    Number(serde_json::Number),
    String(String),
    Bytes(Vec<u8>),
    Array(Vec<CanonicalValue>),
    Object(Vec<(String, CanonicalValue)>),
}

type XmlStackFrame = (
    String,
    Vec<(String, String)>,
    Vec<(String, CanonicalValue)>,
    String,
);

impl CanonicalValue {
    pub(crate) fn from_json_value(v: serde_json::Value) -> Self {
        match v {
            serde_json::Value::Null => Self::Null,
            serde_json::Value::Bool(b) => Self::Bool(b),
            serde_json::Value::Number(n) => Self::Number(n),
            serde_json::Value::String(s) => Self::String(s),
            serde_json::Value::Array(a) => {
                Self::Array(a.into_iter().map(Self::from_json_value).collect())
            }
            serde_json::Value::Object(m) => {
                if m.len() == 1 {
                    if let Some(serde_json::Value::String(b64)) = m.get("$binary") {
                        use base64::Engine;
                        if let Ok(bytes) = base64::engine::general_purpose::STANDARD.decode(b64) {
                            return Self::Bytes(bytes);
                        }
                    }
                }
                Self::Object(
                    m.into_iter()
                        .map(|(k, v)| (k, Self::from_json_value(v)))
                        .collect(),
                )
            }
        }
    }

    pub(crate) fn to_json_value(&self) -> serde_json::Value {
        match self {
            Self::Null => serde_json::Value::Null,
            Self::Bool(b) => serde_json::Value::Bool(*b),
            Self::Number(n) => serde_json::Value::Number(n.clone()),
            Self::String(s) => serde_json::Value::String(s.clone()),
            Self::Bytes(b) => {
                use base64::Engine;
                let mut map = serde_json::Map::new();
                map.insert(
                    "$binary".to_string(),
                    serde_json::Value::String(base64::engine::general_purpose::STANDARD.encode(b)),
                );
                serde_json::Value::Object(map)
            }
            Self::Array(a) => {
                serde_json::Value::Array(a.iter().map(|v| v.to_json_value()).collect())
            }
            Self::Object(o) => {
                let mut map = serde_json::Map::new();
                for (k, v) in o {
                    map.insert(k.clone(), v.to_json_value());
                }
                serde_json::Value::Object(map)
            }
        }
    }

    /// Parses JSON into the canonical hub.
    ///
    /// # Errors
    /// Returns an error if the JSON is malformed.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// assert!(CanonicalValue::from_json(r#"{"a":1}"#).is_ok());
    /// ```
    pub fn from_json(input: &str) -> Result<Self, Box<dyn Error>> {
        Ok(Self::from_json_value(serde_json::from_str(input)?))
    }

    /// Serializes the canonical hub to JSON.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::String("x".into());
    /// assert_eq!(v.to_json().unwrap(), r#""x""#);
    /// ```
    pub fn to_json(&self) -> Result<String, Box<dyn Error>> {
        Ok(serde_json::to_string(&self.to_json_value())?)
    }

    fn from_delimited(input: &str, delim: u8) -> Result<Self, Box<dyn Error>> {
        let input = input.strip_prefix('\u{feff}').unwrap_or(input);
        let mut rdr = csv::ReaderBuilder::new()
            .delimiter(delim)
            .from_reader(Cursor::new(input));
        let headers = rdr.headers()?.clone();
        let mut seen = std::collections::HashSet::new();
        for h in headers.iter() {
            if !seen.insert(h) {
                return Err(format!("duplicate header: {h}").into());
            }
        }
        let mut rows = Vec::new();
        for result in rdr.records() {
            let record = result?;
            let mut obj = Vec::with_capacity(headers.len());
            for (h, v) in headers.iter().zip(record.iter()) {
                obj.push((h.to_string(), Self::String(v.to_string())));
            }
            rows.push(Self::Object(obj));
        }
        Ok(Self::Array(rows))
    }

    fn to_delimited(&self, delim: u8) -> Result<String, Box<dyn Error>> {
        let rows = self.as_tabular()?;
        if rows.is_empty() {
            return Ok(String::new());
        }
        let mut headers: Vec<String> = Vec::new();
        for row in &rows {
            let obj = row.as_object()?;
            for (k, _) in obj {
                if !headers.contains(k) {
                    headers.push(k.clone());
                }
            }
        }
        let mut wtr = csv::WriterBuilder::new()
            .delimiter(delim)
            .from_writer(vec![]);
        wtr.write_record(&headers)?;
        for row in &rows {
            let obj = row.as_object()?;
            let mut record = Vec::with_capacity(headers.len());
            for h in &headers {
                let cell = match obj.iter().find(|(k, _)| k == h) {
                    Some((_, Self::Null)) | None => String::new(),
                    Some((_, Self::Bool(b))) => b.to_string(),
                    Some((_, Self::Number(n))) => n.to_string(),
                    Some((_, Self::String(s))) => s.clone(),
                    Some((_, Self::Bytes(b))) => {
                        use base64::Engine;
                        base64::engine::general_purpose::STANDARD.encode(b)
                    }
                    Some((_, Self::Array(_))) | Some((_, Self::Object(_))) => {
                        return Err(format!(
                            "nested value at key {h} cannot be represented in tabular output"
                        )
                        .into());
                    }
                };
                record.push(cell);
            }
            wtr.write_record(&record)?;
        }
        let data = wtr.into_inner()?;
        Ok(String::from_utf8(data)?)
    }

    pub fn from_toml(input: &str) -> Result<Self, Box<dyn Error>> {
        let value: toml::Value = toml::from_str(input)?;
        Ok(Self::from_toml_value(value))
    }

    fn from_toml_value(v: toml::Value) -> Self {
        match v {
            toml::Value::String(s) => Self::String(s),
            toml::Value::Integer(i) => Self::Number(serde_json::Number::from(i)),
            toml::Value::Float(f) => match serde_json::Number::from_f64(f) {
                Some(n) => Self::Number(n),
                None => Self::String(f.to_string()),
            },
            toml::Value::Boolean(b) => Self::Bool(b),
            toml::Value::Datetime(dt) => Self::String(dt.to_string()),
            toml::Value::Array(a) => {
                Self::Array(a.into_iter().map(Self::from_toml_value).collect())
            }
            toml::Value::Table(t) => Self::Object(
                t.into_iter()
                    .map(|(k, v)| (k, Self::from_toml_value(v)))
                    .collect(),
            ),
        }
    }

    pub fn to_toml(&self) -> Result<String, Box<dyn Error>> {
        let value = match self {
            Self::Array(_) => {
                let mut table = toml::map::Map::new();
                table.insert("item".to_string(), self.to_toml_value()?);
                toml::Value::Table(table)
            }
            Self::Object(_) => self.to_toml_value()?,
            _ => {
                let mut table = toml::map::Map::new();
                table.insert("value".to_string(), self.to_toml_value()?);
                toml::Value::Table(table)
            }
        };
        Ok(toml::to_string_pretty(&value)?)
    }

    fn to_toml_value(&self) -> Result<toml::Value, Box<dyn Error>> {
        Ok(match self {
            Self::Null => return Err("TOML has no null type".into()),
            Self::Bool(b) => toml::Value::Boolean(*b),
            Self::Number(n) => {
                if let Some(i) = n.as_i64() {
                    toml::Value::Integer(i)
                } else if let Some(u) = n.as_u64() {
                    if u <= i64::MAX as u64 {
                        toml::Value::Integer(u as i64)
                    } else {
                        return Err(format!("TOML integer out of range: {u}").into());
                    }
                } else if let Some(f) = n.as_f64() {
                    toml::Value::Float(f)
                } else {
                    return Err(format!("cannot represent number {n} in TOML").into());
                }
            }
            Self::String(s) => toml::Value::String(s.clone()),
            Self::Bytes(b) => {
                use base64::Engine;
                toml::Value::String(base64::engine::general_purpose::STANDARD.encode(b))
            }
            Self::Array(a) => {
                let mut items = Vec::with_capacity(a.len());
                for item in a {
                    if let Self::Null = item {
                        return Err("TOML arrays cannot contain null".into());
                    }
                    items.push(item.to_toml_value()?);
                }
                toml::Value::Array(items)
            }
            Self::Object(o) => {
                let mut table = toml::map::Map::new();
                for (k, v) in o {
                    if let Self::Null = v {
                        continue;
                    }
                    table.insert(k.clone(), v.to_toml_value()?);
                }
                toml::Value::Table(table)
            }
        })
    }

    pub fn from_xlsx_workbook(bytes: &[u8]) -> Result<Vec<(String, Self)>, Box<dyn Error>> {
        use calamine::{Reader, Xlsx};
        let cursor = Cursor::new(bytes.to_vec());
        let mut workbook: Xlsx<_> = Xlsx::new(cursor)?;
        let names = workbook.sheet_names().to_owned();
        let mut out = Vec::with_capacity(names.len());
        for name in names {
            let range = workbook
                .worksheet_range(&name)
                .map_err(|e| format!("failed to read sheet {name}: {e:?}"))?;
            out.push((name, Self::from_calamine_range(&range)?));
        }
        Ok(out)
    }

    pub(crate) fn from_calamine_range(
        range: &calamine::Range<calamine::Data>,
    ) -> Result<Self, Box<dyn Error>> {
        use calamine::Data;
        let mut rows = range.rows();
        let header = match rows.next() {
            Some(h) => h.iter().map(cell_to_string).collect::<Vec<String>>(),
            None => return Ok(Self::Array(Vec::new())),
        };
        let mut records = Vec::new();
        for row in rows {
            if row.iter().all(|c| matches!(c, Data::Empty)) {
                continue;
            }
            let mut obj = Vec::with_capacity(header.len());
            for (i, h) in header.iter().enumerate() {
                let val = match row.get(i) {
                    Some(Data::Empty) | None => Self::Null,
                    Some(Data::String(s)) => Self::String(s.clone()),
                    Some(Data::Float(f)) => {
                        if f.is_finite() && f.fract() == 0.0 && f.abs() < i64::MAX as f64 {
                            Self::Number(serde_json::Number::from(*f as i64))
                        } else {
                            match serde_json::Number::from_f64(*f) {
                                Some(n) => Self::Number(n),
                                None => Self::String(f.to_string()),
                            }
                        }
                    }
                    Some(Data::Int(i)) => Self::Number(serde_json::Number::from(*i)),
                    Some(Data::Bool(b)) => Self::Bool(*b),
                    Some(Data::DateTime(dt)) => Self::String(dt.to_string()),
                    Some(Data::DateTimeIso(s)) => Self::String(s.clone()),
                    Some(Data::DurationIso(s)) => Self::String(s.clone()),
                    Some(Data::Error(e)) => Self::String(format!("{e:?}")),
                };
                obj.push((h.clone(), val));
            }
            records.push(Self::Object(obj));
        }
        Ok(Self::Array(records))
    }

    pub fn to_xlsx_workbook(sheets: &[(String, Self)]) -> Result<Vec<u8>, Box<dyn Error>> {
        use rust_xlsxwriter::Workbook;
        let mut wb = Workbook::new();
        for (name, value) in sheets {
            let ws = wb.add_worksheet();
            ws.set_name(name.as_str())?;
            let rows = value.as_tabular()?;
            if rows.is_empty() {
                continue;
            }
            let mut headers: Vec<String> = Vec::new();
            for row in &rows {
                let obj = row.as_object()?;
                for (k, _) in obj {
                    if !headers.contains(k) {
                        headers.push(k.clone());
                    }
                }
            }
            for (col, h) in headers.iter().enumerate() {
                ws.write_string(0, col as u16, h)?;
            }
            for (i, row) in rows.iter().enumerate() {
                let obj = row.as_object()?;
                for (col, h) in headers.iter().enumerate() {
                    let row_idx = (i + 1) as u32;
                    let col_idx = col as u16;
                    match obj.iter().find(|(k, _)| k == h) {
                        Some((_, Self::String(s))) => {
                            ws.write_string(row_idx, col_idx, s)?;
                        }
                        Some((_, Self::Number(n))) => {
                            if let Some(f) = n.as_f64() {
                                ws.write_number(row_idx, col_idx, f)?;
                            } else {
                                ws.write_string(row_idx, col_idx, n.to_string())?;
                            }
                        }
                        Some((_, Self::Bool(b))) => {
                            ws.write_boolean(row_idx, col_idx, *b)?;
                        }
                        Some((_, Self::Null)) | None => {}
                        Some((_, Self::Bytes(b))) => {
                            use base64::Engine;
                            let s = base64::engine::general_purpose::STANDARD.encode(b);
                            ws.write_string(row_idx, col_idx, &s)?;
                        }
                        Some((_, Self::Array(_))) | Some((_, Self::Object(_))) => {
                            return Err(format!(
                                "nested value at key {h} cannot be represented in XLSX"
                            )
                            .into());
                        }
                    }
                }
            }
        }
        Ok(wb.save_to_buffer()?)
    }

    pub fn from_sql_workbook(
        input: &str,
        dialect: crate::SqlDialect,
    ) -> Result<Vec<(String, Self)>, Box<dyn Error>> {
        use sqlparser::ast::{
            Expr, SetExpr, Statement, TableObject, Value as SqlValue, ValueWithSpan, Values,
        };
        use sqlparser::dialect::{GenericDialect, MySqlDialect, PostgreSqlDialect, SQLiteDialect};
        use sqlparser::parser::Parser;

        let statements = match dialect {
            crate::SqlDialect::Generic => Parser::parse_sql(&GenericDialect {}, input)?,
            crate::SqlDialect::PostgreSql => Parser::parse_sql(&PostgreSqlDialect {}, input)?,
            crate::SqlDialect::Sqlite => Parser::parse_sql(&SQLiteDialect {}, input)?,
            crate::SqlDialect::MySql => Parser::parse_sql(&MySqlDialect {}, input)?,
        };

        let mut schemas: std::collections::HashMap<String, Vec<String>> =
            std::collections::HashMap::new();
        let mut tables: Vec<SqlTable> = Vec::new();

        for statement in statements {
            match statement {
                Statement::CreateTable(create) => {
                    let name = clean_ident(&create.name.to_string());
                    let cols: Vec<String> = create
                        .columns
                        .iter()
                        .map(|c| clean_ident(&c.name.to_string()))
                        .collect();
                    schemas.insert(name, cols);
                }
                Statement::Insert(insert) => {
                    let table = match &insert.table {
                        TableObject::TableName(n) => clean_ident(&n.to_string()),
                        _ => {
                            return Err(
                                "INSERT into table functions or queries is not supported".into()
                            );
                        }
                    };
                    let source = insert
                        .source
                        .as_ref()
                        .ok_or("INSERT without a source is not supported")?;
                    let rows = match source.body.as_ref() {
                        SetExpr::Values(Values { rows, .. }) => rows,
                        _ => {
                            return Err(
                                "only INSERT ... VALUES is supported for data extraction".into()
                            );
                        }
                    };
                    let explicit_cols: Vec<String> = insert
                        .columns
                        .iter()
                        .map(|c| clean_ident(&c.to_string()))
                        .collect();
                    let cols = if explicit_cols.is_empty() {
                        schemas.get(&table).cloned().unwrap_or_else(|| {
                            (0..rows.first().map(|r| r.len()).unwrap_or(0))
                                .map(|i| format!("column_{}", i + 1))
                                .collect()
                        })
                    } else {
                        explicit_cols
                    };
                    let slot = match tables.iter_mut().find(|(n, _)| n == &table) {
                        Some((_, rows)) => rows,
                        None => {
                            tables.push((table.clone(), Vec::new()));
                            &mut tables.last_mut().expect("just pushed").1
                        }
                    };
                    for row in rows {
                        let mut obj = Vec::with_capacity(cols.len());
                        for (i, expr) in row.iter().enumerate() {
                            let key = cols
                                .get(i)
                                .cloned()
                                .unwrap_or_else(|| format!("column_{}", i + 1));
                            let value = match expr {
                                Expr::Value(ValueWithSpan { value, .. }) => match value {
                                    SqlValue::Number(n, _) => {
                                        if let Ok(i) = n.parse::<i64>() {
                                            Self::Number(serde_json::Number::from(i))
                                        } else if let Ok(f) = n.parse::<f64>() {
                                            match serde_json::Number::from_f64(f) {
                                                Some(n) => Self::Number(n),
                                                None => Self::String(n.clone()),
                                            }
                                        } else {
                                            Self::String(n.clone())
                                        }
                                    }
                                    SqlValue::SingleQuotedString(s)
                                    | SqlValue::DoubleQuotedString(s)
                                    | SqlValue::EscapedStringLiteral(s) => Self::String(s.clone()),
                                    SqlValue::Boolean(b) => Self::Bool(*b),
                                    SqlValue::Null => Self::Null,
                                    other => Self::String(other.to_string()),
                                },
                                Expr::Identifier(id) => Self::String(id.value.clone()),
                                other => Self::String(other.to_string()),
                            };
                            obj.push((key, value));
                        }
                        slot.push(obj);
                    }
                }
                _ => {}
            }
        }

        if tables.is_empty() {
            return Err("no INSERT statements with literal VALUES found".into());
        }

        Ok(tables
            .into_iter()
            .map(|(name, rows)| {
                let records = rows.into_iter().map(Self::Object).collect();
                (name, Self::Array(records))
            })
            .collect())
    }

    pub fn to_sql_workbook(sheets: &[(String, Self)]) -> Result<String, Box<dyn Error>> {
        let mut out = String::new();
        for (table, value) in sheets {
            out.push_str(&value.to_sql_with_table(table)?);
        }
        Ok(out)
    }

    pub fn to_sql_with_table(&self, table: &str) -> Result<String, Box<dyn Error>> {
        let rows = self.as_tabular()?;
        if rows.is_empty() {
            return Ok(String::new());
        }
        let mut headers: Vec<String> = Vec::new();
        for row in &rows {
            let obj = row.as_object()?;
            for (k, _) in obj {
                if !headers.contains(k) {
                    headers.push(k.clone());
                }
            }
        }
        let cols = headers
            .iter()
            .map(|h| quote_ident(h))
            .collect::<Vec<_>>()
            .join(", ");
        let mut out = String::new();
        for row in &rows {
            let obj = row.as_object()?;
            let mut values = Vec::with_capacity(headers.len());
            for h in &headers {
                let cell = match obj.iter().find(|(k, _)| k == h) {
                    Some((_, Self::Null)) | None => "NULL".to_string(),
                    Some((_, Self::Bool(b))) => b.to_string().to_uppercase(),
                    Some((_, Self::Number(n))) => n.to_string(),
                    Some((_, Self::String(s))) => format!("'{}'", s.replace('\'', "''")),
                    Some((_, Self::Bytes(b))) => {
                        format!(
                            "X'{}'",
                            b.iter()
                                .map(|byte| format!("{byte:02x}"))
                                .collect::<String>()
                        )
                    }
                    Some((_, Self::Array(_))) | Some((_, Self::Object(_))) => {
                        return Err(format!(
                            "nested value at key {h} cannot be represented in SQL"
                        )
                        .into());
                    }
                };
                values.push(cell);
            }
            out.push_str(&format!(
                "INSERT INTO {} ({}) VALUES ({});\n",
                quote_ident(table),
                cols,
                values.join(", ")
            ));
        }
        Ok(out)
    }

    fn as_object(&self) -> Result<&Vec<(String, CanonicalValue)>, Box<dyn Error>> {
        match self {
            Self::Object(o) => Ok(o),
            _ => Err("expected object".into()),
        }
    }

    pub(crate) fn as_tabular(&self) -> Result<Vec<&CanonicalValue>, Box<dyn Error>> {
        if let Some(arr) = find_first_object_array(self) {
            return Ok(arr.iter().collect());
        }
        let mut current = self;
        while let Self::Object(o) = current {
            if o.len() != 1 {
                break;
            }
            match &o[0].1 {
                Self::Object(_) => current = &o[0].1,
                _ => break,
            }
        }
        if let Self::Object(o) = current {
            if o.iter().all(|(_, v)| is_scalar(v)) {
                return Ok(vec![current]);
            }
        }
        Err("cannot determine tabular structure".into())
    }

    /// Parses CSV into the canonical hub.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_csv("a\n1").unwrap();
    /// assert_eq!(v.to_json().unwrap(), r#"[{"a":"1"}]"#);
    /// ```
    pub fn from_csv(input: &str) -> Result<Self, Box<dyn Error>> {
        Self::from_delimited(input, b',')
    }

    /// Serializes the canonical hub to CSV.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
    /// assert_eq!(v.to_csv().unwrap(), "a\n1\n");
    /// ```
    pub fn to_csv(&self) -> Result<String, Box<dyn Error>> {
        self.to_delimited(b',')
    }

    /// Parses TSV into the canonical hub.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_tsv("a\tb\n1\t2").unwrap();
    /// assert_eq!(v.to_json().unwrap(), r#"[{"a":"1","b":"2"}]"#);
    /// ```
    pub fn from_tsv(input: &str) -> Result<Self, Box<dyn Error>> {
        Self::from_delimited(input, b'\t')
    }

    /// Serializes the canonical hub to TSV.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
    /// assert_eq!(v.to_tsv().unwrap(), "a\n1\n");
    /// ```
    pub fn to_tsv(&self) -> Result<String, Box<dyn Error>> {
        self.to_delimited(b'\t')
    }

    /// Parses YAML into the canonical hub.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_yaml("- a: 1\n").unwrap();
    /// assert_eq!(v.to_json().unwrap(), r#"[{"a":1}]"#);
    /// ```
    pub fn from_yaml(input: &str) -> Result<Self, Box<dyn Error>> {
        let opts = serde_saphyr::options! {
            strict_booleans: true,
            ignore_binary_tag_for_string: true,
        };
        let v: serde_json::Value = serde_saphyr::from_str_with_options(input, opts)?;
        Ok(Self::from_json_value(v))
    }

    /// Serializes the canonical hub to YAML.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
    /// assert!(v.to_yaml().unwrap().contains("a: 1"));
    /// ```
    pub fn to_yaml(&self) -> Result<String, Box<dyn Error>> {
        Ok(serde_saphyr::to_string(&self.to_json_value())?)
    }

    /// Parses XML into the canonical hub.
    ///
    /// Attributes become `@name` keys, text content becomes `#text`, and
    /// repeated sibling elements become arrays. The returned value is an
    /// object with exactly one key: the root element name.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_xml("<r><a>1</a><a>2</a></r>").unwrap();
    /// assert_eq!(v.to_json().unwrap(), r#"{"r":{"a":["1","2"]}}"#);
    /// ```
    pub fn from_xml(input: &str) -> Result<Self, Box<dyn Error>> {
        use quick_xml::events::Event;
        use quick_xml::Reader;

        let mut reader = Reader::from_str(input);
        reader.config_mut().trim_text(true);
        let mut stack: Vec<XmlStackFrame> = Vec::new();
        let mut root: Option<(String, CanonicalValue)> = None;

        loop {
            match reader.read_event() {
                Ok(Event::Start(e)) => {
                    let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
                    let mut attrs = Vec::new();
                    for a in e.attributes() {
                        let a = a?;
                        attrs.push((
                            String::from_utf8_lossy(a.key.as_ref()).to_string(),
                            a.unescape_value()?.to_string(),
                        ));
                    }
                    stack.push((name, attrs, Vec::new(), String::new()));
                }
                Ok(Event::Text(e)) => {
                    if let Some(top) = stack.last_mut() {
                        top.3.push_str(&e.unescape()?);
                    }
                }
                Ok(Event::End(_)) => {
                    let (name, attributes, children, text) = stack.pop().ok_or("unbalanced XML")?;
                    let trimmed = text.trim().to_string();
                    let value = if attributes.is_empty() && children.is_empty() {
                        Self::String(trimmed)
                    } else {
                        let mut obj: Vec<(String, CanonicalValue)> = Vec::new();
                        for (k, v) in attributes {
                            obj.push((format!("@{k}"), Self::String(v)));
                        }
                        if !trimmed.is_empty() {
                            obj.push(("#text".to_string(), Self::String(trimmed)));
                        }
                        for (cname, cval) in children {
                            if let Some(slot) = obj.iter_mut().find(|(k, _)| k == &cname) {
                                match &mut slot.1 {
                                    Self::Array(a) => a.push(cval),
                                    _ => {
                                        let old = slot.1.clone();
                                        slot.1 = Self::Array(vec![old, cval]);
                                    }
                                }
                            } else {
                                obj.push((cname, cval));
                            }
                        }
                        Self::Object(obj)
                    };
                    if let Some(parent) = stack.last_mut() {
                        parent.2.push((name, value));
                    } else {
                        root = Some((name, value));
                    }
                }
                Ok(Event::Eof) => break,
                Ok(_) => {}
                Err(e) => return Err(e.into()),
            }
        }

        let (root_name, root_value) = root.ok_or("empty XML")?;
        Ok(Self::Object(vec![(root_name, root_value)]))
    }

    /// Serializes the canonical hub to XML.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_json(r#"[{"a":"1"}]"#).unwrap();
    /// let xml = v.to_xml().unwrap();
    /// assert!(xml.contains("<root>"));
    /// assert!(xml.contains("<a>1</a>"));
    /// ```
    pub fn to_xml(&self) -> Result<String, Box<dyn Error>> {
        let mut writer = quick_xml::Writer::new_with_indent(Vec::new(), b' ', 2);
        match self {
            Self::Object(o) if o.len() == 1 && !o[0].0.starts_with('@') && o[0].0 != "#text" => {
                write_element(&mut writer, &o[0].0, &o[0].1)?;
            }
            Self::Array(a) => {
                write_open(&mut writer, "root", &[])?;
                for item in a {
                    write_element(&mut writer, "item", item)?;
                }
                write_close(&mut writer, "root")?;
            }
            _ => write_element(&mut writer, "root", self)?,
        }
        Ok(String::from_utf8(writer.into_inner())?)
    }
}

fn write_open<W: std::io::Write>(
    writer: &mut quick_xml::Writer<W>,
    name: &str,
    attrs: &[(String, String)],
) -> Result<(), Box<dyn Error>> {
    use quick_xml::events::{BytesStart, Event};
    let mut start = BytesStart::new(name);
    for (k, v) in attrs {
        start.push_attribute((k.as_str(), v.as_str()));
    }
    writer.write_event(Event::Start(start))?;
    Ok(())
}

fn write_close<W: std::io::Write>(
    writer: &mut quick_xml::Writer<W>,
    name: &str,
) -> Result<(), Box<dyn Error>> {
    use quick_xml::events::{BytesEnd, Event};
    writer.write_event(Event::End(BytesEnd::new(name)))?;
    Ok(())
}

fn write_element<W: std::io::Write>(
    writer: &mut quick_xml::Writer<W>,
    name: &str,
    value: &CanonicalValue,
) -> Result<(), Box<dyn Error>> {
    use quick_xml::events::{BytesText, Event};

    match value {
        CanonicalValue::Array(items) => {
            for item in items {
                write_element(writer, name, item)?;
            }
        }
        CanonicalValue::Object(o) => {
            let attrs: Vec<(String, String)> = o
                .iter()
                .filter_map(|(k, v)| {
                    k.strip_prefix('@').and_then(|attr| match v {
                        CanonicalValue::String(s) => Some((attr.to_string(), s.clone())),
                        _ => None,
                    })
                })
                .collect();
            write_open(writer, name, &attrs)?;
            for (k, v) in o {
                if k.starts_with('@') {
                    continue;
                }
                if k == "#text" {
                    if let CanonicalValue::String(s) = v {
                        writer.write_event(Event::Text(BytesText::new(s)))?;
                    }
                    continue;
                }
                write_element(writer, k, v)?;
            }
            write_close(writer, name)?;
        }
        CanonicalValue::String(s) => {
            write_open(writer, name, &[])?;
            writer.write_event(Event::Text(BytesText::new(s)))?;
            write_close(writer, name)?;
        }
        CanonicalValue::Bytes(b) => {
            use base64::Engine;
            let s = base64::engine::general_purpose::STANDARD.encode(b);
            write_open(writer, name, &[])?;
            writer.write_event(Event::Text(BytesText::new(&s)))?;
            write_close(writer, name)?;
        }
        CanonicalValue::Number(n) => {
            write_open(writer, name, &[])?;
            writer.write_event(Event::Text(BytesText::new(&n.to_string())))?;
            write_close(writer, name)?;
        }
        CanonicalValue::Bool(b) => {
            write_open(writer, name, &[])?;
            writer.write_event(Event::Text(BytesText::new(&b.to_string())))?;
            write_close(writer, name)?;
        }
        CanonicalValue::Null => {
            write_open(writer, name, &[])?;
            write_close(writer, name)?;
        }
    }
    Ok(())
}

type SqlRow = Vec<(String, CanonicalValue)>;
type SqlTable = (String, Vec<SqlRow>);

impl CanonicalValue {
    /// Parses an ODS (OpenDocument Spreadsheet) file into a value.
    ///
    /// A single-sheet ODS becomes an array of row objects; a multi-sheet
    /// ODS becomes an object keyed by sheet name.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// // Fixture covered in tests/ods.rs.
    /// ```
    pub fn from_ods(bytes: &[u8]) -> Result<Self, Box<dyn Error>> {
        use calamine::{Ods, Reader};
        let cursor = Cursor::new(bytes.to_vec());
        let mut workbook: Ods<_> = Ods::new(cursor)?;
        let names = workbook.sheet_names().to_owned();
        let mut out: Vec<(String, CanonicalValue)> = Vec::with_capacity(names.len());
        for name in names {
            let range = workbook
                .worksheet_range(&name)
                .map_err(|e| format!("failed to read sheet {name}: {e:?}"))?;
            out.push((name, Self::from_calamine_range(&range)?));
        }
        if out.len() == 1 {
            Ok(out.remove(0).1)
        } else {
            Ok(Self::Object(out))
        }
    }
}

/// Writes a workbook as an ODS file.
///
/// Each entry of `sheets` becomes a `table:table` inside the resulting
/// `content.xml`. The `mimetype` entry is written first and stored
/// uncompressed, as required by the ODF spec.
///
/// # Errors
///
/// Returns an error if any row contains nested values or if ZIP/XML I/O fails.
pub fn to_ods_workbook(sheets: &[(String, CanonicalValue)]) -> Result<Vec<u8>, Box<dyn Error>> {
    use std::io::Write as IoWrite;
    use zip::write::SimpleFileOptions;
    use zip::{CompressionMethod, ZipWriter};

    let content = ods_content_xml(sheets)?;
    let manifest = ods_manifest_xml();
    let mimetype = "application/vnd.oasis.opendocument.spreadsheet";

    let cursor = Cursor::new(Vec::new());
    let mut zip = ZipWriter::new(cursor);
    let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
    let deflated = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);

    zip.start_file("mimetype", stored)?;
    zip.write_all(mimetype.as_bytes())?;

    zip.start_file("META-INF/manifest.xml", deflated)?;
    zip.write_all(manifest.as_bytes())?;

    zip.start_file("content.xml", deflated)?;
    zip.write_all(content.as_bytes())?;

    let cursor = zip.finish()?;
    Ok(cursor.into_inner())
}

fn ods_manifest_xml() -> String {
    r#"<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
  <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>
  <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
</manifest:manifest>
"#
    .to_string()
}

fn ods_content_xml(sheets: &[(String, CanonicalValue)]) -> Result<String, Box<dyn Error>> {
    use quick_xml::escape::escape;
    let mut out = String::new();
    out.push_str(r#"<?xml version="1.0" encoding="UTF-8"?>"#);
    out.push_str(
        r#"<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.2">"#,
    );
    out.push_str("<office:body><office:spreadsheet>");

    for (name, value) in sheets {
        out.push_str(&format!("<table:table table:name=\"{}\">", escape(name)));
        let rows = value.as_tabular()?;
        let mut headers: Vec<String> = Vec::new();
        for row in &rows {
            if let CanonicalValue::Object(o) = row {
                for (k, _) in o {
                    if !headers.contains(k) {
                        headers.push(k.clone());
                    }
                }
            }
        }
        out.push_str("<table:table-row>");
        for h in &headers {
            out.push_str("<table:table-cell office:value-type=\"string\">");
            out.push_str(&format!("<text:p>{}</text:p>", escape(h)));
            out.push_str("</table:table-cell>");
        }
        out.push_str("</table:table-row>");

        for row in &rows {
            if let CanonicalValue::Object(o) = row {
                out.push_str("<table:table-row>");
                for h in &headers {
                    match o.iter().find(|(k, _)| k == h).map(|(_, v)| v) {
                        Some(CanonicalValue::String(s)) => {
                            out.push_str("<table:table-cell office:value-type=\"string\">");
                            out.push_str(&format!("<text:p>{}</text:p>", escape(s)));
                            out.push_str("</table:table-cell>");
                        }
                        Some(CanonicalValue::Number(n)) => {
                            out.push_str(&format!(
                                "<table:table-cell office:value-type=\"float\" office:value=\"{}\">",
                                n
                            ));
                            out.push_str(&format!("<text:p>{}</text:p>", n));
                            out.push_str("</table:table-cell>");
                        }
                        Some(CanonicalValue::Bool(b)) => {
                            out.push_str(&format!(
                                "<table:table-cell office:value-type=\"boolean\" office:boolean-value=\"{}\">",
                                b
                            ));
                            out.push_str(&format!(
                                "<text:p>{}</text:p>",
                                if *b { "TRUE" } else { "FALSE" }
                            ));
                            out.push_str("</table:table-cell>");
                        }
                        Some(CanonicalValue::Bytes(b)) => {
                            use base64::Engine;
                            let s = base64::engine::general_purpose::STANDARD.encode(b);
                            out.push_str("<table:table-cell office:value-type=\"string\">");
                            out.push_str(&format!("<text:p>{}</text:p>", s));
                            out.push_str("</table:table-cell>");
                        }
                        Some(CanonicalValue::Null) | None => {
                            out.push_str("<table:table-cell/>");
                        }
                        Some(CanonicalValue::Array(_)) | Some(CanonicalValue::Object(_)) => {
                            return Err(format!(
                                "nested value at key {h} cannot be represented in ODS"
                            )
                            .into());
                        }
                    }
                }
                out.push_str("</table:table-row>");
            }
        }
        out.push_str("</table:table>");
    }

    out.push_str("</office:spreadsheet></office:body></office:document-content>");
    Ok(out)
}

fn is_scalar(v: &CanonicalValue) -> bool {
    matches!(
        v,
        CanonicalValue::Null
            | CanonicalValue::Bool(_)
            | CanonicalValue::Number(_)
            | CanonicalValue::String(_)
    )
}

fn find_first_object_array(v: &CanonicalValue) -> Option<&Vec<CanonicalValue>> {
    match v {
        CanonicalValue::Array(a)
            if !a.is_empty() && a.iter().all(|x| matches!(x, CanonicalValue::Object(_))) =>
        {
            Some(a)
        }
        CanonicalValue::Object(o) => {
            for (_, v) in o {
                if let Some(arr) = find_first_object_array(v) {
                    return Some(arr);
                }
            }
            None
        }
        _ => None,
    }
}

fn cell_to_string(c: &calamine::Data) -> String {
    use calamine::Data;
    match c {
        Data::Empty => String::new(),
        Data::String(s) => s.clone(),
        Data::Float(f) => f.to_string(),
        Data::Int(i) => i.to_string(),
        Data::Bool(b) => b.to_string(),
        Data::DateTime(dt) => dt.to_string(),
        Data::DateTimeIso(s) => s.clone(),
        Data::DurationIso(s) => s.clone(),
        Data::Error(e) => format!("{e:?}"),
    }
}

fn clean_ident(s: &str) -> String {
    s.trim_matches('"').trim_matches('`').to_string()
}

fn quote_ident(s: &str) -> String {
    format!("\"{}\"", s.replace('"', "\"\""))
}

impl CanonicalValue {
    /// Parses NDJSON (newline-delimited JSON) into an array.
    ///
    /// Each non-empty line must be a complete JSON value. The result is a
    /// `CanonicalValue::Array` with one element per line, preserving order.
    ///
    /// # Errors
    /// Returns an error if any non-empty line is malformed JSON.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_ndjson("{\"a\":1}\n{\"a\":2}\n").unwrap();
    /// assert_eq!(v.to_json().unwrap(), r#"[{"a":1},{"a":2}]"#);
    /// ```
    pub fn from_ndjson(input: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let mut values = Vec::new();
        for line in input.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            let v: serde_json::Value = serde_json::from_str(trimmed)?;
            values.push(Self::from_json_value(v));
        }
        Ok(Self::Array(values))
    }

    /// Serializes the value as NDJSON (one JSON value per line).
    ///
    /// Arrays become one line per element. Non-array values become a
    /// single line.
    ///
    /// # Errors
    /// Returns an error if serialization fails.
    ///
    /// # Examples
    /// ```
    /// use sheeter_engine::CanonicalValue;
    /// let v = CanonicalValue::from_json(r#"[{"a":1},{"a":2}]"#).unwrap();
    /// assert_eq!(v.to_ndjson().unwrap(), "{\"a\":1}\n{\"a\":2}\n");
    /// ```
    pub fn to_ndjson(&self) -> Result<String, Box<dyn std::error::Error>> {
        let mut out = String::new();
        match self {
            Self::Array(items) => {
                for item in items {
                    out.push_str(&serde_json::to_string(&item.to_json_value())?);
                    out.push('\n');
                }
            }
            other => {
                out.push_str(&serde_json::to_string(&other.to_json_value())?);
                out.push('\n');
            }
        }
        Ok(out)
    }
}