Skip to main content

hpr_flightdata/
altos_telem_csv.rs

1//! AltOS's radio telemetry decoded to a table: one row a packet, under a header naming the
2//! packet's fields, `serial,tick,ptype,state,v_batt,…,height,crc`.
3//!
4//! An Altus Metrum flight computer's radio packet opens with the board's serial number, its clock
5//! in ticks of 10 ms and the packet's type, and the type lays out the rest ([`crate::altos_telem`];
6//! *AltOS Telemetry*, Bdale Garbee and Keith Packard, v1.9.22.2, 2025, §2.1, p. 2). A TeleMega
7//! sends its sensors in two packets, and type `0x09`, "TeleMega Kalman and Voltage Data for 15V
8//! boards", holds the flight's `state`, `v_batt`, `v_pyro`, the six pyro channels' continuity
9//! `sense`, `ground_pres`, `ground_accel`, `accel_plus_g`, `accel_minus_g`, `acceleration` in
10//! sixteenths of a m/s², `speed` in sixteenths of a m/s and `height` in meters (§2.3, Tables 4
11//! and 6, pp. 3–4). One sample file is such packets decoded to a CSV, by software it doesn't
12//! name: its header names a row's serial number, its tick, its packet type and a `crc` flag,
13//! then the packet's fields by the document's names, the scales already applied:
14//!
15//! ```text
16//! serial,tick,ptype,state,v_batt,v_pyro,sense,v_apogee,v_main,ground_pres,ground_accel,accel_plus_g,accel_minus_g,acceleration,speed,height,crc
17//! 1234,402.94,9,3,4.1,4.19,0.00 0.00 0.00 4.20,4.2,4.14,98500,-28,-29,10,62.25,27.375,6,TRUE
18//! ```
19//!
20//! **Recognition.** The header, after any byte-order mark, opens `serial,tick,ptype,`.
21//!
22//! **Reading by the header.** Each column is read by its name:
23//!
24//! | Columns | Read as | Why |
25//! |---|---|---|
26//! | `tick` | the clock, s, as written | the table writes the tick in seconds, to the hundredth, the tick's 10 ms |
27//! | `height` | [`Quantity::AltitudeAboveGround`], m, and the primary track | the document's meters above the pad: the board's own estimate |
28//! | `acceleration` | [`Quantity::Acceleration`], m/s² | the document's sixteenths of a m/s², divided out: the board's estimate along its axis, about 0 at rest |
29//! | `speed` | [`Quantity::Speed`], m/s | the document's sixteenths of a m/s, divided out: the board's estimate, which leans on its accelerometer |
30//! | `ground_pres` | [`Quantity::Pressure`], Pa, where the column's first value lies between 10 and 120 kPa, as every launch site's pad does; as written otherwise | the document's barometer reading on the ground |
31//! | `v_batt`, `v_pyro`, `v_apogee`, `v_main`, each number of `sense` | [`Quantity::Voltage`], V | the document sends the converter's counts, but the table holds volts: 4.1 for the battery, a single LiPo cell's, the battery a TeleMega takes (*The Altus Metrum System*, v1.9.22.2, §3.2, p. 6); only `v_batt` is held to a band, by the survey |
32//! | `serial`, `ptype`, `state` | [`Quantity::Number`], as written | the board's serial number, the packet's type and the flight's state |
33//! | `ground_accel`, `accel_plus_g`, `accel_minus_g` | [`Quantity::Unrecognized`], as written | the accelerometer's counts, as the telemetry reader keeps them |
34//!
35//! Every other column is kept as written, [`Quantity::Unrecognized`], with a note. `sense` holds
36//! several numbers in one cell, by spaces, one a pyro channel: each is its own channel, `sense 1`
37//! and on, and every row must hold as many as the first. A TeleMega has six pyro channels, four of
38//! them programmable (*The Altus Metrum System*, §1, p. 3), and the sample file's `sense` holds
39//! four, beside `v_apogee` and `v_main`.
40//!
41//! **The flight's states.** Each change of `state` is an [`Event`], named as AltosUI's exports
42//! name the state's number ([`crate::altos_telem`]): 2 `pad`, 3 `boost`, 4 `fast`, 5 `coast`, 6
43//! `drogue`, 7 `main` and 8 `landed`; another number is `state N`.
44//!
45//! **What is left out, and refused.** A row whose `crc` is `FALSE`, a packet whose check failed,
46//! is left out and counted in a note: its bytes are noise. A `crc` that is neither `TRUE` nor
47//! `FALSE`, a row with another number of cells than the header, a row whose serial number or packet
48//! type isn't the first row's (two boards, or one table holding two packets' fields), and a tick
49//! no later than the one before refuse the file at the row's line. The board's tick turns over
50//! every 655.36 s (§2.1), and a table of seconds can't tell a turn from a board switched off and
51//! on, so this reader doesn't guess. A last line the file ends in with no line end, which isn't a
52//! whole row, is dropped with a note. A cell that is empty or not a finite number is a gap, `NaN`,
53//! counted per column in a note.
54//!
55//! **The primary track.** [`FlightLog::altitude_m`] is `height`, the board's own estimate of its
56//! height above the pad, on the rows' ticks; [`FlightLog::battery_v`] is `v_batt`. The vertical
57//! speed is absent: `speed` leans on the accelerometer, as for [AltOS's CSV](crate::altos).
58//!
59//! The document is published under CC BY-SA 3.0; this reader restates its facts, not its tables.
60
61use crate::altos_telem::state_name;
62use crate::error::LogError;
63use crate::log::{Channel, Event, FlightLog, LogFormat, Quantity, Stated};
64
65/// The format's name in errors.
66const FORMAT: &str = "AltOS telemetry CSV";
67
68/// What the header opens with.
69const HEADER_START: &str = "serial,tick,ptype,";
70
71/// The most columns a header may name.
72pub const MAX_COLUMNS: usize = 256;
73
74/// The most numbers a `sense` cell may hold.
75pub const MAX_SENSES: usize = 16;
76
77/// The pressures a launch site's pad can have, Pa.
78const PAD_PRESSURE_PA: std::ops::RangeInclusive<f64> = 10_000.0..=120_000.0;
79
80/// The most characters of the file an error or note quotes.
81const QUOTED_CHARS: usize = 80;
82
83/// What a column is to this reader.
84#[derive(Debug, Clone, Copy, PartialEq)]
85enum Role {
86    /// `tick`: the clock.
87    Clock,
88    /// `crc`: whether the row's packet passed its check.
89    Crc,
90    /// `sense`: several voltages in one cell.
91    Senses,
92    /// `ground_pres`: a pressure, Pa where the pad's lies in [`PAD_PRESSURE_PA`].
93    Pressure,
94    /// A channel of this quantity, as written.
95    Value(Quantity),
96}
97
98/// A column's role from its name, and whether the reader knows the name.
99fn role(name: &str) -> (Role, bool) {
100    use Quantity as Q;
101    let value = |quantity| (Role::Value(quantity), true);
102    match name {
103        "tick" => (Role::Clock, true),
104        "crc" => (Role::Crc, true),
105        "sense" => (Role::Senses, true),
106        "ground_pres" => (Role::Pressure, true),
107        "height" => value(Q::AltitudeAboveGround),
108        "acceleration" => value(Q::Acceleration),
109        "speed" => value(Q::Speed),
110        "v_batt" | "v_pyro" | "v_apogee" | "v_main" => value(Q::Voltage),
111        "serial" | "ptype" | "state" => value(Q::Number),
112        "ground_accel" | "accel_plus_g" | "accel_minus_g" => value(Q::Unrecognized),
113        _ => (Role::Value(Q::Unrecognized), false),
114    }
115}
116
117/// Whether a text is AltOS's telemetry decoded to a table: its header, after any byte-order
118/// mark, opens `serial,tick,ptype,`.
119pub fn recognizes(text: &str) -> bool {
120    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
121    text.split(['\r', '\n'])
122        .map(str::trim)
123        .find(|line| !line.is_empty())
124        .is_some_and(|line| line.starts_with(HEADER_START))
125}
126
127fn syntax(line: usize, message: String) -> LogError {
128    LogError::Syntax {
129        format: FORMAT,
130        line,
131        message,
132    }
133}
134
135/// A cell's value: a finite number, or `None`.
136fn number_in(cell: &str) -> Option<f64> {
137    cell.parse::<f64>().ok().filter(|value| value.is_finite())
138}
139
140/// Reads AltOS's telemetry decoded to a table, from its text.
141///
142/// # Errors
143///
144/// [`LogError::NotThisFormat`] if the header doesn't open `serial,tick,ptype,`;
145/// [`LogError::Syntax`] for a header naming a column twice, more than [`MAX_COLUMNS`] or no
146/// `height` or `crc`, and for a row that the module's docs say refuses the file;
147/// [`LogError::NoData`] if no row passed its check.
148pub fn read(text: &str) -> Result<FlightLog, LogError> {
149    if !recognizes(text) {
150        return Err(LogError::NotThisFormat {
151            format: FORMAT,
152            message: "the header doesn't open `serial,tick,ptype,`".to_owned(),
153        });
154    }
155    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
156    let ends_unended = !text.ends_with(['\r', '\n']);
157    let text = text.replace("\r\n", "\n").replace('\r', "\n");
158    let mut lines = text
159        .split('\n')
160        .enumerate()
161        .map(|(index, line)| (index + 1, line.trim()))
162        .filter(|(_, line)| !line.is_empty())
163        .peekable();
164    // `recognizes` has found the header.
165    let Some((header_line, header)) = lines.next() else {
166        return Err(LogError::NoData { format: FORMAT });
167    };
168    let names: Vec<&str> = header.split(',').map(str::trim).collect();
169    if names.len() > MAX_COLUMNS {
170        return Err(syntax(
171            header_line,
172            format!("the header names more than {MAX_COLUMNS} columns"),
173        ));
174    }
175    for (index, name) in names.iter().enumerate() {
176        if names[..index].contains(name) {
177            return Err(syntax(
178                header_line,
179                format!("the header names `{}` twice", short(name)),
180            ));
181        }
182    }
183    let find = |wanted: &str| names.iter().position(|name| *name == wanted);
184    for required in ["height", "crc"] {
185        if find(required).is_none() {
186            return Err(syntax(
187                header_line,
188                format!("the header names no `{required}` column"),
189            ));
190        }
191    }
192    let roles: Vec<(Role, bool)> = names.iter().map(|name| role(name)).collect();
193    // `recognizes` has found `tick` and `ptype`, and `serial` before them.
194    let (clock, ptype, serial) = (find("tick"), find("ptype"), find("serial"));
195
196    let mut time_s: Vec<f64> = Vec::new();
197    let mut values: Vec<Vec<f64>> = vec![Vec::new(); names.len()];
198    let mut senses: Vec<Vec<f64>> = Vec::new();
199    let mut gaps = vec![0_usize; names.len()];
200    let mut failed = 0;
201    let mut first: Option<(String, String)> = None;
202    let mut dropped_last = false;
203    while let Some((number, line)) = lines.next() {
204        let cells: Vec<&str> = line.split(',').map(str::trim).collect();
205        if cells.len() != names.len() {
206            if ends_unended && lines.peek().is_none() && cells.len() < names.len() {
207                dropped_last = true;
208                continue;
209            }
210            return Err(syntax(
211                number,
212                format!(
213                    "the row has {} cells and the header names {}",
214                    cells.len(),
215                    names.len()
216                ),
217            ));
218        }
219        let cell = |index: Option<usize>| index.map_or("", |index| cells[index]);
220        // A packet that failed its check is noise: left out before its serial number or type
221        // can stand for the table's.
222        let passed = match cell(find("crc")) {
223            "TRUE" => true,
224            "FALSE" => false,
225            other => {
226                return Err(syntax(
227                    number,
228                    format!("the `crc` cell is {:?}, not TRUE or FALSE", short(other)),
229                ));
230            }
231        };
232        if !passed {
233            failed += 1;
234            continue;
235        }
236        let identity = (cell(serial).to_owned(), cell(ptype).to_owned());
237        match &first {
238            None => first = Some(identity),
239            Some(stated) if *stated != identity => {
240                return Err(syntax(
241                    number,
242                    format!(
243                        "the row's serial number and packet type, {:?} and {:?}, aren't the \
244                         first row's, {:?} and {:?}. One table holds one board's packets of one \
245                         type",
246                        short(&identity.0),
247                        short(&identity.1),
248                        short(&stated.0),
249                        short(&stated.1)
250                    ),
251                ));
252            }
253            Some(_) => {}
254        }
255        let Some(time) = number_in(cell(clock)) else {
256            return Err(syntax(
257                number,
258                format!("the tick {:?} isn't a number", short(cell(clock))),
259            ));
260        };
261        if let Some(&before) = time_s.last()
262            && time <= before
263        {
264            return Err(syntax(
265                number,
266                format!(
267                    "the tick {time} s isn't later than the one before, {before} s. The board's \
268                     tick turns over every 655.36 s, and a table of seconds can't tell a turn \
269                     from a board switched off and on"
270                ),
271            ));
272        }
273        time_s.push(time);
274        for (index, (role, _)) in roles.iter().enumerate() {
275            match role {
276                Role::Clock | Role::Crc => {}
277                Role::Senses => {
278                    let parts: Vec<&str> = cells[index].split_whitespace().collect();
279                    if senses.is_empty() {
280                        if parts.is_empty() || parts.len() > MAX_SENSES {
281                            return Err(syntax(
282                                number,
283                                format!(
284                                    "the `sense` cell holds {} numbers: from 1 to {MAX_SENSES}",
285                                    parts.len()
286                                ),
287                            ));
288                        }
289                        senses = vec![Vec::new(); parts.len()];
290                    }
291                    if parts.len() != senses.len() {
292                        return Err(syntax(
293                            number,
294                            format!(
295                                "the `sense` cell holds {} numbers and the first row's {}",
296                                parts.len(),
297                                senses.len()
298                            ),
299                        ));
300                    }
301                    for (channel, part) in senses.iter_mut().zip(parts) {
302                        channel.push(number_in(part).unwrap_or_else(|| {
303                            gaps[index] += 1;
304                            f64::NAN
305                        }));
306                    }
307                }
308                Role::Pressure | Role::Value(_) => {
309                    values[index].push(number_in(cells[index]).unwrap_or_else(|| {
310                        gaps[index] += 1;
311                        f64::NAN
312                    }));
313                }
314            }
315        }
316    }
317    if time_s.is_empty() {
318        return Err(LogError::NoData { format: FORMAT });
319    }
320
321    let mut notes = Vec::new();
322    if failed > 0 {
323        notes.push(format!(
324            "{failed} {} whose `crc` is FALSE, a packet that failed its check, left out",
325            if failed == 1 { "row" } else { "rows" }
326        ));
327    }
328    let mut channels = Vec::new();
329    for (index, (role, known)) in roles.iter().enumerate() {
330        let name = names[index];
331        let quantity = match role {
332            Role::Clock | Role::Crc => continue,
333            Role::Senses => {
334                for (place, values) in std::mem::take(&mut senses).into_iter().enumerate() {
335                    channels.push(Channel {
336                        column: format!("sense {}", place + 1),
337                        quantity: Quantity::Voltage,
338                        time_s: time_s.clone(),
339                        values,
340                    });
341                }
342                if gaps[index] > 0 {
343                    notes.push(format!(
344                        "the `sense` column has {} numbers that aren't, read as gaps",
345                        gaps[index]
346                    ));
347                }
348                continue;
349            }
350            Role::Pressure => {
351                let pad = values[index].first().copied().unwrap_or(f64::NAN);
352                if PAD_PRESSURE_PA.contains(&pad) {
353                    Quantity::Pressure
354                } else {
355                    notes.push(format!(
356                        "`{name}`'s first value isn't a pad's pressure in Pa; it is kept as written"
357                    ));
358                    Quantity::Unrecognized
359                }
360            }
361            Role::Value(quantity) => *quantity,
362        };
363        if !known {
364            notes.push(format!(
365                "the column `{}` isn't one of the packet's fields this reader knows; its values \
366                 are kept as written",
367                short(name)
368            ));
369        }
370        if gaps[index] > 0 {
371            notes.push(format!(
372                "the `{}` column has {} empty or not a number, read as gaps",
373                short(name),
374                gaps[index]
375            ));
376        }
377        channels.push(Channel {
378            column: name.to_owned(),
379            quantity,
380            time_s: time_s.clone(),
381            values: std::mem::take(&mut values[index]),
382        });
383    }
384    let channel_values = |column: &str| {
385        channels
386            .iter()
387            .find(|channel| channel.column == column)
388            .map(|channel| channel.values.clone())
389    };
390    // `read` has refused a header without `height`.
391    let altitude_m = channel_values("height").unwrap_or_default();
392    let battery_v = channel_values("v_batt");
393    let mut events = Vec::new();
394    if let Some(states) = channel_values("state") {
395        let mut last = None;
396        for (&time, &state) in time_s.iter().zip(&states) {
397            if state.is_finite() && last != Some(state) {
398                // A whole number from 0 to 255 is a state's byte; another number is quoted short,
399                // so a file of huge ones can't make long names.
400                let name = (0..=u8::MAX)
401                    .find(|&byte| f64::from(byte) == state)
402                    .map_or_else(
403                        || format!("state {}", short(&state.to_string())),
404                        state_name,
405                    );
406                events.push(Event { time_s: time, name });
407                last = Some(state);
408            }
409        }
410    }
411    notes.push(
412        "`acceleration` is the board's own estimate along its axis with gravity taken out: about \
413         0 at rest, not 1 g. `speed` leans on the accelerometer, so the record has no vertical \
414         speed"
415            .to_owned(),
416    );
417    if dropped_last {
418        notes.push(
419            "the last line, which ends the file with no line end and isn't a whole row, dropped"
420                .to_owned(),
421        );
422    }
423    let serial_number = first
424        .map(|(serial, _)| serial)
425        .filter(|serial| !serial.is_empty())
426        .map(|serial| short(&serial));
427    Ok(FlightLog {
428        format: LogFormat::AltosTelemetryCsv,
429        logger: "AltOS".to_owned(),
430        serial_number,
431        firmware: None,
432        flight_number: None,
433        stated: Stated::default(),
434        time_s,
435        altitude_m,
436        vertical_speed_m_s: None,
437        temperature_k: None,
438        battery_v,
439        channels,
440        events,
441        notes,
442    })
443}
444
445/// Text from the file as an error or note quotes it, cut to [`QUOTED_CHARS`] characters.
446fn short(text: &str) -> String {
447    if text.chars().count() <= QUOTED_CHARS {
448        text.to_owned()
449    } else {
450        let mut cut: String = text.chars().take(QUOTED_CHARS - 1).collect();
451        cut.push('…');
452        cut
453    }
454}
455
456#[cfg(test)]
457mod tests {
458    use super::*;
459
460    const HEADER: &str = "serial,tick,ptype,state,v_batt,v_pyro,sense,v_apogee,v_main,\
461                          ground_pres,ground_accel,accel_plus_g,accel_minus_g,acceleration,speed,\
462                          height,crc\r\n";
463
464    /// A row of invented values.
465    fn row(tick: &str, state: u8, height: i32, crc: &str) -> String {
466        format!(
467            "1234,{tick},9,{state},4.1,4.2,0.00 0.00 0.00 4.20,4.2,4.14,98500,-28,-29,10,\
468             -0.5625,-1.25,{height},{crc}\r\n"
469        )
470    }
471
472    fn table(rows: &[String]) -> String {
473        format!("\u{feff}{HEADER}{}", rows.concat())
474    }
475
476    fn error_line(text: &str) -> (usize, String) {
477        match read(text) {
478            Err(LogError::Syntax { line, message, .. }) => (line, message),
479            other => panic!("expected a syntax error, got {other:?}"),
480        }
481    }
482
483    /// A packet that failed its check is noise: its serial number can't refuse the table nor
484    /// stand for it.
485    #[test]
486    fn a_failed_packet_neither_refuses_the_table_nor_names_its_board() {
487        let noisy = row("95.00", 2, 0, "FALSE").replacen("1234", "9z9", 1);
488        let mut rows = rows();
489        rows.insert(0, noisy);
490        let log = read(&table(&rows)).unwrap();
491        assert_eq!(log.serial_number.as_deref(), Some("1234"));
492        assert_eq!(log.time_s.len(), 3);
493    }
494
495    /// A state that isn't a byte is named, but its number is quoted short.
496    #[test]
497    fn a_state_that_isnt_a_byte_is_quoted_short() {
498        let huge = row("402.94", 3, 6, "TRUE").replace(",9,3,", ",9,1e300,");
499        let log = read(&table(&[row("95.27", 2, 0, "TRUE"), huge])).unwrap();
500        let name = &log.events[1].name;
501        assert!(
502            name.starts_with("state 1000") && name.chars().count() <= 6 + QUOTED_CHARS,
503            "{name}"
504        );
505    }
506
507    fn rows() -> Vec<String> {
508        vec![
509            row("95.27", 2, 0, "TRUE"),
510            row("402.94", 3, 6, "TRUE"),
511            row("403.05", 3, 9, "FALSE"),
512            row("403.16", 4, 14, "TRUE"),
513        ]
514    }
515
516    #[test]
517    fn each_column_reads_by_its_name() {
518        let text = table(&rows());
519        assert!(recognizes(&text));
520        let log = read(&text).unwrap();
521        assert_eq!(log.format, LogFormat::AltosTelemetryCsv);
522        assert_eq!(log.time_s, [95.27, 402.94, 403.16]);
523        assert_eq!(log.altitude_m, [0.0, 6.0, 14.0]);
524        assert_eq!(log.battery_v.as_deref(), Some(&[4.1, 4.1, 4.1][..]));
525        assert_eq!(log.serial_number.as_deref(), Some("1234"));
526        let quantity = |column: &str| {
527            log.channels
528                .iter()
529                .find(|c| c.column == column)
530                .map(|c| c.quantity)
531        };
532        assert_eq!(quantity("acceleration"), Some(Quantity::Acceleration));
533        assert_eq!(quantity("speed"), Some(Quantity::Speed));
534        assert_eq!(quantity("ground_pres"), Some(Quantity::Pressure));
535        assert_eq!(quantity("v_main"), Some(Quantity::Voltage));
536        assert_eq!(quantity("ground_accel"), Some(Quantity::Unrecognized));
537        assert_eq!(quantity("state"), Some(Quantity::Number));
538        assert_eq!(quantity("tick"), None);
539        assert_eq!(quantity("crc"), None);
540        let senses: Vec<&Channel> = log
541            .channels
542            .iter()
543            .filter(|c| c.column.starts_with("sense "))
544            .collect();
545        assert_eq!(senses.len(), 4);
546        assert_eq!(senses[3].values, [4.2, 4.2, 4.2]);
547        assert!(senses.iter().all(|c| c.quantity == Quantity::Voltage));
548        let events: Vec<(f64, &str)> = log
549            .events
550            .iter()
551            .map(|e| (e.time_s, e.name.as_str()))
552            .collect();
553        assert_eq!(
554            events,
555            [(95.27, "pad"), (402.94, "boost"), (403.16, "fast")]
556        );
557        assert!(
558            log.notes
559                .iter()
560                .any(|n| n.starts_with("1 row whose `crc` is FALSE"))
561        );
562    }
563
564    #[test]
565    fn a_row_of_another_board_or_packet_type_is_refused() {
566        let mut other_board = rows();
567        other_board[1] = other_board[1].replacen("1234", "4321", 1);
568        assert_eq!(error_line(&table(&other_board)).0, 3);
569        let mut other_type = rows();
570        other_type[1] = other_type[1].replacen(",9,", ",10,", 1);
571        let (line, message) = error_line(&table(&other_type));
572        assert_eq!(line, 3);
573        assert!(message.contains("one type"), "{message}");
574    }
575
576    #[test]
577    fn a_tick_no_later_than_the_one_before_is_refused() {
578        let mut back = rows();
579        back[3] = row("10.00", 6, 20, "TRUE");
580        let (line, message) = error_line(&table(&back));
581        assert_eq!(line, 5);
582        assert!(message.contains("655.36"), "{message}");
583        // A failed row's tick doesn't count.
584        let mut failed_back = rows();
585        failed_back[2] = row("1.00", 3, 9, "FALSE");
586        assert!(read(&table(&failed_back)).is_ok());
587    }
588
589    #[test]
590    fn a_crc_or_a_row_that_isnt_one_is_refused() {
591        let mut crc = rows();
592        crc[1] = row("402.94", 3, 6, "yes");
593        assert!(error_line(&table(&crc)).1.contains("not TRUE or FALSE"));
594        let mut long = rows();
595        long[1] = long[1].replace(",TRUE", ",TRUE,1");
596        assert!(error_line(&table(&long)).1.contains("18 cells"));
597        let mut senses = rows();
598        senses[1] = senses[1].replace("0.00 0.00 0.00 4.20", "0.00 4.20");
599        assert!(error_line(&table(&senses)).1.contains("first row's 4"));
600        let mut tick = rows();
601        tick[1] = row("x", 3, 6, "TRUE");
602        assert!(error_line(&table(&tick)).1.contains("isn't a number"));
603    }
604
605    #[test]
606    fn a_cut_last_row_is_dropped_only_with_no_line_end() {
607        let mut text = table(&rows());
608        text.push_str("1234,403.27,9,4");
609        let log = read(&text).unwrap();
610        assert_eq!(log.time_s.len(), 3);
611        assert!(log.notes.iter().any(|n| n.contains("no line end")));
612        text.push_str("\r\n");
613        assert_eq!(error_line(&text).0, 6);
614        // A whole last row with no line end reads, as the sample file's does.
615        let whole = table(&rows()).trim_end().to_owned();
616        assert_eq!(read(&whole).unwrap().time_s.len(), 3);
617    }
618
619    #[test]
620    fn a_header_without_height_or_crc_or_naming_one_twice_is_refused() {
621        let no_crc = table(&rows()).replace(",height,crc", ",height,check");
622        assert!(error_line(&no_crc).1.contains("no `crc`"));
623        let twice = table(&rows()).replace(",v_main,", ",v_batt,");
624        assert!(error_line(&twice).1.contains("`v_batt` twice"));
625        let wide = format!(
626            "serial,tick,ptype,height,crc,{}\n",
627            (0..MAX_COLUMNS)
628                .map(|i| format!("c{i}"))
629                .collect::<Vec<_>>()
630                .join(",")
631        );
632        assert!(error_line(&wide).1.contains("more than 256"));
633    }
634
635    #[test]
636    fn an_unknown_column_and_a_pressure_off_any_pad_are_kept_as_written() {
637        let text = table(&rows())
638            .replace(",crc\r\n", ",crc,rssi\r\n")
639            .replace(",TRUE\r\n", ",TRUE,-80\r\n")
640            .replace(",FALSE\r\n", ",FALSE,-80\r\n")
641            .replace(",98500,", ",985,");
642        let log = read(&text).unwrap();
643        let rssi = log.channels.iter().find(|c| c.column == "rssi").unwrap();
644        assert_eq!(rssi.quantity, Quantity::Unrecognized);
645        let pressure = log
646            .channels
647            .iter()
648            .find(|c| c.column == "ground_pres")
649            .unwrap();
650        assert_eq!(pressure.quantity, Quantity::Unrecognized);
651        assert!(log.notes.iter().any(|n| n.contains("`rssi`")));
652    }
653}