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 }
40}
41
42fn load_sheets(
43 input: &[u8],
44 format: Format,
45) -> Result<Vec<(String, CanonicalValue)>, Box<dyn Error>> {
46 match format {
47 Format::Xlsx => CanonicalValue::from_xlsx_workbook(input),
48 Format::Sql => {
49 let text = std::str::from_utf8(input)?;
50 CanonicalValue::from_sql_workbook(text, SqlDialect::Generic)
51 }
52 _ => {
53 let text = std::str::from_utf8(input)?;
54 let hub = match format {
55 Format::Csv => CanonicalValue::from_csv(text)?,
56 Format::Tsv => CanonicalValue::from_tsv(text)?,
57 Format::Json => CanonicalValue::from_json(text)?,
58 Format::Yaml => CanonicalValue::from_yaml(text)?,
59 Format::Xml => CanonicalValue::from_xml(text)?,
60 Format::Toml => CanonicalValue::from_toml(text)?,
61 Format::Xlsx | Format::Sql => unreachable!(),
62 };
63 Ok(vec![("Sheet1".to_string(), hub)])
64 }
65 }
66}
67
68fn describe_sheet(out: &mut String, name: &str, value: &CanonicalValue) {
69 out.push_str(&format!(" {name}:\n"));
70 match value {
71 CanonicalValue::Array(rows) => {
72 let cols = collect_headers(rows);
73 out.push_str(&format!(" rows: {}\n", rows.len()));
74 out.push_str(&format!(" columns: {}\n", cols.len()));
75 for (i, col) in cols.iter().enumerate() {
76 let ty = infer_type(rows, col);
77 out.push_str(&format!(" {}: {} ({})\n", i + 1, col, ty));
78 }
79 }
80 CanonicalValue::Object(o) => {
81 out.push_str(&format!(" fields: {}\n", o.len()));
82 for (k, v) in o {
83 out.push_str(&format!(" {k}: {}\n", infer_scalar(v)));
84 }
85 }
86 CanonicalValue::String(s) => {
87 out.push_str(&format!(" scalar: string ({} bytes)\n", s.len()));
88 }
89 CanonicalValue::Number(n) => out.push_str(&format!(" scalar: number ({n})\n")),
90 CanonicalValue::Bool(b) => out.push_str(&format!(" scalar: bool ({b})\n")),
91 CanonicalValue::Null => out.push_str(" scalar: null\n"),
92 }
93}
94
95fn collect_headers(rows: &[CanonicalValue]) -> Vec<String> {
96 let mut out: Vec<String> = Vec::new();
97 for row in rows {
98 if let CanonicalValue::Object(o) = row {
99 for (k, _) in o {
100 if !out.contains(k) {
101 out.push(k.clone());
102 }
103 }
104 }
105 }
106 out
107}
108
109fn infer_type(rows: &[CanonicalValue], col: &str) -> &'static str {
110 let mut has_int = false;
111 let mut has_float = false;
112 let mut has_str = false;
113 let mut has_bool = false;
114 let mut has_null = false;
115 for row in rows {
116 if let CanonicalValue::Object(o) = row {
117 match o.iter().find(|(k, _)| k == col).map(|(_, v)| v) {
118 Some(CanonicalValue::Number(n)) => {
119 if n.is_i64() || n.is_u64() {
120 has_int = true;
121 } else {
122 has_float = true;
123 }
124 }
125 Some(CanonicalValue::String(_)) => has_str = true,
126 Some(CanonicalValue::Bool(_)) => has_bool = true,
127 Some(CanonicalValue::Null) | None => has_null = true,
128 _ => has_str = true,
129 }
130 }
131 }
132 match (has_int, has_float, has_str, has_bool, has_null) {
133 (true, false, false, false, _) => "integer",
134 (_, true, false, false, _) => "number",
135 (false, false, false, true, _) => "boolean",
136 (false, false, true, false, _) => "string",
137 (false, false, false, false, true) => "null",
138 _ => "mixed",
139 }
140}
141
142fn infer_scalar(v: &CanonicalValue) -> &'static str {
143 match v {
144 CanonicalValue::Null => "null",
145 CanonicalValue::Bool(_) => "boolean",
146 CanonicalValue::Number(n) if n.is_i64() || n.is_u64() => "integer",
147 CanonicalValue::Number(_) => "number",
148 CanonicalValue::String(_) => "string",
149 CanonicalValue::Array(_) => "array",
150 CanonicalValue::Object(_) => "object",
151 }
152}