Skip to main content

byob_types/
chart.rs

1//! Charts byob draws itself: `ChartSpec` v1, the same JSON hob saves as `<title>.chart.json`,
2//! so chart files move between the two apps.
3//!
4//! [`validate`] ports hob's `validateChart` (`app/src/shared/chart.ts`): it coerces
5//! numbers written as text ("1,234.5", "€12", "40%"), puts timelines in date order, caps
6//! rows and series, and fails with one plain sentence the model can act on.
7
8use serde::{Deserialize, Serialize};
9use serde_json::{Map, Value};
10
11/// Most rows one chart may hold.
12pub const MAX_ROWS: usize = 2000;
13/// Most series (number columns) one chart may hold.
14pub const MAX_SERIES: usize = 8;
15/// Longest title kept, in characters.
16pub const MAX_TITLE_CHARS: usize = 120;
17/// Longest unit kept, in characters.
18pub const MAX_UNIT_CHARS: usize = 12;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
21#[serde(rename_all = "lowercase")]
22pub enum ChartType {
23    Bar,
24    Line,
25    Area,
26    Pie,
27    Scatter,
28}
29
30impl ChartType {
31    pub const ALL: [ChartType; 5] = [ChartType::Bar, ChartType::Line, ChartType::Area, ChartType::Pie, ChartType::Scatter];
32
33    pub fn name(self) -> &'static str {
34        match self {
35            ChartType::Bar => "bar",
36            ChartType::Line => "line",
37            ChartType::Area => "area",
38            ChartType::Pie => "pie",
39            ChartType::Scatter => "scatter",
40        }
41    }
42
43    pub fn from_name(s: &str) -> Option<Self> {
44        Self::ALL.into_iter().find(|t| t.name() == s)
45    }
46}
47
48/// One number column and how the legend names it.
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub struct ChartSeries {
51    pub key: String,
52    pub label: String,
53}
54
55/// A validated chart. Build it with [`validate`]; a spec read from disk or the wire should
56/// be passed through [`validate`] again (see [`ChartSpec::from_json`]).
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct ChartSpec {
59    /// Always 1.
60    pub version: u32,
61    #[serde(rename = "type")]
62    pub kind: ChartType,
63    pub title: String,
64    /// The column along the bottom (pie: the slice names).
65    pub x: String,
66    pub series: Vec<ChartSeries>,
67    /// Rows: `x` holds a string or number (scatter: a number or null); each series key a
68    /// number or null.
69    pub data: Vec<Map<String, Value>>,
70    /// Shown with numbers, e.g. "€" or "kg".
71    #[serde(default, skip_serializing_if = "Option::is_none")]
72    pub unit: Option<String>,
73    /// Bars or areas on top of each other.
74    #[serde(default, skip_serializing_if = "is_false")]
75    pub stacked: bool,
76}
77
78fn is_false(b: &bool) -> bool {
79    !*b
80}
81
82impl ChartSpec {
83    /// Parse and re-validate an untrusted `.chart.json` (from disk, hob, or a peer).
84    pub fn from_json(text: &str) -> Result<Self, String> {
85        let v: Value = serde_json::from_str(text).map_err(|e| format!("not a chart file: {e}"))?;
86        validate(&v)
87    }
88
89    pub fn to_json(&self) -> String {
90        serde_json::to_string(self).unwrap_or_default()
91    }
92
93    /// A number cell (`None` for null, missing or non-numbers).
94    pub fn number(row: &Map<String, Value>, key: &str) -> Option<f64> {
95        row.get(key).and_then(Value::as_f64).filter(|n| n.is_finite())
96    }
97
98    /// The `x` cell of a row as text, for labels.
99    pub fn label(&self, row: &Map<String, Value>) -> String {
100        match row.get(&self.x) {
101            Some(Value::String(s)) => s.clone(),
102            Some(Value::Null) | None => String::new(),
103            Some(v) => js_string(v),
104        }
105    }
106}
107
108/// `String(v)` as JavaScript would write it, for the values the model sends.
109fn js_string(v: &Value) -> String {
110    match v {
111        Value::Null => "null".into(),
112        Value::Bool(b) => b.to_string(),
113        Value::Number(n) => match n.as_f64() {
114            Some(f) if f.fract() == 0.0 && f.abs() < 1e15 => format!("{f:.0}"),
115            _ => n.to_string(),
116        },
117        Value::String(s) => s.clone(),
118        Value::Array(a) => a.iter().map(|x| if x.is_null() { String::new() } else { js_string(x) }).collect::<Vec<_>>().join(","),
119        Value::Object(_) => "[object Object]".into(),
120    }
121}
122
123/// A chart cell that should hold a number but doesn't.
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
125pub struct NotANumber;
126
127/// A cell as a number: `Ok(None)` for empty, `Err` for something that isn't a number.
128/// "1,234.5" → 1234.5; currency, percent signs and spaces are dropped.
129pub fn to_number(v: Option<&Value>) -> Result<Option<f64>, NotANumber> {
130    match v {
131        None | Some(Value::Null) => Ok(None),
132        Some(Value::String(s)) if s.is_empty() => Ok(None),
133        Some(Value::Number(n)) => n.as_f64().filter(|f| f.is_finite()).map(Some).ok_or(NotANumber),
134        Some(Value::String(s)) => parse_number(s).map(Some).ok_or(NotANumber),
135        Some(_) => Err(NotANumber),
136    }
137}
138
139fn is_word(c: char) -> bool {
140    c.is_ascii_alphanumeric() || c == '_'
141}
142
143/// JavaScript `Number(...)` after hob's clean-up: thousands separators (a comma followed by
144/// exactly three digits) and `%€$£¥` and whitespace removed.
145fn parse_number(s: &str) -> Option<f64> {
146    let chars: Vec<char> = s.trim().chars().collect();
147    let mut cleaned = String::with_capacity(chars.len());
148    for (i, &c) in chars.iter().enumerate() {
149        if c == ',' {
150            let three = (1..=3).all(|k| chars.get(i + k).is_some_and(char::is_ascii_digit));
151            let boundary = !chars.get(i + 4).is_some_and(|&n| is_word(n));
152            if three && boundary {
153                continue;
154            }
155        }
156        if matches!(c, '%' | '€' | '$' | '£' | '¥') || c.is_whitespace() {
157            continue;
158        }
159        cleaned.push(c);
160    }
161    if cleaned.is_empty() {
162        // Number("") is 0 in JavaScript.
163        return Some(0.0);
164    }
165    // Rust accepts "inf"/"nan" spellings JavaScript doesn't; neither is finite anyway.
166    let lower = cleaned.to_ascii_lowercase();
167    if lower.contains("inf") || lower.contains("nan") {
168        return None;
169    }
170    cleaned.parse::<f64>().ok().filter(|f| f.is_finite())
171}
172
173const MONTHS: [&str; 12] = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
174
175fn month_of(word: &str) -> Option<u32> {
176    let w = word.trim_end_matches('.').to_ascii_lowercase();
177    if w.len() < 3 {
178        return None;
179    }
180    let pos = MONTHS.iter().position(|m| w.starts_with(m))?;
181    // Accept "mar", "march", "sept"; refuse e.g. "marble".
182    const FULL: [&str; 12] = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
183    let full = FULL.get(pos)?;
184    (full.starts_with(&w) || w == "sept").then(|| u32::try_from(pos).unwrap_or(0) + 1)
185}
186
187/// Days since 1970-01-01 (proleptic Gregorian).
188fn days_from_civil(y: i64, m: u32, d: u32) -> Option<i64> {
189    if !(1..=12).contains(&m) || !(1..=31).contains(&d) {
190        return None;
191    }
192    let dim = [31, if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 { 29 } else { 28 }, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
193    if d > *dim.get(usize::try_from(m - 1).ok()?)? {
194        return None;
195    }
196    let y = if m <= 2 { y - 1 } else { y };
197    let era = y.div_euclid(400);
198    let yoe = y - era * 400;
199    let mp = i64::from((m + 9) % 12);
200    let doy = (153 * mp + 2) / 5 + i64::from(d) - 1;
201    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
202    Some(era * 146_097 + doe - 719_468)
203}
204
205/// Seconds into the day for "HH:MM[:SS]" (after a `T` or a space), if present.
206fn time_of(rest: &str) -> Option<i64> {
207    let rest = rest.trim().trim_end_matches('Z');
208    if rest.is_empty() {
209        return Some(0);
210    }
211    let main = rest.split(['+', 'Z', ' ']).next().unwrap_or_default();
212    let mut parts = main.split(':');
213    let h: i64 = parts.next()?.trim().parse().ok()?;
214    let m: i64 = parts.next().map_or(Some(0), |p| p.trim().parse().ok())?;
215    let s: f64 = parts.next().map_or(Some(0.0), |p| p.trim().parse().ok())?;
216    if !(0..24).contains(&h) || !(0..60).contains(&m) || !(0.0..61.0).contains(&s) {
217        return None;
218    }
219    #[allow(clippy::cast_possible_truncation)] // checked range above
220    Some(h * 3600 + m * 60 + s as i64)
221}
222
223/// A sortable time (seconds since 1970) for strings that look like dates — "2026-03-05",
224/// "3/5/2026", "Mar 5, 2026", "5 March 2026", "March 2026", "2026-03". Bare names ("Mar",
225/// "North") and plain numbers are not dates, as in hob.
226pub fn date_of(v: &Value) -> Option<i64> {
227    let s = v.as_str()?.trim();
228    if !s.chars().any(|c| c.is_ascii_digit()) || !s.chars().any(|c| matches!(c, '-' | '/' | '.') || c.is_whitespace()) {
229        return None;
230    }
231    let (date, time) = match s.find(['T', ' ']) {
232        Some(i) if s.as_bytes().first().is_some_and(u8::is_ascii_digit) && s.get(..i).is_some_and(|d| d.contains(['-', '/'])) => {
233            (s.get(..i).unwrap_or(s), s.get(i + 1..).unwrap_or(""))
234        }
235        _ => (s, ""),
236    };
237    let secs = time_of(time)?;
238    let day = numeric_date(date).or_else(|| worded_date(s))?;
239    day.checked_mul(86_400)?.checked_add(secs)
240}
241
242/// "2026-03-05", "2026/3/5", "2026-03", "3/5/2026" (US month first, like JavaScript).
243fn numeric_date(s: &str) -> Option<i64> {
244    let sep = if s.contains('-') {
245        '-'
246    } else if s.contains('/') {
247        '/'
248    } else {
249        return None;
250    };
251    let parts: Vec<&str> = s.split(sep).collect();
252    if parts.iter().any(|p| p.is_empty() || !p.chars().all(|c| c.is_ascii_digit()) || p.len() > 4) {
253        return None;
254    }
255    let n = |i: usize| parts.get(i).and_then(|p| p.parse::<i64>().ok());
256    match parts.as_slice() {
257        [y, _, _] if y.len() == 4 => days_from_civil(n(0)?, u32::try_from(n(1)?).ok()?, u32::try_from(n(2)?).ok()?),
258        [y, _] if y.len() == 4 => days_from_civil(n(0)?, u32::try_from(n(1)?).ok()?, 1),
259        [_, _, y] if sep == '/' && (y.len() == 4 || y.len() == 2) => {
260            let year = if y.len() == 2 { 2000 + n(2)? } else { n(2)? };
261            days_from_civil(year, u32::try_from(n(0)?).ok()?, u32::try_from(n(1)?).ok()?)
262        }
263        _ => None,
264    }
265}
266
267/// "Mar 5, 2026", "5 March 2026", "March 2026", "Tue, 5 Mar 2026".
268fn worded_date(s: &str) -> Option<i64> {
269    let words: Vec<&str> = s.split(|c: char| c.is_whitespace() || c == ',' || c == '-' || c == '/').filter(|w| !w.is_empty()).collect();
270    let mut month = None;
271    let mut year = None;
272    let mut day = None;
273    for w in &words {
274        if let Some(m) = month_of(w) {
275            if month.is_some() {
276                return None;
277            }
278            month = Some(m);
279        } else if let Ok(n) = w.trim_end_matches(['.', 's', 't', 'n', 'd', 'r', 'h']).parse::<i64>() {
280            if w.len() == 4 && year.is_none() {
281                year = Some(n);
282            } else if day.is_none() && (1..=31).contains(&n) {
283                day = Some(n);
284            } else {
285                return None;
286            }
287        } else if w.len() >= 3 && ["mon", "tue", "wed", "thu", "fri", "sat", "sun"].iter().any(|d| w.to_ascii_lowercase().starts_with(d)) {
288            // weekday names are ignored
289        } else {
290            return None;
291        }
292    }
293    days_from_civil(year?, month?, u32::try_from(day.unwrap_or(1)).ok()?)
294}
295
296fn columns(row: Option<&Map<String, Value>>) -> String {
297    row.map(|r| r.keys().cloned().collect::<Vec<_>>().join(", ")).unwrap_or_default()
298}
299
300fn fail(why: impl std::fmt::Display) -> String {
301    format!("Can't draw this chart: {why}")
302}
303
304fn series_of(v: Option<&Value>) -> Vec<ChartSeries> {
305    let raw: Vec<Value> = match v {
306        Some(Value::Array(a)) => a.clone(),
307        Some(Value::String(s)) => vec![Value::String(s.clone())],
308        _ => Vec::new(),
309    };
310    raw.iter()
311        .map(|s| match s {
312            Value::String(k) => ChartSeries { key: k.clone(), label: k.clone() },
313            Value::Object(o) => {
314                let key = o.get("key").filter(|k| !k.is_null()).map(js_string).unwrap_or_default();
315                let label = o.get("label").filter(|l| !l.is_null()).map(js_string).unwrap_or_else(|| key.clone());
316                ChartSeries { key, label }
317            }
318            _ => ChartSeries { key: String::new(), label: String::new() },
319        })
320        .collect()
321}
322
323/// Check a chart the model (or a file) asked for; the error says plainly what to fix.
324pub fn validate(input: &Value) -> Result<ChartSpec, String> {
325    let empty = Map::new();
326    let o = input.as_object().unwrap_or(&empty);
327    let type_name = o.get("type").filter(|t| !t.is_null()).map(js_string).unwrap_or_default();
328    let kind = ChartType::from_name(&type_name).ok_or_else(|| fail(format!("type must be one of {}.", ChartType::ALL.map(ChartType::name).join(", "))))?;
329    let data: Vec<&Map<String, Value>> = match o.get("data") {
330        Some(Value::Array(a)) => a.iter().filter_map(Value::as_object).collect(),
331        _ => Vec::new(),
332    };
333    if data.is_empty() {
334        return Err(fail(r#"there is no data. Give data as a list of rows, like [{"month": "Jan", "sales": 10}]."#));
335    }
336    if data.len() > MAX_ROWS {
337        return Err(fail(format!("{} rows is too many; at most {MAX_ROWS}. Group or sum the data first.", data.len())));
338    }
339    let first = data.first().copied();
340    let x = o.get("x").filter(|x| !x.is_null()).map(js_string).unwrap_or_default();
341    if x.is_empty() || !data.iter().any(|r| r.contains_key(&x)) {
342        return Err(fail(format!("the rows have no column \"{x}\" for x. Columns: {}.", columns(first))));
343    }
344    let series = series_of(o.get("series"));
345    if series.is_empty() {
346        return Err(fail("give at least one series: the column(s) with the numbers."));
347    }
348    if series.len() > MAX_SERIES {
349        return Err(fail(format!("at most {MAX_SERIES} series.")));
350    }
351    if kind == ChartType::Pie && series.len() != 1 {
352        return Err(fail("a pie chart shows one series."));
353    }
354    for s in &series {
355        if !data.iter().any(|r| r.contains_key(&s.key)) {
356            return Err(fail(format!("the rows have no column \"{}\". Columns: {}.", s.key, columns(first))));
357        }
358    }
359    let mut out: Vec<Map<String, Value>> = Vec::with_capacity(data.len());
360    for r in &data {
361        let mut row = Map::new();
362        let xv = r.get(&x);
363        if kind == ChartType::Scatter {
364            let n = to_number(xv)
365                .map_err(|NotANumber| fail(format!("a scatter chart needs numbers in \"{x}\"; found \"{}\".", xv.map(js_string).unwrap_or_default())))?;
366            row.insert(x.clone(), num(n));
367        } else {
368            let v = match xv {
369                None | Some(Value::Null) => Value::String(String::new()),
370                Some(n @ Value::Number(_)) => n.clone(),
371                Some(other) => Value::String(js_string(other)),
372            };
373            row.insert(x.clone(), v);
374        }
375        for s in &series {
376            let cell = r.get(&s.key);
377            let n = to_number(cell).map_err(|NotANumber| {
378                fail(format!("\"{}\" must hold numbers; found \"{}\".", s.key, cell.map(js_string).unwrap_or_else(|| "undefined".into())))
379            })?;
380            row.insert(s.key.clone(), num(n));
381        }
382        out.push(row);
383    }
384    // A timeline in the wrong order draws a zig-zag; put dates in order (pie slices aren't one).
385    if kind != ChartType::Pie && kind != ChartType::Scatter {
386        let times: Option<Vec<i64>> = out.iter().map(|r| r.get(&x).and_then(date_of)).collect();
387        if let Some(times) = times {
388            let mut order: Vec<(i64, Map<String, Value>)> = times.into_iter().zip(out).collect();
389            order.sort_by_key(|(t, _)| *t);
390            out = order.into_iter().map(|(_, r)| r).collect();
391        }
392    }
393    let title = o.get("title").filter(|t| !t.is_null()).map(js_string).unwrap_or_default();
394    let mut title: String = title.trim().chars().take(MAX_TITLE_CHARS).collect();
395    if title.is_empty() {
396        title = series.iter().map(|s| s.label.as_str()).collect::<Vec<_>>().join(" and ");
397    }
398    let unit = o.get("unit").and_then(Value::as_str).map(str::trim).filter(|u| !u.is_empty()).map(|u| u.chars().take(MAX_UNIT_CHARS).collect());
399    let stacked = o.get("stacked") == Some(&Value::Bool(true));
400    Ok(ChartSpec { version: 1, kind, title, x, series, data: out, unit, stacked })
401}
402
403fn num(n: Option<f64>) -> Value {
404    n.and_then(serde_json::Number::from_f64).map_or(Value::Null, Value::Number)
405}
406
407/// Plain CSV as hob reads it: a header row, commas, `"quoted, values"` with `""` escapes.
408/// Blank lines are skipped; missing cells are empty.
409pub fn parse_csv(text: &str) -> Vec<Map<String, Value>> {
410    let rows = csv_rows(text);
411    let Some((head, body)) = rows.split_first() else { return Vec::new() };
412    body.iter().map(|r| head.iter().enumerate().map(|(i, h)| (h.trim().to_owned(), Value::String(r.get(i).cloned().unwrap_or_default()))).collect()).collect()
413}
414
415/// CSV records (hob's tokenizer), header included.
416pub fn csv_rows(text: &str) -> Vec<Vec<String>> {
417    let mut rows = Vec::new();
418    let mut row: Vec<String> = Vec::new();
419    let mut cell = String::new();
420    let mut quoted = false;
421    let mut it = text.chars().peekable();
422    while let Some(c) = it.next() {
423        if quoted {
424            if c == '"' && it.peek() == Some(&'"') {
425                cell.push('"');
426                it.next();
427            } else if c == '"' {
428                quoted = false;
429            } else {
430                cell.push(c);
431            }
432        } else if c == '"' {
433            quoted = true;
434        } else if c == ',' {
435            row.push(std::mem::take(&mut cell));
436        } else if c == '\n' || c == '\r' {
437            if c == '\r' && it.peek() == Some(&'\n') {
438                it.next();
439            }
440            row.push(std::mem::take(&mut cell));
441            if row.iter().any(|v| !v.is_empty()) {
442                rows.push(std::mem::take(&mut row));
443            } else {
444                row.clear();
445            }
446        } else {
447            cell.push(c);
448        }
449    }
450    row.push(cell);
451    if row.iter().any(|v| !v.is_empty()) {
452        rows.push(row);
453    }
454    // A UTF-8 BOM would otherwise end up in the first column name.
455    if let Some(first) = rows.first_mut().and_then(|r| r.first_mut())
456        && let Some(stripped) = first.strip_prefix('\u{feff}')
457    {
458        *first = stripped.to_owned();
459    }
460    rows
461}
462
463#[cfg(test)]
464mod tests {
465    use super::*;
466    use serde_json::json;
467
468    fn rows() -> Value {
469        json!([{ "month": "Jan", "sales": 10, "costs": "4" }, { "month": "Feb", "sales": 30, "costs": 8 }])
470    }
471
472    fn err(v: Value) -> String {
473        validate(&v).unwrap_err()
474    }
475
476    #[test]
477    fn a_good_chart_coerces_numbers_and_takes_plain_keys() {
478        let spec =
479            validate(&json!({"type": "bar", "title": "Sales", "x": "month", "series": ["sales", {"key": "costs", "label": "Costs"}], "data": rows()})).unwrap();
480        assert_eq!(spec.kind, ChartType::Bar);
481        assert_eq!(spec.title, "Sales");
482        assert_eq!(spec.series, vec![ChartSeries { key: "sales".into(), label: "sales".into() }, ChartSeries { key: "costs".into(), label: "Costs".into() }]);
483        assert_eq!(
484            serde_json::to_value(&spec.data).unwrap(),
485            json!([{ "month": "Jan", "sales": 10.0, "costs": 4.0 }, { "month": "Feb", "sales": 30.0, "costs": 8.0 }])
486        );
487        // Same JSON as hob: version 1, "type", no unit/stacked when unset.
488        let v: Value = serde_json::from_str(&spec.to_json()).unwrap();
489        assert_eq!(v["version"], 1);
490        assert_eq!(v["type"], "bar");
491        assert!(v.get("unit").is_none() && v.get("stacked").is_none());
492        assert_eq!(ChartSpec::from_json(&spec.to_json()).unwrap(), spec);
493    }
494
495    #[test]
496    fn says_plainly_what_is_wrong() {
497        assert!(err(json!({"type": "radar", "x": "month", "series": ["sales"], "data": rows()})).contains("type must be one of bar, line, area, pie, scatter"));
498        assert!(err(json!({"type": "bar", "x": "month", "series": ["sales"], "data": []})).contains("no data"));
499        assert!(err(json!({"type": "bar", "x": "week", "series": ["sales"], "data": rows()})).contains("\"week\""));
500        assert!(err(json!({"type": "bar", "x": "month", "series": ["profit"], "data": rows()})).contains("\"profit\""));
501        assert!(err(json!({"type": "bar", "x": "month", "series": ["month"], "data": rows()})).contains("numbers"));
502        assert!(err(json!({"type": "pie", "x": "month", "series": ["sales", "costs"], "data": rows()})).contains("one series"));
503        let nine: Vec<String> = (0..9).map(|i| format!("s{i}")).collect();
504        assert!(err(json!({"type": "bar", "x": "month", "series": nine, "data": rows()})).contains("at most 8"));
505        let many: Vec<Value> = (0..2001).map(|i| json!({"i": i, "v": i})).collect();
506        assert!(err(json!({"type": "bar", "x": "i", "series": ["v"], "data": many})).contains("2000"));
507        assert!(err(json!({"type": "bar", "x": "month", "series": [], "data": rows()})).contains("at least one series"));
508        assert!(err(json!({"type": "scatter", "x": "month", "series": ["sales"], "data": rows()})).contains("scatter chart needs numbers in \"month\""));
509        assert!(err(json!("garbage")).starts_with("Can't draw this chart"));
510        assert!(err(json!({"type": "bar", "x": "week", "series": ["sales"], "data": rows()})).contains("Columns: costs, month, sales"));
511    }
512
513    #[test]
514    fn number_coercion_matches_hob() {
515        let n = |s: &str| to_number(Some(&json!(s)));
516        assert_eq!(n("1,234.5"), Ok(Some(1234.5)));
517        assert_eq!(n("€1,234"), Ok(Some(1234.0)));
518        assert_eq!(n(" 40 % "), Ok(Some(40.0)));
519        assert_eq!(n("$ 1 000"), Ok(Some(1000.0)));
520        assert_eq!(n("1,23"), Err(NotANumber));
521        assert_eq!(n("1,2345"), Err(NotANumber));
522        assert_eq!(n("-3.5e2"), Ok(Some(-350.0)));
523        assert_eq!(n(""), Ok(None));
524        assert_eq!(n("€"), Ok(Some(0.0)));
525        assert_eq!(n("abc"), Err(NotANumber));
526        assert_eq!(n("Infinity"), Err(NotANumber));
527        assert_eq!(n("NaN"), Err(NotANumber));
528        assert_eq!(to_number(None), Ok(None));
529        assert_eq!(to_number(Some(&Value::Null)), Ok(None));
530        assert_eq!(to_number(Some(&json!(true))), Err(NotANumber));
531        assert_eq!(to_number(Some(&json!([1]))), Err(NotANumber));
532        // nulls are kept as gaps
533        let spec = validate(&json!({"type": "line", "x": "m", "series": ["v"], "data": [{"m": "a", "v": null}, {"m": "b", "v": ""}, {"m": "c"}]})).unwrap();
534        assert!(spec.data.iter().all(|r| r["v"].is_null()));
535    }
536
537    #[test]
538    fn dates_along_the_bottom_are_sorted() {
539        let sorted = |data: Value, kind: &str| -> Vec<Value> {
540            validate(&json!({"type": kind, "x": "d", "series": ["v"], "data": data})).unwrap().data.into_iter().map(|r| r["d"].clone()).collect()
541        };
542        assert_eq!(
543            sorted(json!([{"d": "5/18/2026", "v": 1}, {"d": "5/6/2026", "v": 2}, {"d": "4/29/2026", "v": 3}]), "line"),
544            vec![json!("4/29/2026"), json!("5/6/2026"), json!("5/18/2026")]
545        );
546        assert_eq!(sorted(json!([{"d": "2026-03-02", "v": 1}, {"d": "2026-02-27", "v": 2}]), "bar"), vec![json!("2026-02-27"), json!("2026-03-02")]);
547        assert_eq!(sorted(json!([{"d": "Mar", "v": 1}, {"d": "Jan", "v": 2}]), "line"), vec![json!("Mar"), json!("Jan")]);
548        assert_eq!(sorted(json!([{"d": "North", "v": 1}, {"d": "2026-01-01", "v": 2}]), "line"), vec![json!("North"), json!("2026-01-01")]);
549        assert_eq!(sorted(json!([{"d": "2026-03-02", "v": 1}, {"d": "2026-02-27", "v": 2}]), "pie"), vec![json!("2026-03-02"), json!("2026-02-27")]);
550        assert_eq!(
551            sorted(json!([{"d": "Mar 2026", "v": 1}, {"d": "Jan 5, 2026", "v": 2}, {"d": "2025-12", "v": 3}]), "bar"),
552            vec![json!("2025-12"), json!("Jan 5, 2026"), json!("Mar 2026")]
553        );
554    }
555
556    #[test]
557    fn date_parsing() {
558        let d = |s: &str| date_of(&json!(s));
559        assert_eq!(d("1970-01-02"), Some(86_400));
560        assert_eq!(d("1970-01-01T01:00:00Z"), Some(3600));
561        assert_eq!(d("1970-01-01 00:01"), Some(60));
562        assert_eq!(d("2026-02-30"), None);
563        assert_eq!(d("5 March 2026"), d("2026-03-05"));
564        assert_eq!(d("Thu, 5 Mar 2026"), d("2026-03-05"));
565        assert_eq!(d("3/5/26"), d("2026-03-05"));
566        assert_eq!(d("Mar"), None);
567        assert_eq!(d("2026"), None);
568        assert_eq!(d("North 5"), None);
569        assert_eq!(d("Marble 5 2026"), None);
570        assert_eq!(date_of(&json!(5)), None);
571    }
572
573    #[test]
574    fn title_unit_and_stacked() {
575        let spec =
576            validate(&json!({"type": "area", "x": "month", "series": ["sales", "costs"], "data": rows(), "unit": "  € ", "stacked": true, "title": "  "}))
577                .unwrap();
578        assert_eq!(spec.title, "sales and costs");
579        assert_eq!(spec.unit.as_deref(), Some("€"));
580        assert!(spec.stacked);
581        let long = validate(
582            &json!({"type": "bar", "x": "month", "series": "sales", "data": rows(), "title": "t".repeat(500), "unit": "u".repeat(50), "stacked": "yes"}),
583        )
584        .unwrap();
585        assert_eq!(long.title.chars().count(), MAX_TITLE_CHARS);
586        assert_eq!(long.unit.map(|u| u.chars().count()), Some(MAX_UNIT_CHARS));
587        assert!(!long.stacked);
588    }
589
590    #[test]
591    fn x_values_become_strings_or_stay_numbers() {
592        let spec =
593            validate(&json!({"type": "bar", "x": "k", "series": ["v"], "data": [{"k": 2024, "v": 1}, {"k": true, "v": 2}, {"k": null, "v": 3}, {"v": 4}]}))
594                .unwrap();
595        let xs: Vec<Value> = spec.data.iter().map(|r| r["k"].clone()).collect();
596        assert_eq!(xs, vec![json!(2024), json!("true"), json!(""), json!("")]);
597        assert_eq!(spec.label(&spec.data[0]), "2024");
598    }
599
600    #[test]
601    fn reads_simple_csv() {
602        let rows = parse_csv("month,sales\nJan,10\n\"Feb, late\",30\n");
603        assert_eq!(serde_json::to_value(rows).unwrap(), json!([{"month": "Jan", "sales": "10"}, {"month": "Feb, late", "sales": "30"}]));
604        let rows = parse_csv("\u{feff}a,b\r\n\r\n1,\"x \"\"y\"\"\"\r\n2\r\n");
605        assert_eq!(serde_json::to_value(rows).unwrap(), json!([{"a": "1", "b": "x \"y\""}, {"a": "2", "b": ""}]));
606        assert!(parse_csv("").is_empty());
607        assert!(parse_csv("only,header").is_empty());
608    }
609
610    #[test]
611    fn hostile_input_never_panics() {
612        for v in [json!(null), json!([]), json!({"type": "bar", "data": "x"}), json!({"type": "bar", "x": {}, "series": [1, null, {}], "data": [1, "a", {}]})] {
613            assert!(validate(&v).is_err());
614        }
615    }
616}