use std::path::Path;
#[cfg(any(
feature = "datetime",
feature = "date",
feature = "time",
feature = "inet",
feature = "cidr",
feature = "macaddr",
feature = "macaddr8",
feature = "uuid"
))]
use crate::valid;
use crate::{Error, Options, Result, TableLayout, Value};
pub(crate) fn parse(
rows: Vec<Vec<String>>,
layout: &TableLayout,
path: &Path,
options: &Options,
) -> Result<Value> {
match layout {
TableLayout::Kv => kv(rows, path, options),
TableLayout::Db => db(rows, path, options),
TableLayout::Custom(custom) => (custom.parser)(rows, path, options),
}
}
fn kv(rows: Vec<Vec<String>>, path: &Path, options: &Options) -> Result<Value> {
let mut root = Value::Object(Default::default());
for (index, cells) in rows.into_iter().enumerate() {
let row = index + 1;
if cells.iter().all(|cell| cell.is_empty()) {
continue; }
let key = cells
.first()
.ok_or_else(|| table_err(path, row, "missing key cell".into()))?
.trim();
let raw = cells
.get(1)
.ok_or_else(|| table_err(path, row, "missing value cell".into()))?;
let declared = cells.get(2).map(|s| s.trim()).unwrap_or("");
let value = convert(raw, declared, path, row, options)?;
super::insert_key(&mut root, key, value, options.dot_key);
}
Ok(root)
}
fn db(rows: Vec<Vec<String>>, path: &Path, options: &Options) -> Result<Value> {
let mut keys: Option<Vec<String>> = None;
let mut types: Vec<String> = Vec::new();
let mut saw_types = false;
let mut records = Vec::new();
for (index, cells) in rows.into_iter().enumerate() {
let row = index + 1;
let blank = cells.iter().all(|cell| cell.is_empty());
let Some(keys) = &keys else {
if !blank {
keys = Some(cells.iter().map(|cell| cell.trim().to_owned()).collect());
}
continue; };
if blank && saw_types {
continue; }
if !saw_types {
for cell in &cells {
let declared = cell.trim();
if !declared.is_empty() && !known_type_id(declared) {
return Err(table_err(
path,
row,
format!(
"unknown or disabled type id '{declared}' in the type row — \
if this sheet is key,value rows, use the \"kv\" layout \
(spreadsheets default to db)"
),
));
}
}
types = cells.iter().map(|cell| cell.trim().to_owned()).collect();
saw_types = true;
continue;
}
let mut record = Value::Object(Default::default());
for (column, raw) in cells.iter().enumerate() {
if raw.is_empty() {
continue; }
let Some(key) = keys.get(column).filter(|key| !key.is_empty()) else {
continue; };
let declared = types.get(column).map(String::as_str).unwrap_or("");
let value = convert(raw, declared, path, row, options)?;
super::insert_key(&mut record, key, value, options.dot_key);
}
records.push(record);
}
match keys {
None => Ok(Value::Null),
Some(_) => Ok(Value::Array(records)),
}
}
fn canonical_type(declared: &str) -> &str {
match declared {
"int" | "integer" => "i64",
"uint" => "u64",
"float" | "double" | "number" => "f64",
"string" | "text" => "str",
"boolean" => "bool",
"datetime" => "dt",
other => other,
}
}
fn known_type_id(declared: &str) -> bool {
if let Some((_, format)) = parse_array_id(declared) {
return format.is_empty() || known_type_id(format);
}
#[cfg(feature = "csv")]
if parse_csv_id(declared).is_some() {
return true;
}
match canonical_type(declared) {
"" | "auto" | "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "i128"
| "u128" | "f32" | "f64" | "bool" | "str" => true,
#[cfg(feature = "datetime")]
"dt" => true,
#[cfg(feature = "date")]
"date" => true,
#[cfg(feature = "time")]
"time" => true,
#[cfg(feature = "ipv4")]
"ipv4" => true,
#[cfg(feature = "ipv6")]
"ipv6" => true,
#[cfg(feature = "inet")]
"inet" => true,
#[cfg(feature = "cidr")]
"cidr" => true,
#[cfg(feature = "macaddr")]
"macaddr" => true,
#[cfg(feature = "macaddr8")]
"macaddr8" => true,
#[cfg(feature = "uuid")]
"uuid" => true,
#[cfg(feature = "json")]
"json" => true,
#[cfg(feature = "jsonc")]
"jsonc" => true,
_ => false,
}
}
fn parse_array_id(declared: &str) -> Option<(char, &str)> {
let rest = declared.strip_prefix("array")?;
let mut chars = rest.chars();
let sep = chars.next().unwrap_or(',');
Some((sep, chars.as_str()))
}
#[cfg(feature = "csv")]
fn parse_csv_id(declared: &str) -> Option<(u8, TableLayout)> {
let rest = declared.strip_prefix("csv")?;
let mut chars = rest.chars();
let sep = match chars.next() {
None => b',',
Some(c) if c.is_ascii() => c as u8,
Some(_) => return None, };
let layout_id = chars.as_str();
let layout = if layout_id.is_empty() {
TableLayout::Kv } else {
TableLayout::from_id(layout_id)?
};
Some((sep, layout))
}
fn array_cell(
raw: &str,
sep: char,
format: &str,
path: &Path,
row: usize,
options: &Options,
) -> Result<Value> {
if raw.is_empty() {
return Ok(Value::Array(Vec::new()));
}
let mut out = Vec::new();
for piece in raw.split(sep) {
out.push(convert(piece, format, path, row, options)?);
}
Ok(Value::Array(out))
}
fn convert(raw: &str, declared: &str, path: &Path, row: usize, options: &Options) -> Result<Value> {
if let Some((sep, format)) = parse_array_id(declared) {
return array_cell(raw, sep, format, path, row, options);
}
#[cfg(feature = "csv")]
if let Some((sep, layout)) = parse_csv_id(declared) {
return super::csv::parse_with(raw, sep, &layout, path, options)
.map_err(|e| table_err(path, row, format!("invalid csv cell: {e}")));
}
let ty = canonical_type(declared);
if matches!(ty, "" | "auto") {
return Ok(auto(raw));
}
let fail = |message: String| table_err(path, row, message);
let bad = || fail(format!("'{raw}' is not a valid {ty}"));
let int = |min: i64, max: i64| {
int_literal(raw)
.filter(|v| (min..=max).contains(v))
.map(Value::Int)
.ok_or_else(&bad)
};
let uint = |max: u64| {
uint_literal(raw)
.filter(|v| *v <= max)
.map(Value::Uint)
.ok_or_else(&bad)
};
match ty {
"i8" => int(i8::MIN.into(), i8::MAX.into()),
"i16" => int(i16::MIN.into(), i16::MAX.into()),
"i32" => int(i32::MIN.into(), i32::MAX.into()),
"i64" => int(i64::MIN, i64::MAX),
"u8" => uint(u8::MAX.into()),
"u16" => uint(u16::MAX.into()),
"u32" => uint(u32::MAX.into()),
"u64" => uint(u64::MAX),
"i128" => int128_literal(raw).map(Value::Int128).ok_or_else(&bad),
"u128" => uint128_literal(raw).map(Value::Uint128).ok_or_else(&bad),
"f32" => float_literal(raw)
.map(|f| Value::Float(f64::from(f as f32)))
.ok_or_else(&bad),
"f64" => float_literal(raw).map(Value::Float).ok_or_else(&bad),
"bool" => parse_bool(raw)
.map(Value::Bool)
.ok_or_else(|| fail(format!("'{raw}' is not a valid bool"))),
"str" => Ok(Value::String(raw.to_owned())),
#[cfg(feature = "datetime")]
"dt" => valid::datetime(raw)
.then(|| Value::DateTime(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "date")]
"date" => valid::date(raw)
.then(|| Value::Date(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "time")]
"time" => valid::time(raw)
.then(|| Value::Time(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "ipv4")]
"ipv4" => raw
.parse::<std::net::Ipv4Addr>()
.map(Value::Ipv4)
.map_err(|_| bad()),
#[cfg(feature = "ipv6")]
"ipv6" => raw
.parse::<std::net::Ipv6Addr>()
.map(Value::Ipv6)
.map_err(|_| bad()),
#[cfg(feature = "inet")]
"inet" => match raw.parse::<std::net::IpAddr>() {
Ok(ip) => Ok(ip_value(ip)),
Err(_) => valid::inet(raw)
.then(|| Value::Inet(raw.to_owned()))
.ok_or_else(&bad),
},
#[cfg(feature = "cidr")]
"cidr" => valid::cidr(raw)
.then(|| Value::Cidr(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "macaddr")]
"macaddr" => valid::macaddr(raw)
.then(|| Value::MacAddr(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "macaddr8")]
"macaddr8" => valid::macaddr8(raw)
.then(|| Value::MacAddr8(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "uuid")]
"uuid" => valid::uuid(raw)
.then(|| Value::Uuid(raw.to_owned()))
.ok_or_else(&bad),
#[cfg(feature = "json")]
"json" => super::json::parse(raw, path).map_err(|_| bad()),
#[cfg(feature = "jsonc")]
"jsonc" => super::jsonc::parse(raw, path).map_err(|_| bad()),
_ => Err(fail(format!("unknown or disabled type '{declared}'"))),
}
}
pub(crate) fn auto(raw: &str) -> Value {
if raw.eq_ignore_ascii_case("true") {
return Value::Bool(true);
}
if raw.eq_ignore_ascii_case("false") {
return Value::Bool(false);
}
#[cfg(feature = "date")]
if valid::date(raw) {
return Value::Date(raw.to_owned());
}
#[cfg(feature = "time")]
if valid::time(raw) {
return Value::Time(raw.to_owned());
}
#[cfg(feature = "datetime")]
if valid::datetime(raw) {
return Value::DateTime(raw.to_owned());
}
#[cfg(feature = "uuid")]
if valid::uuid_hyphenated(raw) {
return Value::Uuid(raw.to_owned());
}
#[cfg(feature = "macaddr")]
if valid::macaddr_pairs(raw) {
return Value::MacAddr(raw.to_owned());
}
#[cfg(feature = "macaddr8")]
if valid::macaddr8_pairs(raw) {
return Value::MacAddr8(raw.to_owned());
}
#[cfg(feature = "ipv4")]
if let Ok(ip) = raw.parse::<std::net::Ipv4Addr>() {
return Value::Ipv4(ip);
}
#[cfg(feature = "ipv6")]
if let Ok(ip) = raw.parse::<std::net::Ipv6Addr>() {
return Value::Ipv6(ip);
}
#[cfg(feature = "cidr")]
if raw.contains('/') && valid::cidr(raw) {
return Value::Cidr(raw.to_owned());
}
#[cfg(feature = "inet")]
if raw.contains('/') && valid::inet(raw) {
return Value::Inet(raw.to_owned());
}
if let Some(number) = auto_number(raw) {
return number;
}
Value::String(raw.to_owned())
}
fn auto_number(raw: &str) -> Option<Value> {
#[cfg(feature = "numeric")]
let (cleaned, bigint) = clean_literal(raw)?;
#[cfg(not(feature = "numeric"))]
let (cleaned, bigint) = (raw.to_owned(), false);
#[cfg(feature = "numeric")]
if let Some((digits, radix)) = split_radix(&cleaned) {
return i64::from_str_radix(&digits, radix)
.ok()
.map(Value::Int)
.or_else(|| u64::from_str_radix(&digits, radix).ok().map(Value::Uint));
}
let body = cleaned.strip_prefix('-').unwrap_or(&cleaned);
let leading_zero =
body.len() > 1 && body.starts_with('0') && body.as_bytes()[1].is_ascii_digit();
let numeric_shape = !body.is_empty()
&& body.chars().any(|c| c.is_ascii_digit())
&& body
.chars()
.all(|c| c.is_ascii_digit() || matches!(c, '.' | 'e' | 'E' | '+' | '-'));
if leading_zero || !numeric_shape {
return None;
}
if let Ok(i) = cleaned.parse::<i64>() {
return Some(Value::Int(i));
}
if let Ok(u) = cleaned.parse::<u64>() {
return Some(Value::Uint(u));
}
if bigint {
return None; }
cleaned.parse::<f64>().ok().map(Value::Float)
}
#[cfg(feature = "numeric")]
fn clean_literal(raw: &str) -> Option<(String, bool)> {
let b = raw.as_bytes();
for (i, &c) in b.iter().enumerate() {
if c == b'_' {
let between_digits = i > 0
&& i + 1 < b.len()
&& b[i - 1].is_ascii_hexdigit()
&& b[i + 1].is_ascii_hexdigit();
if !between_digits {
return None;
}
}
}
let mut s: String = raw.chars().filter(|&c| c != '_').collect();
let bigint = s.ends_with('n')
&& s.len() > 1
&& s[..s.len() - 1]
.bytes()
.all(|c| c.is_ascii_hexdigit() || matches!(c, b'x' | b'X' | b'o' | b'O' | b'+' | b'-'));
if bigint {
s.truncate(s.len() - 1);
}
Some((s, bigint))
}
#[cfg(feature = "numeric")]
fn relaxed_numeric(raw: &str) -> Option<String> {
let converted: String = raw
.chars()
.map(|c| match c {
'\u{FF10}'..='\u{FF19}' => char::from(b'0' + (c as u32 - 0xFF10) as u8),
other => other,
})
.collect();
let last_dot = converted.rfind('.');
let mut seen_minus = false;
let mut out = String::new();
for (i, c) in converted.char_indices() {
if c.is_ascii_digit() {
out.push(c);
} else if c == '.' && Some(i) == last_dot {
out.push('.');
} else if c == '-' && !seen_minus {
out.push('-');
seen_minus = true;
}
}
out.chars().any(|c| c.is_ascii_digit()).then_some(out)
}
#[cfg(feature = "numeric")]
fn split_radix(s: &str) -> Option<(String, u32)> {
let (sign, body) = match s.strip_prefix('-') {
Some(body) => ("-", body),
None => ("", s.strip_prefix('+').unwrap_or(s)),
};
let prefixes = [
("0x", 16),
("0X", 16),
("0b", 2),
("0B", 2),
("0o", 8),
("0O", 8),
];
let (digits, radix) = prefixes
.iter()
.find_map(|(prefix, radix)| body.strip_prefix(prefix).map(|d| (d, *radix)))?;
if digits.is_empty() {
return None;
}
Some((format!("{sign}{digits}"), radix))
}
fn parse_bool(raw: &str) -> Option<bool> {
match raw.trim().to_ascii_lowercase().as_str() {
"true" | "t" | "yes" | "y" | "on" | "1" => Some(true),
"false" | "f" | "no" | "n" | "off" | "0" => Some(false),
_ => None,
}
}
fn int128_literal(raw: &str) -> Option<i128> {
#[cfg(feature = "numeric")]
{
let primary = clean_literal(raw).and_then(|(s, _)| match split_radix(&s) {
Some((digits, radix)) => i128::from_str_radix(&digits, radix).ok(),
None => s.parse().ok(),
});
primary.or_else(|| relaxed_numeric(raw).and_then(|s| s.parse().ok()))
}
#[cfg(not(feature = "numeric"))]
raw.parse().ok()
}
fn uint128_literal(raw: &str) -> Option<u128> {
#[cfg(feature = "numeric")]
{
let primary = clean_literal(raw).and_then(|(s, _)| match split_radix(&s) {
Some((digits, radix)) => u128::from_str_radix(&digits, radix).ok(),
None => s.parse().ok(),
});
primary.or_else(|| relaxed_numeric(raw).and_then(|s| s.parse().ok()))
}
#[cfg(not(feature = "numeric"))]
raw.parse().ok()
}
fn int_literal(raw: &str) -> Option<i64> {
#[cfg(feature = "numeric")]
{
let primary = clean_literal(raw).and_then(|(s, _)| match split_radix(&s) {
Some((digits, radix)) => i64::from_str_radix(&digits, radix).ok(),
None => s.parse().ok(),
});
primary.or_else(|| relaxed_numeric(raw).and_then(|s| s.parse().ok()))
}
#[cfg(not(feature = "numeric"))]
raw.parse().ok()
}
fn uint_literal(raw: &str) -> Option<u64> {
#[cfg(feature = "numeric")]
{
let primary = clean_literal(raw).and_then(|(s, _)| match split_radix(&s) {
Some((digits, radix)) => u64::from_str_radix(&digits, radix).ok(),
None => s.parse().ok(),
});
primary.or_else(|| relaxed_numeric(raw).and_then(|s| s.parse().ok()))
}
#[cfg(not(feature = "numeric"))]
raw.parse().ok()
}
fn float_literal(raw: &str) -> Option<f64> {
#[cfg(feature = "numeric")]
{
let primary = clean_literal(raw).and_then(|(s, bigint)| {
if let Some((digits, radix)) = split_radix(&s) {
i64::from_str_radix(&digits, radix)
.ok()
.map(|v| v as f64)
.or_else(|| u64::from_str_radix(&digits, radix).ok().map(|v| v as f64))
} else if bigint {
s.parse::<i64>()
.ok()
.map(|v| v as f64)
.or_else(|| s.parse::<u64>().ok().map(|v| v as f64))
} else {
s.parse().ok()
}
});
primary.or_else(|| relaxed_numeric(raw).and_then(|s| s.parse::<f64>().ok()))
}
#[cfg(not(feature = "numeric"))]
raw.parse().ok()
}
#[cfg(feature = "inet")]
fn ip_value(ip: std::net::IpAddr) -> Value {
match ip {
std::net::IpAddr::V4(v4) => Value::Ipv4(v4),
std::net::IpAddr::V6(v6) => Value::Ipv6(v6),
}
}
fn table_err(path: &Path, row: usize, message: String) -> Error {
Error::Table {
path: path.to_path_buf(),
row,
message,
}
}