1use crate::error::LogError;
107use crate::log::{Channel, FlightLog, LogFormat, Quantity, Stated};
108use crate::table::{Notes, short};
109
110const RAW: &str = "RTx .rtx";
112
113const CSV: &str = "RTx CSV";
115
116const TEXT: &str = "RTx text";
118
119const LOGGER: &str = "Missile Works RTx";
121
122const SERIALIZATION_HEADER: &[u8] = &[
125 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00,
127];
128
129const CLASS_RECORD: &[u8] = &[0x04, 0x01, 0x00, 0x00, 0x00, 0x7e];
132
133const CLASS_NAME: &[u8] = b"System.Collections.Generic.List`1[[System.Int32, mscorlib, \
135Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]";
136
137const MEMBERS: &[u8] = b"\x03\x00\x00\x00\x06_items\x05_size\x08_version\x07\x00\x00\x08\x08\x08\
141\x09\x02\x00\x00\x00";
142
143const ARRAY_RECORD: &[u8] = &[0x0f, 0x02, 0x00, 0x00, 0x00];
145
146const INT32: u8 = 8;
148
149const MESSAGE_END: u8 = 0x0b;
151
152const RECORD_WORDS: usize = 16;
154
155const MODULUS: u64 = 1_000_000_000;
157
158const MULTIPLIER: u64 = 387_420_489;
160
161const INVERSE: u64 = 513_180_409;
163
164const DECIMALS_LEAD: u32 = 100_000;
167
168const DAY_S: f64 = 86_400.0;
170
171const NEXT_DAY_S: f64 = DAY_S / 2.0;
174
175const FOOT_M: f64 = 0.3048;
177
178const CSV_HEADER: [&str; 10] = [
180 "UTC Time",
181 "Latitude",
182 "Longitude",
183 "Altitude",
184 "Velocity",
185 "VDOP",
186 "NavStatus",
187 "Sats",
188 "SOG",
189 "COG",
190];
191
192const TEXT_HEADER: [&str; 12] = [
194 "UTC", "Lat", "", "Lon", "", "Alt", "Vel", "VDOP", "Nav", "Sats", "SOG", "COG",
195];
196
197pub const ALTITUDE_STEP_M: f64 = 1.0;
199
200pub const EXPORT_ALTITUDE_STEP_M: f64 = FOOT_M;
202
203pub fn recognizes_raw(bytes: &[u8]) -> bool {
206 bytes
207 .strip_prefix(SERIALIZATION_HEADER)
208 .and_then(|rest| rest.strip_prefix(CLASS_RECORD))
209 .is_some_and(|rest| rest.starts_with(CLASS_NAME))
210}
211
212fn export_header(line: &str) -> Option<LogFormat> {
214 if line.split(',').map(str::trim).eq(CSV_HEADER) {
215 Some(LogFormat::RtxCsv)
216 } else if line.split('\t').map(str::trim).eq(TEXT_HEADER) {
217 Some(LogFormat::RtxText)
218 } else {
219 None
220 }
221}
222
223pub fn export_format(text: &str) -> Option<LogFormat> {
226 text.strip_prefix('\u{feff}')
227 .unwrap_or(text)
228 .split(['\r', '\n'])
229 .find(|line| !line.trim().is_empty())
230 .and_then(export_header)
231}
232
233fn word(slot: i32) -> Option<u16> {
236 let slot = u64::try_from(slot).ok().filter(|&s| s < MODULUS)?;
237 u16::try_from(slot * INVERSE % MODULUS).ok()
238}
239
240#[doc(hidden)]
242pub fn slot(word: u16) -> i32 {
243 #[allow(
244 clippy::cast_possible_truncation,
245 reason = "a remainder modulo 10⁹ is within an `i32`"
246 )]
247 {
248 (u64::from(word) * MULTIPLIER % MODULUS) as i32
249 }
250}
251
252fn time_of_day(hhmmss: u64) -> Option<f64> {
255 let (hours, minutes, seconds) = (hhmmss / 10_000, hhmmss / 100 % 100, hhmmss % 100);
256 (hours < 24 && minutes < 60 && seconds <= 60).then(|| {
257 #[allow(
258 clippy::cast_precision_loss,
259 reason = "under 86,401 s, exact in an `f64`"
260 )]
261 {
262 (hours * 3_600 + minutes * 60 + seconds) as f64
263 }
264 })
265}
266
267fn coordinate(
270 degrees_minutes: u16,
271 decimals: u32,
272 hemisphere: u8,
273 letters: [u8; 2],
274 most: f64,
275) -> Option<f64> {
276 let decimals = decimals.checked_sub(DECIMALS_LEAD)?;
277 if decimals >= DECIMALS_LEAD || degrees_minutes % 100 >= 60 {
278 return None;
279 }
280 let minutes = f64::from(degrees_minutes % 100) + f64::from(decimals) / 1e5;
281 let value = f64::from(degrees_minutes / 100) + minutes / 60.0;
282 let sign = if hemisphere == letters[0] {
283 1.0
284 } else if hemisphere == letters[1] {
285 -1.0
286 } else {
287 return None;
288 };
289 (value <= most).then_some(sign * value)
290}
291
292#[derive(Default)]
294struct Counts {
295 bad_times: usize,
296 bad_time_example: Option<u64>,
297 bad_latitudes: usize,
298 bad_longitudes: usize,
299 no_fix: usize,
300 next_days: usize,
301 days_before: usize,
302}
303
304struct Fix {
306 hhmmss: u64,
307 latitude: Option<f64>,
308 longitude: Option<f64>,
309}
310
311fn clock(fixes: &[Fix], counts: &mut Counts) -> Option<(Vec<f64>, u64)> {
316 let mut zero: Option<(f64, u64)> = None;
317 let mut day = 0.0;
318 let mut last: Option<f64> = None;
319 let mut time_s = Vec::with_capacity(fixes.len());
320 for fix in fixes {
321 let Some(of_day) = time_of_day(fix.hhmmss) else {
322 counts.bad_times += 1;
323 counts.bad_time_example.get_or_insert(fix.hhmmss);
324 time_s.push(f64::NAN);
325 continue;
326 };
327 if last.is_some_and(|last| of_day + day < last - NEXT_DAY_S) {
328 day += DAY_S;
329 counts.next_days += 1;
330 } else if last.is_some_and(|last| of_day + day > last + NEXT_DAY_S) {
331 day -= DAY_S;
332 counts.days_before += 1;
333 }
334 let at = of_day + day;
335 last = Some(at);
336 let (zero_s, _) = *zero.get_or_insert((at, fix.hhmmss));
337 time_s.push(at - zero_s);
338 }
339 zero.map(|(_, hhmmss)| (time_s, hhmmss))
340}
341
342fn clock_notes(time_s: &[f64], zero: u64, counts: &Counts, quoted: &str) -> Vec<String> {
344 let mut notes = vec![format!(
345 "the log's clock is the GPS's time of day, UTC, `{quoted}`, in whole seconds. Its zero is \
346 the first record's, {:02}:{:02}:{:02}. The file states no date",
347 zero / 10_000,
348 zero / 100 % 100,
349 zero % 100
350 )];
351 let known: Vec<f64> = time_s.iter().copied().filter(|t| !t.is_nan()).collect();
352 let backs = known.windows(2).filter(|pair| pair[1] < pair[0]).count();
353 let largest = known
354 .windows(2)
355 .map(|pair| pair[0] - pair[1])
356 .fold(0.0, f64::max);
357 let mut sorted = known.clone();
358 sorted.sort_by(f64::total_cmp);
359 let repeats = sorted.windows(2).filter(|pair| pair[0] == pair[1]).count();
360 if backs > 0 {
361 notes.push(format!(
362 "times go back {backs} times from one record to the next, by up to {largest:.0} s. \
363 {repeats} records repeat a time already written. Every record is kept, in the \
364 file's order"
365 ));
366 }
367 if counts.next_days > 0 {
368 notes.push(format!(
369 "times fall more than 12 hours behind the one before {} times, each read as the next \
370 day's",
371 counts.next_days
372 ));
373 }
374 if counts.days_before > 0 {
375 notes.push(format!(
376 "times run more than 12 hours ahead of the one before {} times, each read as the day \
377 before's",
378 counts.days_before
379 ));
380 }
381 if counts.bad_times > 0 {
382 notes.push(format!(
383 "{} records hold a time that isn't a time of day, such as {}: their times are gaps",
384 counts.bad_times,
385 counts.bad_time_example.unwrap_or_default()
386 ));
387 }
388 for (count, what) in [
389 (counts.bad_latitudes, "latitudes"),
390 (counts.bad_longitudes, "longitudes"),
391 ] {
392 if count > 0 {
393 notes.push(format!(
394 "{count} {what} are out of range, so they are gaps. Out of range means minutes \
395 past 59, decimals without their leading 1, or a hemisphere byte other than its \
396 two letters. It also means a value beyond ±90° or ±180°"
397 ));
398 }
399 }
400 if counts.no_fix > 0 {
401 notes.push(format!(
402 "{} records have the latitude and longitude both 0, as a receiver writes before it \
403 has a fix: their positions are gaps",
404 counts.no_fix
405 ));
406 }
407 notes
408}
409
410fn position(
412 latitude: Option<f64>,
413 longitude: Option<f64>,
414 counts: &mut Counts,
415) -> (Option<f64>, Option<f64>) {
416 counts.bad_latitudes += usize::from(latitude.is_none());
417 counts.bad_longitudes += usize::from(longitude.is_none());
418 if latitude == Some(0.0) && longitude == Some(0.0) {
419 counts.no_fix += 1;
420 (None, None)
421 } else {
422 (latitude, longitude)
423 }
424}
425
426const NO_BAROMETER: &str = "the log has no barometer, so the record's altitude track is empty: \
428 an RTx's file holds GPS fixes only, and every field is a channel";
429
430fn channel(column: &str, quantity: Quantity, time_s: &[f64], values: Vec<f64>) -> Channel {
432 Channel {
433 column: column.to_owned(),
434 quantity,
435 time_s: time_s.to_vec(),
436 values,
437 }
438}
439
440fn fault(offset: usize, message: impl Into<String>) -> LogError {
442 LogError::Binary {
443 format: RAW,
444 offset,
445 message: message.into(),
446 }
447}
448
449fn int_at(bytes: &[u8], at: usize) -> i32 {
451 let mut four = [0; 4];
452 four.copy_from_slice(&bytes[at..at + 4]);
453 i32::from_le_bytes(four)
454}
455
456fn expect(bytes: &[u8], at: usize, wanted: &[u8], what: &str) -> Result<usize, LogError> {
458 match wanted
459 .iter()
460 .enumerate()
461 .find(|&(k, b)| bytes.get(at + k) != Some(b))
462 {
463 Some((k, _)) => Err(fault(
464 at + k,
465 format!("the stream isn't the RTx's list here: {what}"),
466 )),
467 None => Ok(at + wanted.len()),
468 }
469}
470
471pub fn read_raw(bytes: &[u8]) -> Result<FlightLog, LogError> {
479 if !recognizes_raw(bytes) {
480 return Err(LogError::NotThisFormat {
481 format: RAW,
482 message: "the file doesn't open as an MS-NRBF stream of the RTx's `List<Int32>`"
483 .to_owned(),
484 });
485 }
486 let mut at = expect(bytes, 0, SERIALIZATION_HEADER, "the serialization header")?;
487 at = expect(bytes, at, CLASS_RECORD, "the list's class record")?;
488 at = expect(bytes, at, CLASS_NAME, "the list's class name")?;
489 at = expect(bytes, at, MEMBERS, "the list's members")?;
490 if bytes.len() < at + 8 {
491 return Err(fault(bytes.len(), "the file ends inside the list's counts"));
492 }
493 let size_at = at;
494 let size = int_at(bytes, at);
495 at = expect(bytes, at + 8, ARRAY_RECORD, "the items' array record")?;
496 if bytes.len() < at + 5 {
497 return Err(fault(
498 bytes.len(),
499 "the file ends inside the items' array record",
500 ));
501 }
502 let length_at = at;
503 let length = usize::try_from(int_at(bytes, at))
504 .map_err(|_| fault(length_at, "the items' array has a negative length"))?;
505 at = expect(bytes, at + 4, &[INT32], "the items' type, `Int32`")?;
506 let data_at = at;
507 let end = length
508 .checked_mul(4)
509 .and_then(|n| n.checked_add(data_at))
510 .filter(|&end| end < bytes.len())
511 .ok_or_else(|| {
512 fault(
513 length_at,
514 format!("the items' {length} slots run past the file's end"),
515 )
516 })?;
517 if bytes[end] != MESSAGE_END || bytes.len() != end + 1 {
518 return Err(fault(
519 end,
520 "the stream doesn't end with `MessageEnd` straight after the items",
521 ));
522 }
523 let size = usize::try_from(size)
524 .ok()
525 .filter(|&s| s % RECORD_WORDS == 0 && s <= length)
526 .ok_or_else(|| {
527 fault(
528 size_at,
529 format!(
530 "the list's `_size`, {size}, isn't a whole number of {RECORD_WORDS}-word \
531 records within its {length} slots"
532 ),
533 )
534 })?;
535 let slot_at = |k: usize| data_at + 4 * k;
536 if let Some(k) = (size..length).find(|&k| int_at(bytes, slot_at(k)) != 0) {
537 return Err(fault(
538 slot_at(k),
539 format!("slot {k}, past the list's `_size` of {size}, isn't 0"),
540 ));
541 }
542 let mut records = Vec::with_capacity(size / RECORD_WORDS);
543 for r in 0..size / RECORD_WORDS {
544 let mut record = [0u16; RECORD_WORDS];
545 for (w, value) in record.iter_mut().enumerate() {
546 let k = r * RECORD_WORDS + w;
547 let raw = int_at(bytes, slot_at(k));
548 *value = word(raw).ok_or_else(|| {
549 fault(
550 slot_at(k),
551 format!(
552 "slot {k}, {raw}, isn't a 16-bit word times {MULTIPLIER} modulo \
553 {MODULUS}"
554 ),
555 )
556 })?;
557 }
558 records.push(record);
559 }
560 if records.is_empty() {
561 return Err(LogError::NoData { format: RAW });
562 }
563
564 let mut counts = Counts::default();
565 let fixes: Vec<Fix> = records
566 .iter()
567 .map(|w| {
568 let [north, east] = w[11].to_le_bytes();
569 let pair = |lo: u16, hi: u16| u32::from(hi) << 16 | u32::from(lo);
570 Fix {
571 hhmmss: u64::from(pair(w[0], w[1])),
572 latitude: coordinate(w[2], pair(w[3], w[4]), north, *b"NS", 90.0),
573 longitude: coordinate(w[5], pair(w[6], w[7]), east, *b"EW", 180.0),
574 }
575 })
576 .collect();
577 let (time_s, zero) = clock(&fixes, &mut counts).ok_or(LogError::NoData { format: RAW })?;
578 let (latitudes, longitudes): (Vec<f64>, Vec<f64>) = fixes
579 .iter()
580 .map(|fix| {
581 let (lat, lon) = position(fix.latitude, fix.longitude, &mut counts);
582 (lat.unwrap_or(f64::NAN), lon.unwrap_or(f64::NAN))
583 })
584 .unzip();
585 let field = |w: usize| -> Vec<f64> { records.iter().map(|r| f64::from(r[w])).collect() };
586 let byte = |w: usize, high: bool| -> Vec<f64> {
587 records
588 .iter()
589 .map(|r| f64::from(if high { r[w] >> 8 } else { r[w] & 0xff }))
590 .collect()
591 };
592 let channels = vec![
593 channel("Latitude", Quantity::Latitude, &time_s, latitudes),
594 channel("Longitude", Quantity::Longitude, &time_s, longitudes),
595 channel("Altitude", Quantity::GnssAltitude, &time_s, field(8)),
596 channel("Velocity", Quantity::Unrecognized, &time_s, field(9)),
597 channel("VDOP", Quantity::Number, &time_s, field(10)),
598 channel("NavStatus", Quantity::Number, &time_s, byte(12, false)),
599 channel("Sats", Quantity::Number, &time_s, byte(12, true)),
600 channel("SOG", Quantity::Unrecognized, &time_s, field(13)),
601 channel("COG", Quantity::Angle, &time_s, field(14)),
602 channel("word 15", Quantity::Unrecognized, &time_s, field(15)),
603 ];
604 let mut notes = vec![
605 NO_BAROMETER.to_owned(),
606 format!(
607 "each slot is its 16-bit word times {MULTIPLIER} modulo {MODULUS}, read back by \
608 multiplying by {INVERSE}; the list holds {} records of {RECORD_WORDS} words",
609 records.len()
610 ),
611 ];
612 notes.extend(clock_notes(&time_s, zero, &counts, "UTC Time"));
613 let at_zero = records.iter().filter(|r| r[8] == 0).count();
614 notes.extend(units_notes("Altitude", "Velocity", true, at_zero));
615 notes.push(
616 "`word 15` kept as written, a word the exports leave out: no document names it".to_owned(),
617 );
618 Ok(log(LogFormat::RtxRaw, records.len(), channels, notes))
619}
620
621fn units_notes(altitude: &str, velocity: &str, raw: bool, at_zero: usize) -> Vec<String> {
625 let mut notes = vec![
626 if raw {
627 format!(
628 "`{altitude}` read in meters, from the manual and the exports. The values are \
629 whole meters, above mean sea level as the RTx's manual states its GPS altitude. \
630 The KML export writes them the same way, and the CSV and text exports convert \
631 them to feet. A word can't fall below 0"
632 )
633 } else {
634 format!(
635 "`{altitude}` read in feet, from the `.rtx` files beside the sample exports. The \
636 values are whole feet, the `.rtx`'s meters × 3.280 84 rounded down, read as 0.3048 m each"
637 )
638 },
639 format!(
640 "`{velocity}` and `SOG` kept as written, in units no document states. `SOG` runs \
641 about 3.6 times the speed between fixes, as kilometers an hour would. The \
642 velocity runs far more than the climb rate while it climbs"
643 ),
644 "`COG` read in degrees, from the positions. It is the course over ground, within a few \
645 degrees of the bearing between the fixes either side while the tracker moves"
646 .to_owned(),
647 ];
648 if at_zero > 0 {
649 notes.push(format!(
650 "{at_zero} `{altitude}` values read exactly 0, the least the file can hold. Each \
651 stands for 0 m or lower, as an altitude clamped at 0 would"
652 ));
653 }
654 notes
655}
656
657fn log(format: LogFormat, samples: usize, channels: Vec<Channel>, notes: Vec<String>) -> FlightLog {
659 FlightLog {
660 format,
661 logger: LOGGER.to_owned(),
662 serial_number: None,
663 firmware: None,
664 flight_number: None,
665 stated: Stated {
666 samples: Some(samples),
667 ..Stated::default()
668 },
669 time_s: Vec::new(),
670 altitude_m: Vec::new(),
671 vertical_speed_m_s: None,
672 temperature_k: None,
673 battery_v: None,
674 channels,
675 events: Vec::new(),
676 notes,
677 }
678}
679
680fn coordinate_cell(cell: &str, letters: [u8; 2], most: f64) -> Result<Option<f64>, ()> {
683 if let Some(degrees) = cell.strip_suffix('°') {
684 let value: f64 = degrees.trim().parse().map_err(|_| ())?;
685 if !value.is_finite() {
686 return Err(());
687 }
688 return Ok((value.abs() <= most).then_some(value));
689 }
690 let hemisphere = *cell.as_bytes().last().ok_or(())?;
691 if !letters.contains(&hemisphere) {
692 return Err(());
693 }
694 let number = &cell[..cell.len() - 1];
695 let (whole, decimals) = number.split_once('.').ok_or(())?;
696 if whole.is_empty()
697 || decimals.len() != 5
698 || !whole
699 .bytes()
700 .chain(decimals.bytes())
701 .all(|b| b.is_ascii_digit())
702 {
703 return Err(());
704 }
705 let Ok(degrees_minutes) = whole.parse::<u16>() else {
707 return Ok(None);
708 };
709 let decimals: u32 = decimals.parse().map_err(|_| ())?;
710 Ok(coordinate(
711 degrees_minutes,
712 decimals + DECIMALS_LEAD,
713 hemisphere,
714 letters,
715 most,
716 ))
717}
718
719pub fn read_export(text: &str) -> Result<FlightLog, LogError> {
727 let mut lines =
728 crate::table::numbered_lines(text).skip_while(|(_, line)| line.trim().is_empty());
729 let Some(format) = lines.next().and_then(|(_, line)| export_header(line)) else {
730 return Err(LogError::NotThisFormat {
731 format: CSV,
732 message: "the first line that isn't blank isn't the RTx CSV's or text file's header"
733 .to_owned(),
734 });
735 };
736 let (name, separator, names) = match format {
737 LogFormat::RtxCsv => (CSV, ',', CSV_HEADER),
738 _ => (
739 TEXT,
740 '\t',
741 [
742 "UTC", "Lat", "Lon", "Alt", "Vel", "VDOP", "Nav", "Sats", "SOG", "COG",
743 ],
744 ),
745 };
746 let mut fixes = Vec::new();
747 let mut rows: Vec<[f64; 7]> = Vec::new();
748 for (line_number, line) in lines {
749 if line.trim().is_empty() {
750 continue;
751 }
752 let syntax = |message: String| LogError::Syntax {
753 format: name,
754 line: line_number,
755 message,
756 };
757 let cells: Vec<&str> = line.split(separator).map(str::trim).collect();
758 let [time, latitude, longitude, rest @ ..] = &cells[..] else {
759 return Err(syntax(format!("{} cells, not 10", cells.len())));
760 };
761 if cells.len() != 10 {
762 return Err(syntax(format!("{} cells, not 10", cells.len())));
763 }
764 let not_a =
765 |k: usize, cell: &str| syntax(format!("`{}` {} isn't one", names[k], short(cell)));
766 if time.is_empty() || time.len() > 10 || !time.bytes().all(|b| b.is_ascii_digit()) {
767 return Err(not_a(0, time));
768 }
769 let hhmmss: u64 = time.parse().map_err(|_| not_a(0, time))?;
770 let latitude = coordinate_cell(latitude, *b"NS", 90.0).map_err(|()| not_a(1, latitude))?;
771 let longitude =
772 coordinate_cell(longitude, *b"EW", 180.0).map_err(|()| not_a(2, longitude))?;
773 let mut values = [0.0; 7];
774 for (k, (value, cell)) in values.iter_mut().zip(rest).enumerate() {
775 *value = crate::table::number(cell, false).ok_or_else(|| not_a(k + 3, cell))?;
776 }
777 fixes.push(Fix {
778 hhmmss,
779 latitude,
780 longitude,
781 });
782 rows.push(values);
783 }
784 if fixes.is_empty() {
785 return Err(LogError::NoData { format: name });
786 }
787 let mut counts = Counts::default();
788 let (time_s, zero) = clock(&fixes, &mut counts).ok_or(LogError::NoData { format: name })?;
789 let (latitudes, longitudes): (Vec<f64>, Vec<f64>) = fixes
790 .iter()
791 .map(|fix| {
792 let (lat, lon) = position(fix.latitude, fix.longitude, &mut counts);
793 (lat.unwrap_or(f64::NAN), lon.unwrap_or(f64::NAN))
794 })
795 .unzip();
796 let column = |k: usize, scale: f64| -> Vec<f64> { rows.iter().map(|r| r[k] * scale).collect() };
797 let channels = vec![
798 channel(names[1], Quantity::Latitude, &time_s, latitudes),
799 channel(names[2], Quantity::Longitude, &time_s, longitudes),
800 channel(names[3], Quantity::GnssAltitude, &time_s, column(0, FOOT_M)),
801 channel(names[4], Quantity::Unrecognized, &time_s, column(1, 1.0)),
802 channel(names[5], Quantity::Number, &time_s, column(2, 1.0)),
803 channel(names[6], Quantity::Number, &time_s, column(3, 1.0)),
804 channel(names[7], Quantity::Number, &time_s, column(4, 1.0)),
805 channel(names[8], Quantity::Unrecognized, &time_s, column(5, 1.0)),
806 channel(names[9], Quantity::Angle, &time_s, column(6, 1.0)),
807 ];
808 let mut notes = Notes::default();
809 notes.push(NO_BAROMETER.to_owned());
810 for note in clock_notes(&time_s, zero, &counts, names[0]) {
811 notes.push(note);
812 }
813 let at_zero = rows.iter().filter(|r| r[0] == 0.0).count();
814 for note in units_notes(names[3], names[4], false, at_zero) {
815 notes.push(note);
816 }
817 notes.push(
818 "the software writes every record of its `.rtx` file but the last: the `.rtx` holds one \
819 more fix"
820 .to_owned(),
821 );
822 Ok(log(format, fixes.len(), channels, notes.into_vec()))
823}
824
825#[cfg(test)]
826mod tests {
827 use super::*;
828
829 #[test]
830 fn the_inverse_undoes_the_multiplier() {
831 assert_eq!(MULTIPLIER * INVERSE % MODULUS, 1);
832 assert_eq!(MULTIPLIER, 3u64.pow(18));
833 for value in [0u16, 1, 2, 255, 22_350, 65_535] {
834 assert_eq!(word(slot(value)), Some(value));
835 }
836 assert_eq!(word(slot(2) + 1), None);
838 assert_eq!(word(-1), None);
839 assert_eq!(word(1_000_000_000), None);
840 }
841
842 #[test]
843 fn times_of_day_are_read_strictly() {
844 assert_eq!(time_of_day(170_903), Some(61_743.0));
845 assert_eq!(time_of_day(0), Some(0.0));
846 assert_eq!(time_of_day(235_960), Some(86_400.0));
847 assert_eq!(time_of_day(240_000), None);
848 assert_eq!(time_of_day(126_000), None);
849 assert_eq!(time_of_day(120_061), None);
850 assert_eq!(time_of_day(4_278_331_365), None);
851 }
852
853 #[test]
854 fn a_coordinate_takes_its_leading_one_and_hemisphere() {
855 let north = coordinate(2755, 193_418, b'N', *b"NS", 90.0).unwrap_or(f64::NAN);
856 assert!((north - (27.0 + 55.934_18 / 60.0)).abs() < 1e-12);
857 let west = coordinate(8042, 100_589, b'W', *b"EW", 180.0).unwrap_or(f64::NAN);
858 assert!((west + (80.0 + 42.005_89 / 60.0)).abs() < 1e-12);
859 assert_eq!(coordinate(2760, 100_000, b'N', *b"NS", 90.0), None);
860 assert_eq!(coordinate(2755, 99_999, b'N', *b"NS", 90.0), None);
861 assert_eq!(coordinate(2755, 200_000, b'N', *b"NS", 90.0), None);
862 assert_eq!(coordinate(2755, 100_000, b'X', *b"NS", 90.0), None);
863 assert_eq!(coordinate(9100, 100_000, b'N', *b"NS", 90.0), None);
864 }
865}