mirror of
https://github.com/redstrate/Physis.git
synced 2025-04-26 14:17:45 +00:00
Add write support and relevant test for EXL files
This commit is contained in:
parent
1fd7cdf07d
commit
f3e51048b2
1 changed files with 31 additions and 1 deletions
32
src/exl.rs
32
src/exl.rs
|
@ -1,6 +1,6 @@
|
|||
use crate::gamedata::MemoryBuffer;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{BufRead, BufReader, Cursor};
|
||||
use std::io::{BufRead, BufReader, BufWriter, Cursor, Write};
|
||||
|
||||
/// Represents an Excel List.
|
||||
pub struct EXL {
|
||||
|
@ -40,6 +40,23 @@ impl EXL {
|
|||
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)
|
||||
}
|
||||
|
||||
/// Checks whether or not the list contains a key.
|
||||
///
|
||||
/// # Example
|
||||
|
@ -90,4 +107,17 @@ mod tests {
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue