Skip to main content

Module binary_format

Module binary_format 

Source
Expand description

Custom binary serialisation format for scientific data.

This module provides three layers of binary I/O:

  1. BinaryWriter / BinaryReader — low-level type-safe primitives (all integers, both float sizes, byte slices, length-prefixed strings, and prefixed f64 arrays). All multi-byte values are stored in little-endian byte order for maximum portability.

  2. ScirsDataFile — a structured scientific container format built on top of the primitives:

    • Fixed 8-byte magic header (SCIRS2DF)
    • u8 version number
    • u32 record count
    • Sequence of named, typed DataRecord entries
  3. DataRecord — the payload variant that a record may hold: Scalar, Vector, Matrix, and Text.

§File layout

┌─────────────────────────────────────────────────────────┐
│ Magic    : [u8; 8]  = b"SCIRS2DF"                       │
│ Version  : u8       = 1                                  │
│ N records: u32 LE                                        │
├─────────────────────────────────────────────────────────┤
│ Record₁                                                  │
│   Name   : u32 LE length-prefix + UTF-8 bytes           │
│   Tag    : u8   (0=Scalar, 1=Vector, 2=Matrix, 3=Text)  │
│   Data   :                                               │
│     Scalar → f64 LE                                      │
│     Vector → u64 LE count + count×f64 LE                │
│     Matrix → u64 rows + u64 cols + rows×cols×f64 LE     │
│     Text   → u32 LE length + UTF-8 bytes                 │
├─────────────────────────────────────────────────────────┤
│ Record₂ …                                                │
└─────────────────────────────────────────────────────────┘

§Examples

use scirs2_io::binary_format::{write_scirs, read_scirs, DataRecord};

let records = vec![
    DataRecord::named("pi",   DataRecord::Scalar(std::f64::consts::PI)),
    DataRecord::named("data", DataRecord::Vector(vec![1.0, 2.0, 3.0])),
];
// write_scirs / read_scirs take &[(name, DataRecord)] and return Vec<(name, DataRecord)>
write_scirs("out.scirs2", &records).unwrap();
let loaded = read_scirs("out.scirs2").unwrap();

Structs§

BinaryReader
Type-safe binary reader backed by a BufReader<File>.
BinaryWriter
Type-safe binary writer backed by a BufWriter<File>.
ScirsDataFile
Structured scientific data file (SCIRS2DF format).

Enums§

DataRecord
The payload carried by a single named record in a ScirsDataFile.

Functions§

read_scirs
Read all named DataRecords from a SCIRS2DF binary file.
write_scirs
Write a sequence of named DataRecords to a SCIRS2DF binary file.