motion_lib/
lib.rs

1mod asm;
2mod disasm;
3#[allow(clippy::all)]
4pub mod mlist;
5
6use mlist::MList;
7use std::fs::{read, File};
8use std::io::{prelude::*, Cursor, Error};
9use std::path::Path;
10
11pub use diff;
12pub use hash40;
13
14/// Attempts to read a [MList] from the given reader (requires [Seek]).
15/// The reader should be positioned at the start of the filetype.
16/// Returns a [MList] if successful, otherwise an [Error].
17pub fn read_stream<R: Read + Seek>(reader: &mut R) -> Result<MList, Error> {
18    disasm::disassemble(reader)
19}
20
21/// Attempts to write a [MList] into the given writer (requires [Seek]).
22/// Returns nothing if successful, otherwise an [Error].
23pub fn write_stream<W: Write + Seek>(writer: &mut W, mlist: &MList) -> Result<(), Error> {
24    asm::assemble(writer, mlist)
25}
26
27/// Attempts to read a [MList] from a file path
28pub fn open<P: AsRef<Path>>(file: P) -> Result<MList, Error> {
29    disasm::disassemble(&mut Cursor::new(read(file)?))
30}
31
32/// Attempts to write a [MList] to a file path
33pub fn save<P: AsRef<Path>>(path: P, mlist: &MList) -> Result<(), Error> {
34    let mut file = File::create(path)?;
35    let mut cursor = Cursor::new(Vec::<u8>::new());
36    asm::assemble(&mut cursor, mlist)?;
37    file.write_all(&cursor.into_inner())?;
38    Ok(())
39}