use num_enum::TryFromPrimitive;
#[cfg(feature = "bundles")]
pub mod bundle;
pub mod read;
pub mod write;
pub use read::Reader;
pub use write::Writer;
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromPrimitive)]
pub enum VariableType {
Real = 0, List = 1, Matrix = 2, Equation = 3, String = 4, Program = 5, ProtectedProgram = 6, Picture = 7, GDB = 8, Unknown = 9,
UnknownEquation = 0xa,
NewEquation = 0xb,
Complex = 0xc, ComplexList = 0xd, Undefined = 0xe,
Window = 0xf,
Zoom = 0x10, TableSetup = 0x11, LCD = 0x12,
Backup = 0x13,
AppVar = 0x15, TemporaryProgram = 0x16,
Group = 0x17, }
impl VariableType {
fn has_length_prefix(&self) -> bool {
use VariableType::*;
match self {
Equation | String | GDB | Program | ProtectedProgram | Picture | Window
| TableSetup | AppVar => true,
Real | List | Matrix | Complex | ComplexList => false,
x => unimplemented!("Format for variable type {:?} is unknown", x),
}
}
pub fn file_extension(&self) -> &'static str {
use VariableType::*;
match self {
Real => "8xn",
Complex => "8xc",
List | ComplexList => "8xl",
Matrix => "8xm",
Equation => "8xy",
String => "8xs",
Program | ProtectedProgram => "8xp",
Picture => "8xi",
GDB => "8xd",
Zoom => "8xz",
TableSetup => "8xt",
AppVar => "8xv",
Group => "8xg",
t => todo!("File extension for type {:?} isn't yet known", t),
}
}
}
const MAX_DATA: u16 = u16::MAX - 17;
#[test]
fn round_trip_is_lossless() {
use std::io::{Cursor, Read, Write};
let mut ref_data = [0u8; 256];
for (x, b) in ref_data.iter_mut().enumerate() {
*b = x as u8;
}
let mut file_data = vec![];
{
let mut writer = Writer::new(
Cursor::new(&mut file_data),
VariableType::Program,
"ABC123",
false,
)
.unwrap();
writer.write_all(&ref_data).unwrap();
writer.close().unwrap();
}
let mut reader = Reader::new(&*file_data).unwrap();
assert_eq!(reader.name(), b"ABC123\0\0");
assert!(!reader.is_archived());
let mut read_data = vec![];
reader.read_to_end(&mut read_data).unwrap();
reader.finish().unwrap().expect("checksum should be valid");
assert_eq!(ref_data, &*read_data);
}