1use crate::{CanonicalValue, Format, SqlDialect};
2use std::error::Error;
3
4pub fn inspect(input: &[u8], format: Format) -> Result<String, Box<dyn Error>> {
19 let sheets = load_sheets(input, format)?;
20 let mut out = String::new();
21 out.push_str(&format!("format: {}\n", format_name(format)));
22 out.push_str(&format!("documents: {}\n", sheets.len()));
23 for (name, value) in &sheets {
24 describe_sheet(&mut out, name, value);
25 }
26 Ok(out)
27}
28
29fn format_name(f: Format) -> &'static str {
30 match f {
31 Format::Csv => "csv",
32 Format::Tsv => "tsv",
33 Format::Json => "json",
34 Format::Yaml => "yaml",
35 Format::Xml => "xml",
36 Format::Toml => "toml",
37 Format::Xlsx => "xlsx",
38 Format::Sql => "sql",
39 Format::Ndjson => "ndjson",
40 Format::Ods => "ods",
41 }
42}
43
44fn load_sheets(
45 input: &[u8],
46 format: Format,
47) -> Result<Vec<(String, CanonicalValue)>, Box<dyn Error>> {
48 match format {
49 Format::Xlsx => CanonicalValue::from_xlsx_workbook(input),
50 Format::Sql => {
51 let text = std::str::from_utf8(input)?;
52 CanonicalValue::from_sql_workbook(text, SqlDialect::Generic)
53 }
54 Format::Ndjson => {
55 let text = std::str::from_utf8(input)?;
56 Ok(vec![(
57 "Sheet1".to_string(),
58 CanonicalValue::from_ndjson(text)?,
59 )])
60 }
61 _ => {
62 let text = std::str::from_utf8(input)?;
63 let hub = match format {
64 Format::Csv => CanonicalValue::from_csv(text)?,
65 Format::Tsv => CanonicalValue::from_tsv(text)?,
66 Format::Json => CanonicalValue::from_json(text)?,
67 Format::Yaml => CanonicalValue::from_yaml(text)?,
68 Format::Xml => CanonicalValue::from_xml(text)?,
69 Format::Toml => CanonicalValue::from_toml(text)?,
70 Format::Sql | Format::Ndjson | Format::Xlsx | Format::Ods => unreachable!(),
71 };
72 Ok(vec![("Sheet1".to_string(), hub)])
73 }
74 }
75}
76
77fn describe_sheet(out: &mut String, name: &str, value: &CanonicalValue) {
78 out.push_str(&format!(" {name}:\n"));
79 match value {
80 CanonicalValue::Array(rows) => {
81 let cols = collect_headers(rows);
82 out.push_str(&format!(" rows: {}\n", rows.len()));
83 out.push_str(&format!(" columns: {}\n", cols.len()));
84 for (i, col) in cols.iter().enumerate() {
85 let ty = infer_type(rows, col);
86 out.push_str(&format!(" {}: {} ({})\n", i + 1, col, ty));
87 }
88 }
89 CanonicalValue::Object(o) => {
90 out.push_str(&format!(" fields: {}\n", o.len()));
91 for (k, v) in o {
92 out.push_str(&format!(" {k}: {}\n", infer_scalar(v)));
93 }
94 }
95 CanonicalValue::String(s) => {
96 out.push_str(&format!(" scalar: string ({} bytes)\n", s.len()));
97 }
98 CanonicalValue::Bytes(b) => {
99 out.push_str(&format!(" scalar: binary ({} bytes)\n", b.len()));
100 }
101 CanonicalValue::Number(n) => out.push_str(&format!(" scalar: number ({n})\n")),
102 CanonicalValue::Bool(b) => out.push_str(&format!(" scalar: bool ({b})\n")),
103 CanonicalValue::Null => out.push_str(" scalar: null\n"),
104 }
105}
106
107fn collect_headers(rows: &[CanonicalValue]) -> Vec<String> {
108 let mut out: Vec<String> = Vec::new();
109 for row in rows {
110 if let CanonicalValue::Object(o) = row {
111 for (k, _) in o {
112 if !out.contains(k) {
113 out.push(k.clone());
114 }
115 }
116 }
117 }
118 out
119}
120
121fn infer_type(rows: &[CanonicalValue], col: &str) -> &'static str {
122 let mut has_int = false;
123 let mut has_float = false;
124 let mut has_str = false;
125 let mut has_bool = false;
126 let mut has_null = false;
127 for row in rows {
128 if let CanonicalValue::Object(o) = row {
129 match o.iter().find(|(k, _)| k == col).map(|(_, v)| v) {
130 Some(CanonicalValue::Number(n)) => {
131 if n.is_i64() || n.is_u64() {
132 has_int = true;
133 } else {
134 has_float = true;
135 }
136 }
137 Some(CanonicalValue::String(_)) => has_str = true,
138 Some(CanonicalValue::Bool(_)) => has_bool = true,
139 Some(CanonicalValue::Null) | None => has_null = true,
140 _ => has_str = true,
141 }
142 }
143 }
144 match (has_int, has_float, has_str, has_bool, has_null) {
145 (true, false, false, false, _) => "integer",
146 (_, true, false, false, _) => "number",
147 (false, false, false, true, _) => "boolean",
148 (false, false, true, false, _) => "string",
149 (false, false, false, false, true) => "null",
150 _ => "mixed",
151 }
152}
153
154fn infer_scalar(v: &CanonicalValue) -> &'static str {
155 match v {
156 CanonicalValue::Null => "null",
157 CanonicalValue::Bool(_) => "boolean",
158 CanonicalValue::Number(n) if n.is_i64() || n.is_u64() => "integer",
159 CanonicalValue::Number(_) => "number",
160 CanonicalValue::String(_) => "string",
161 CanonicalValue::Bytes(_) => "binary",
162 CanonicalValue::Array(_) => "array",
163 CanonicalValue::Object(_) => "object",
164 }
165}