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