use std::collections::HashMap;
use std::fmt;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(untagged)]
pub enum Value {
#[default]
Null,
Bool(bool),
Int(i64),
Float(f64),
Text(String),
}
impl Value {
pub fn as_f64(&self) -> Option<f64> {
match self {
Value::Int(v) => Some(*v as f64),
Value::Float(v) if v.is_finite() => Some(*v),
Value::Text(s) => parse_number(s),
_ => None,
}
}
pub fn is_null(&self) -> bool {
matches!(self, Value::Null) || matches!(self, Value::Float(v) if !v.is_finite())
}
pub fn text(&self) -> String {
match self {
Value::Null => String::new(),
Value::Bool(b) => b.to_string(),
Value::Int(v) => v.to_string(),
Value::Float(v) if !v.is_finite() => String::new(),
Value::Float(v) => fmt_float(*v),
Value::Text(s) => s.clone(),
}
}
pub fn text_opt(s: Option<&str>) -> Value {
match s.map(str::trim) {
Some(t) if !t.is_empty() => Value::Text(t.to_string()),
_ => Value::Null,
}
}
pub fn float_opt(v: Option<f64>) -> Value {
match v {
Some(x) if x.is_finite() => Value::Float(x),
_ => Value::Null,
}
}
pub fn count(v: u64) -> Value {
Value::Int(i64::try_from(v).unwrap_or(i64::MAX))
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.text())
}
}
impl From<f64> for Value {
fn from(v: f64) -> Self {
Value::float_opt(Some(v))
}
}
impl From<i64> for Value {
fn from(v: i64) -> Self {
Value::Int(v)
}
}
impl From<u64> for Value {
fn from(v: u64) -> Self {
Value::count(v)
}
}
impl From<u32> for Value {
fn from(v: u32) -> Self {
Value::Int(i64::from(v))
}
}
impl From<bool> for Value {
fn from(v: bool) -> Self {
Value::Bool(v)
}
}
impl From<String> for Value {
fn from(v: String) -> Self {
Value::Text(v)
}
}
impl From<&str> for Value {
fn from(v: &str) -> Self {
Value::Text(v.to_string())
}
}
impl<T: Into<Value>> From<Option<T>> for Value {
fn from(v: Option<T>) -> Self {
v.map_or(Value::Null, Into::into)
}
}
pub fn fmt_float(v: f64) -> String {
if v.fract() == 0.0 && v.abs() < 1e15 {
format!("{v:.0}")
} else {
format!("{v}")
}
}
pub fn parse_number(s: &str) -> Option<f64> {
let t = s.trim();
if t.is_empty() || t.len() > 64 {
return None;
}
let body = t.strip_prefix(['+', '-']).unwrap_or(t);
let mut digits = 0;
let mut seen_e = false;
let mut seen_dot = false;
let mut prev = ' ';
for c in body.chars() {
match c {
'0'..='9' => digits += 1,
'.' if !seen_e && !seen_dot => seen_dot = true,
'e' | 'E' if digits > 0 && !seen_e => seen_e = true,
'+' | '-' if matches!(prev, 'e' | 'E') => {}
_ => return None,
}
prev = c;
}
if digits == 0 {
return None;
}
t.parse::<f64>().ok().filter(|v| v.is_finite())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ColumnType {
String,
Integer,
Float,
Boolean,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Role {
Id,
Key,
Annotation,
Metadata,
Value,
Error,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Column {
pub name: String,
#[serde(rename = "type")]
pub kind: ColumnType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unit: Option<String>,
pub role: Role,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnDoc {
pub name: &'static str,
pub role: Role,
pub unit: Option<&'static str>,
pub description: &'static str,
}
impl ColumnDoc {
pub const fn value(name: &'static str, description: &'static str) -> Self {
ColumnDoc {
name,
role: Role::Value,
unit: None,
description,
}
}
pub const fn unit(name: &'static str, unit: &'static str, description: &'static str) -> Self {
ColumnDoc {
name,
role: Role::Value,
unit: Some(unit),
description,
}
}
pub const fn key(name: &'static str, description: &'static str) -> Self {
ColumnDoc {
name,
role: Role::Key,
unit: None,
description,
}
}
pub const fn meta(name: &'static str, description: &'static str) -> Self {
ColumnDoc {
name,
role: Role::Metadata,
unit: None,
description,
}
}
fn matches(&self, name: &str) -> bool {
match self.name.strip_suffix('*') {
Some(prefix) => name.starts_with(prefix),
None => self.name == name,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Row {
pub cells: Vec<(String, Value)>,
}
impl Row {
pub fn new() -> Self {
Row::default()
}
pub fn set(&mut self, name: impl Into<String>, value: impl Into<Value>) -> &mut Self {
let name = name.into();
let value = value.into();
if let Some(c) = self.cells.iter_mut().find(|(n, _)| *n == name) {
c.1 = value;
} else {
self.cells.push((name, value));
}
self
}
#[must_use]
pub fn with(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
self.set(name, value);
self
}
pub fn get(&self, name: &str) -> Option<&Value> {
self.cells.iter().find(|(n, _)| n == name).map(|(_, v)| v)
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Table {
pub columns: Vec<Column>,
pub rows: Vec<Vec<Value>>,
}
impl Table {
pub fn index_of(&self, name: &str) -> Option<usize> {
self.columns.iter().position(|c| c.name == name)
}
pub fn get(&self, row: usize, name: &str) -> &Value {
static NULL: Value = Value::Null;
self.index_of(name)
.and_then(|i| self.rows.get(row).and_then(|r| r.get(i)))
.unwrap_or(&NULL)
}
pub fn select(&mut self, names: &[String]) -> Result<(), String> {
let mut idx = Vec::new();
for n in names {
match self.index_of(n) {
Some(i) => idx.push(i),
None => {
return Err(format!(
"no column `{n}`; the table has: {}",
self.columns
.iter()
.map(|c| c.name.as_str())
.collect::<Vec<_>>()
.join(", ")
));
}
}
}
self.columns = idx.iter().map(|&i| self.columns[i].clone()).collect();
for r in &mut self.rows {
*r = idx.iter().map(|&i| r[i].clone()).collect();
}
Ok(())
}
pub fn infer_types(&mut self) {
for (i, c) in self.columns.iter_mut().enumerate() {
c.kind = infer(self.rows.iter().map(|r| &r[i]));
}
}
}
pub fn infer<'a>(cells: impl Iterator<Item = &'a Value>) -> ColumnType {
let (mut int, mut float, mut boolean, mut text) = (false, false, false, false);
for v in cells {
match v {
Value::Null => {}
Value::Int(_) => int = true,
Value::Float(_) => float = true,
Value::Bool(_) => boolean = true,
Value::Text(_) => text = true,
}
}
if text || (boolean && (int || float)) {
ColumnType::String
} else if float {
ColumnType::Float
} else if int {
ColumnType::Integer
} else if boolean {
ColumnType::Boolean
} else {
ColumnType::String
}
}
pub fn assemble(rows: Vec<Row>, docs: &[ColumnDoc], role_of: &dyn Fn(&str) -> Role) -> Table {
let mut names: Vec<String> = Vec::new();
let mut at: HashMap<String, usize> = HashMap::new();
for r in &rows {
for (n, _) in &r.cells {
if !at.contains_key(n) {
at.insert(n.clone(), names.len());
names.push(n.clone());
}
}
}
let role = |n: &str| {
docs.iter()
.find(|d| d.matches(n))
.map_or_else(|| role_of(n), |d| d.role)
};
let rank = |r: Role| match r {
Role::Id => 0,
Role::Key => 1,
Role::Metadata => 2,
Role::Annotation => 3,
Role::Value => 4,
Role::Error => 5,
};
let mut order: Vec<usize> = (0..names.len()).collect();
order.sort_by_key(|&i| (rank(role(&names[i])), i));
let pos: HashMap<&str, usize> = order
.iter()
.enumerate()
.map(|(p, &i)| (names[i].as_str(), p))
.collect();
let mut out_rows = Vec::with_capacity(rows.len());
for r in rows {
let mut cells = vec![Value::Null; names.len()];
for (n, v) in r.cells {
if let Some(&p) = pos.get(n.as_str()) {
cells[p] = v;
}
}
out_rows.push(cells);
}
let columns = order
.iter()
.enumerate()
.map(|(p, &i)| {
let n = &names[i];
let doc = docs.iter().find(|d| d.matches(n));
Column {
name: n.clone(),
kind: infer(out_rows.iter().map(|r| &r[p])),
unit: doc.and_then(|d| d.unit.map(str::to_string)),
role: role(n),
description: doc.map(|d| d.description.to_string()),
}
})
.collect();
Table {
columns,
rows: out_rows,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numbers_parse_strictly() {
assert_eq!(parse_number(" 1.5 "), Some(1.5));
assert_eq!(parse_number("-2e3"), Some(-2000.0));
assert_eq!(parse_number("1,000"), None);
assert_eq!(parse_number("NaN"), None);
assert_eq!(parse_number("."), None);
assert_eq!(parse_number("1.2.3"), None);
assert_eq!(parse_number("A1"), None);
}
#[test]
fn assemble_orders_by_role_then_first_seen_and_types_columns() {
let docs = [
ColumnDoc {
name: "path",
role: Role::Id,
unit: None,
description: "file",
},
ColumnDoc::key("channel", "channel"),
ColumnDoc::unit("rate_hz", "Hz", "rate"),
ColumnDoc {
name: "error",
role: Role::Error,
unit: None,
description: "why",
},
];
let rows = vec![
Row::new()
.with("path", "a")
.with("mean", 1.5)
.with("channel", 0u32),
Row::new()
.with("path", "b")
.with("error", "bad")
.with("rate_hz", 10u32)
.with("mean", Value::Int(2)),
];
let t = assemble(rows, &docs, &|_| Role::Value);
let names: Vec<&str> = t.columns.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, ["path", "channel", "mean", "rate_hz", "error"]);
assert_eq!(t.columns[2].kind, ColumnType::Float);
assert_eq!(t.columns[3].unit.as_deref(), Some("Hz"));
assert_eq!(t.rows[1][1], Value::Null);
assert_eq!(t.get(1, "error"), &Value::Text("bad".into()));
}
}