blotter.rs/src/lib.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2022-10-28 21:58:07 -05:00
mod convert;
2022-10-28 22:54:14 -05:00
pub mod custom_data;
2022-10-28 21:25:04 -05:00
pub mod error;
pub(crate) mod io;
pub mod v5;
2022-10-28 21:49:37 -05:00
pub mod v6;
2021-10-23 21:54:15 -05:00
2022-10-28 21:25:04 -05:00
use io::read_u8;
use std::io::{Read, Write};
2021-10-23 21:54:15 -05:00
2022-10-28 21:25:04 -05:00
use crate::error::Error;
use crate::io::read_magic;
2021-10-23 21:54:15 -05:00
2022-10-28 22:04:30 -05:00
pub use v6 as latest;
2021-10-23 21:54:15 -05:00
#[derive(Debug)]
2022-10-28 21:25:04 -05:00
pub enum BlotterFile {
V5(v5::BlotterFile),
2022-10-28 21:49:37 -05:00
V6(v6::BlotterFile),
2021-06-28 23:56:19 -05:00
}
2021-10-23 21:54:15 -05:00
impl BlotterFile {
2022-10-28 21:25:04 -05:00
pub fn read<R: Read>(reader: &mut R) -> Result<Self, Error> {
2021-10-23 21:54:15 -05:00
read_magic(reader, b"Logic World save")?;
let save_version = read_u8(reader)?;
2022-10-28 21:25:04 -05:00
match save_version {
2022-10-28 21:49:37 -05:00
v5::SAVE_VERSION => v5::BlotterFile::read_after_save_version(reader).map(Self::V5),
v6::SAVE_VERSION => v6::BlotterFile::read_after_save_version(reader).map(Self::V6),
2022-10-28 21:25:04 -05:00
_ => Err(Error::IncompatibleVersion(save_version)),
2021-10-23 21:54:15 -05:00
}
}
2021-10-23 22:16:40 -05:00
2022-10-28 21:25:04 -05:00
pub fn write<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
match self {
Self::V5(file) => file.write(writer),
2022-10-28 21:49:37 -05:00
Self::V6(file) => file.write(writer),
2021-10-23 22:16:40 -05:00
}
2021-10-23 21:54:15 -05:00
}
2022-10-28 22:04:30 -05:00
/// Convert the file into the latest version.
pub fn migrate(self) -> latest::BlotterFile {
match self {
Self::V5(file) => Self::V6(file.into()).migrate(),
Self::V6(file) => file,
}
}
2021-10-23 22:16:40 -05:00
}