1use std::collections::BTreeSet;
73
74use crate::error::LogError;
75use crate::log::{Channel, Figure, FlightLog, LogFormat, Quantity, Stated};
76use crate::table::finite;
77
78const FORMAT: &str = "MARSA download";
80
81const RECOGNITION_LINES: usize = 200;
83
84pub const MAX_SETTINGS: usize = 256;
86
87pub const MAX_NAME_BYTES: usize = 64;
89
90pub const MAX_VALUE_CHARS: usize = 64;
92
93const FIRMWARE: &str = "FIRMWARE VER";
95
96pub const ALTITUDE_STEP_M: f64 = 29.3 * FOOT_M;
101
102const FOOT_M: f64 = 0.3048;
104
105const STANDARD_GRAVITY_M_S2: f64 = 9.806_65;
107
108const COLUMNS: [&str; 2] = ["altitude", "acceleration"];
110
111fn trimmed(line: &str) -> &str {
113 line.trim()
114 .trim_end_matches(|c: char| c == ',' || c.is_whitespace())
115}
116
117fn setting(line: &str) -> Option<(&str, &str)> {
121 let rest = line.strip_prefix('<')?;
122 let (name, value) = rest.split_once('>')?;
123 let allowed = |c: char| c.is_ascii_alphanumeric() || matches!(c, ' ' | '_' | '-' | '.');
124 if name.trim().is_empty() || !name.chars().all(allowed) {
125 return None;
126 }
127 Some((name.trim(), value.trim()))
128}
129
130pub fn recognizes(text: &str) -> bool {
133 let text = text.strip_prefix('\u{feff}').unwrap_or(text);
134 let mut lines = text
135 .split(['\r', '\n'])
136 .map(trimmed)
137 .filter(|line| !line.is_empty());
138 let Some(first) = lines.next() else {
139 return false;
140 };
141 setting(first).is_some()
142 && std::iter::once(first)
143 .chain(lines)
144 .take(RECOGNITION_LINES)
145 .any(
146 |line| matches!(setting(line), Some(("MARSA VERSION", value)) if !value.is_empty()),
147 )
148}
149
150fn sample(line: &str) -> Result<[f64; 3], String> {
152 let cells: Vec<&str> = line.split(',').map(str::trim).collect();
153 let [time, altitude, acceleration] = cells.as_slice() else {
154 return Err(format!(
155 "a sample is three numbers by commas, and the line has {} cells",
156 cells.len()
157 ));
158 };
159 let number = |cell: &str| {
160 cell.parse::<f64>()
161 .ok()
162 .filter(|value| value.is_finite())
163 .ok_or_else(|| format!("{:?} isn't a number", short(cell)))
164 };
165 Ok([number(time)?, number(altitude)?, number(acceleration)?])
166}
167
168pub fn read(text: &str) -> Result<FlightLog, LogError> {
177 let text = text.strip_prefix('\u{feff}').unwrap_or(text);
178 if !recognizes(text) {
179 return Err(LogError::NotThisFormat {
180 format: FORMAT,
181 message: "the first line isn't a `<NAME> value` setting, or no `<MARSA VERSION>` \
182 setting comes within the first 200 lines"
183 .to_owned(),
184 });
185 }
186 let syntax = |line: usize, message: String| LogError::Syntax {
187 format: FORMAT,
188 line,
189 message,
190 };
191 let ends_unended = !text.ends_with(['\r', '\n']);
192 let text = text.replace("\r\n", "\n").replace('\r', "\n");
194 let lines: Vec<(usize, &str)> = text
195 .split('\n')
196 .enumerate()
197 .map(|(index, line)| (index + 1, trimmed(line)))
198 .filter(|(_, line)| !line.is_empty())
199 .collect();
200 let last_line = lines.last().map(|&(number, _)| number);
201
202 let mut figures = Vec::new();
203 let mut named = BTreeSet::new();
204 let mut section: Option<&str> = None;
205 let mut settings = 0;
206 let mut firmware = None;
207 let mut time_s: Vec<f64> = Vec::new();
208 let mut altitude_m = Vec::new();
209 let mut acceleration = Vec::new();
210 let mut dropped_last = false;
211 for (number, line) in lines {
212 if let Some((name, value)) = setting(line) {
213 if !time_s.is_empty() {
214 return Err(syntax(
215 number,
216 "a setting among the samples: the settings come before them".to_owned(),
217 ));
218 }
219 settings += 1;
220 if settings > MAX_SETTINGS {
221 return Err(syntax(
222 number,
223 format!("more than {MAX_SETTINGS} settings and headings"),
224 ));
225 }
226 if name.len() > MAX_NAME_BYTES || value.chars().count() > MAX_VALUE_CHARS {
227 return Err(syntax(
228 number,
229 format!(
230 "a setting's name is at most {MAX_NAME_BYTES} bytes and its value \
231 {MAX_VALUE_CHARS} characters: {:?}",
232 short(line)
233 ),
234 ));
235 }
236 if value.is_empty() {
237 section = Some(name);
238 continue;
239 }
240 let full = match section {
241 Some(heading) => format!("{heading}, {name}"),
242 None => name.to_owned(),
243 };
244 if !named.insert(full.clone()) {
245 return Err(syntax(
246 number,
247 format!("the setting `{}` is named twice", short(&full)),
248 ));
249 }
250 if section.is_none() && name == FIRMWARE {
251 firmware = Some(value.to_owned());
252 }
253 let figure = match value.parse::<f64>().ok().filter(|v| v.is_finite()) {
254 Some(number) => Figure::new(full, value, Some(Quantity::Number), vec![number]),
255 None => Figure::new(full, value, None, Vec::new()),
256 };
257 figures.push(figure);
258 continue;
259 }
260 let [time, altitude, accel] = match sample(line) {
261 Ok(values) => values,
262 Err(_) if ends_unended && Some(number) == last_line => {
263 dropped_last = true;
264 continue;
265 }
266 Err(message) => return Err(syntax(number, message)),
267 };
268 if let Some(&before) = time_s.last()
269 && time <= before
270 {
271 return Err(syntax(
272 number,
273 format!("the time {time} s isn't later than the one before, {before} s"),
274 ));
275 }
276 time_s.push(time);
277 altitude_m.push(finite(altitude * FOOT_M, FORMAT, number, COLUMNS[0])?);
278 acceleration.push(finite(
279 accel * STANDARD_GRAVITY_M_S2,
280 FORMAT,
281 number,
282 COLUMNS[1],
283 )?);
284 }
285 if time_s.is_empty() {
286 return Err(LogError::NoData { format: FORMAT });
287 }
288
289 let mut notes = vec![
290 "no line names the samples' three numbers. They are read as the time in s, the \
291 barometric altitude in ft and the acceleration in g, as MARSA's manuals and software \
292 give them. The sample logs' survey holds the feet and the g to the files"
293 .to_owned(),
294 "the acceleration reads about 0 at rest, not 1 g: it is kept so, the rocket's own \
295 acceleration only while it points straight up"
296 .to_owned(),
297 "the settings are kept as written, as no document states their units".to_owned(),
298 ];
299 if dropped_last {
300 notes.push(
301 "the last line, which ends the file with no line end and isn't a whole sample, \
302 dropped: a download cut off as it was written"
303 .to_owned(),
304 );
305 }
306 let channel = |column: &str, quantity, values: &[f64]| Channel {
307 column: column.to_owned(),
308 quantity,
309 time_s: time_s.clone(),
310 values: values.to_vec(),
311 };
312 let channels = vec![
313 channel(COLUMNS[0], Quantity::AltitudeAboveGround, &altitude_m),
314 channel(COLUMNS[1], Quantity::Acceleration, &acceleration),
315 ];
316 Ok(FlightLog {
317 format: LogFormat::Marsa,
318 logger: "MARSA".to_owned(),
319 serial_number: None,
320 firmware,
321 flight_number: None,
322 stated: Stated {
323 apogee_m: None,
324 ground_elevation_msl_m: None,
325 samples: None,
326 figures,
327 },
328 time_s,
329 altitude_m,
330 vertical_speed_m_s: None,
331 temperature_k: None,
332 battery_v: None,
333 channels,
334 events: Vec::new(),
335 notes,
336 })
337}
338
339const QUOTED_CHARS: usize = 80;
341
342fn short(text: &str) -> String {
344 if text.chars().count() <= QUOTED_CHARS {
345 text.to_owned()
346 } else {
347 let mut cut: String = text.chars().take(QUOTED_CHARS - 1).collect();
348 cut.push('…');
349 cut
350 }
351}
352
353#[cfg(test)]
354mod tests {
355 use super::*;
356
357 const DOWNLOAD: &str = "<BARO_ZERO 0> 790\r\n<ACCEL_ZERO 0> 498\r\n<FIRMWARE VER> 26\r\n\
360 <MARSA VERSION> 2\r\n<CHAN 1>\r\n<CHAN APOGEE OPTION> 1\r\n\
361 <CHAN 2>\r\n<CHAN APOGEE OPTION> 0\r\n\
362 -0.1,-58.1,-0.08 \r\n0.0,-58.1,8.20 \r\n0.1,-29.0,8.65 \r\n\
363 0.2,0.0,9.00 \r\n";
364
365 fn error_line(text: &str) -> (usize, String) {
366 match read(text) {
367 Err(LogError::Syntax { line, message, .. }) => (line, message),
368 other => panic!("expected a syntax error, got {other:?}"),
369 }
370 }
371
372 #[test]
373 fn the_samples_read_in_seconds_feet_and_g() {
374 assert!(recognizes(DOWNLOAD));
375 let log = read(DOWNLOAD).unwrap();
376 assert_eq!(log.format, LogFormat::Marsa);
377 assert_eq!(log.time_s, [-0.1, 0.0, 0.1, 0.2]);
378 assert_eq!(
379 log.altitude_m,
380 [-58.1 * 0.3048, -58.1 * 0.3048, -29.0 * 0.3048, 0.0]
381 );
382 let acceleration = &log.channels[1];
383 assert_eq!(
384 (acceleration.column.as_str(), acceleration.quantity),
385 ("acceleration", Quantity::Acceleration)
386 );
387 assert_eq!(acceleration.values[1], 8.20 * 9.806_65);
388 assert_eq!(log.channels[0].quantity, Quantity::AltitudeAboveGround);
389 assert_eq!(log.firmware.as_deref(), Some("26"));
390 }
391
392 #[test]
393 fn each_setting_is_a_figure_named_after_its_section() {
394 let log = read(DOWNLOAD).unwrap();
395 let names: Vec<&str> = log.stated.figures.iter().map(|f| f.name.as_str()).collect();
396 assert_eq!(
397 names,
398 [
399 "BARO_ZERO 0",
400 "ACCEL_ZERO 0",
401 "FIRMWARE VER",
402 "MARSA VERSION",
403 "CHAN 1, CHAN APOGEE OPTION",
404 "CHAN 2, CHAN APOGEE OPTION",
405 ]
406 );
407 let zero = &log.stated.figures[0];
408 assert_eq!(
409 (zero.written.as_str(), zero.quantity, zero.values.as_slice()),
410 ("790", Some(Quantity::Number), &[790.0][..])
411 );
412 }
413
414 #[test]
415 fn a_spreadsheets_resave_reads_the_same() {
416 let resaved = DOWNLOAD
417 .replace(" \r\n", "\r\n")
418 .replace("> 790\r\n", "> 790,,\r\n")
419 .replace("<CHAN 1>\r\n", "<CHAN 1>,,\r\n")
420 .replace("0.2,0.0,9.00", "0.2,0,9");
421 assert_ne!(resaved, DOWNLOAD);
422 let (a, b) = (read(DOWNLOAD).unwrap(), read(&resaved).unwrap());
423 assert_eq!(a.time_s, b.time_s);
424 assert_eq!(a.altitude_m, b.altitude_m);
425 assert_eq!(a.channels, b.channels);
426 assert_eq!(a.stated, b.stated);
427 }
428
429 #[test]
430 fn a_time_no_later_than_the_one_before_is_refused_at_its_line() {
431 let text = DOWNLOAD.replace("0.1,-29.0", "0.0,-29.0");
432 let (line, message) = error_line(&text);
433 assert_eq!(line, 11);
434 assert!(message.contains("isn't later"), "{message}");
435 }
436
437 #[test]
438 fn a_line_that_isnt_three_numbers_is_refused_at_its_line() {
439 for (row, expected) in [
440 ("0.1,-29.0", "cells"),
441 ("0.1,-29.0,8.65,1", "cells"),
442 ("0.1,x,8.65", "isn't a number"),
443 ("0.1,inf,8.65", "isn't a number"),
444 ] {
445 let text = DOWNLOAD.replace("0.1,-29.0,8.65 ", row);
446 let (line, message) = error_line(&text);
447 assert_eq!(line, 11, "{row}");
448 assert!(message.contains(expected), "{row}: {message}");
449 }
450 }
451
452 #[test]
453 fn a_setting_among_the_samples_or_named_twice_is_refused() {
454 let among = DOWNLOAD.replace("0.1,-29.0,8.65 ", "<CHAN 3>");
455 assert!(error_line(&among).1.contains("among the samples"));
456 let twice = DOWNLOAD.replace("<ACCEL_ZERO 0>", "<BARO_ZERO 0>");
457 let (line, message) = error_line(&twice);
458 assert_eq!(line, 2);
459 assert!(
460 message.contains("`BARO_ZERO 0` is named twice"),
461 "{message}"
462 );
463 assert!(read(DOWNLOAD).is_ok());
465 }
466
467 #[test]
468 fn a_cut_last_line_is_dropped_only_with_no_line_end() {
469 let cut = format!("{DOWNLOAD}0.3,29");
470 let log = read(&cut).unwrap();
471 assert_eq!(log.time_s.len(), 4);
472 assert!(log.notes.iter().any(|n| n.contains("cut off")));
473 let ended = format!("{DOWNLOAD}0.3,29\r\n");
474 assert_eq!(error_line(&ended).0, 13);
475 let whole = format!("{DOWNLOAD}0.3,29.1,9.1");
476 let log = read(&whole).unwrap();
477 assert_eq!(log.time_s.len(), 5);
478 assert!(!log.notes.iter().any(|n| n.contains("cut off")));
479 }
480
481 #[test]
482 fn the_bounds_hold_at_their_limits() {
483 let settings: String = (1..MAX_SETTINGS)
484 .map(|i| format!("<S {i}> {i}\n"))
485 .collect();
486 let text = format!("<MARSA VERSION> 2\n{settings}0,0,0\n");
487 assert_eq!(read(&text).unwrap().stated.figures.len(), MAX_SETTINGS);
488 let over = format!("<S 0> 0\n{text}");
489 assert!(error_line(&over).1.contains("more than 256"));
490 let long_name = "N".repeat(MAX_NAME_BYTES);
491 let at = format!("<{long_name}> 1\n<MARSA VERSION> 2\n0,0,0\n");
492 assert!(read(&at).is_ok());
493 let past = format!("<{long_name}N> 1\n<MARSA VERSION> 2\n0,0,0\n");
494 assert!(error_line(&past).1.contains("at most 64 bytes"));
495 let value = "9".repeat(MAX_VALUE_CHARS);
496 assert!(read(&format!("<V> {value}\n<MARSA VERSION> 2\n0,0,0\n")).is_ok());
497 let past = format!("<V> {value}9\n<MARSA VERSION> 2\n0,0,0\n");
498 assert_eq!(error_line(&past).0, 1);
499 }
500
501 #[test]
502 fn a_value_too_large_for_its_conversion_is_refused() {
503 let text = DOWNLOAD.replace("0.1,-29.0,8.65", "0.1,-29.0,1e308");
504 let (line, message) = error_line(&text);
505 assert_eq!(line, 11);
506 assert!(message.contains("acceleration"), "{message}");
507 }
508
509 #[test]
511 fn a_byte_order_mark_is_passed_over() {
512 let marked = format!("\u{feff}{DOWNLOAD}");
513 assert!(recognizes(&marked));
514 assert_eq!(read(&marked).unwrap(), read(DOWNLOAD).unwrap());
515 }
516
517 #[test]
518 fn a_file_with_no_version_setting_or_no_samples_isnt_read() {
519 let unversioned = DOWNLOAD.replace("<MARSA VERSION> 2\r\n", "");
520 assert!(!recognizes(&unversioned));
521 assert!(matches!(
522 read(&unversioned),
523 Err(LogError::NotThisFormat { .. })
524 ));
525 assert!(!recognizes(
527 &DOWNLOAD.replace("<MARSA VERSION> 2", "<MARSA VERSION>")
528 ));
529 let settings_only = "<BARO_ZERO 0> 790\n<MARSA VERSION> 2\n";
530 assert!(matches!(read(settings_only), Err(LogError::NoData { .. })));
531 assert!(!recognizes(
533 "<?xml version=\"1.0\"?>\n<MARSA VERSION> 2\n0,0,0\n"
534 ));
535 let kml = "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<MARSA VERSION> 2\n</kml>\n";
536 assert!(!recognizes(kml));
537 }
538}