1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-23 05:07:46 +00:00
physis/src/exl.rs

124 lines
3 KiB
Rust
Raw Normal View History

2022-08-16 11:52:07 -04:00
use crate::gamedata::MemoryBuffer;
2022-07-19 19:29:41 -04:00
use std::collections::HashMap;
use std::io::{BufRead, BufReader, BufWriter, Cursor, Write};
2022-07-19 19:29:41 -04:00
/// Represents an Excel List.
pub struct EXL {
/// The version of the list.
pub version: i32,
/// The entries of the list.
pub entries: HashMap<String, i32>,
}
impl EXL {
/// Initializes `EXL` from an existing list.
2022-08-16 11:52:07 -04:00
pub fn from_existing(buffer: &MemoryBuffer) -> Option<EXL> {
2022-07-19 19:29:41 -04:00
let mut exl = Self {
version: 0,
entries: HashMap::new(),
};
2022-07-21 19:58:58 -04:00
let cursor = Cursor::new(buffer);
let reader = BufReader::new(cursor);
2022-07-19 19:29:41 -04:00
for (_, line) in reader.lines().enumerate() {
// now parse the line!
let unwrap = line.unwrap();
let (name, value) = unwrap.split_once(',').unwrap();
let parsed_value: i32 = value.parse().unwrap();
if name == "EXLT" {
exl.version = parsed_value;
} else {
exl.entries.insert(name.parse().unwrap(), parsed_value);
}
}
Some(exl)
}
pub fn write_to_buffer(&self) -> Option<MemoryBuffer> {
let mut buffer = MemoryBuffer::new();
{
let cursor = Cursor::new(&mut buffer);
let mut writer = BufWriter::new(cursor);
writer.write(format!("EXLT,{}", self.version).as_ref());
for entry in &self.entries {
writer.write(format!("\n{},{}", entry.0, entry.1).as_ref());
}
}
Some(buffer)
}
2022-07-19 19:29:41 -04:00
/// Checks whether or not the list contains a key.
///
/// # Example
///
/// ```
/// # use std::fs::read;
/// # use std::path::PathBuf;
2022-07-19 19:29:41 -04:00
/// # use physis::exl::EXL;
/// # let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
/// # d.push("resources/tests");
/// # d.push("test.exl");
/// # let exl_file = read(d).unwrap();
/// let exl = EXL::from_existing(&exl_file).unwrap();
/// exl.contains("Foo");
2022-07-19 19:29:41 -04:00
/// ```
pub fn contains(&self, key: &str) -> bool {
self.entries.contains_key(key)
}
}
#[cfg(test)]
mod tests {
2022-08-16 11:52:07 -04:00
use super::*;
2022-07-27 20:58:51 -04:00
use std::fs::read;
2022-07-19 19:29:41 -04:00
use std::path::PathBuf;
fn common_setup() -> EXL {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("resources/tests");
d.push("test.exl");
2022-07-27 20:58:51 -04:00
EXL::from_existing(&read(d).unwrap()).unwrap()
2022-07-19 19:29:41 -04:00
}
#[test]
fn version_parsing() {
let exl = common_setup();
assert_eq!(exl.version, 2);
}
#[test]
fn contains() {
let exl = common_setup();
assert!(exl.contains("Foo"));
// should be case-sensitive
assert!(!exl.contains("foo"));
}
#[test]
fn test_write() {
let existing_exl = common_setup();
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("resources/tests");
d.push("test.exl");
let exl = read(d).unwrap();
assert_eq!(existing_exl.write_to_buffer().unwrap(), exl);
}
2022-08-16 11:52:07 -04:00
}