Skip to main content

civ_engine/
io.rs

1//! I/O utilities for simulation state persistence
2
3use std::fs;
4use std::path::Path;
5
6/// Read text file contents
7pub fn read_text(path: impl AsRef<Path>) -> std::io::Result<String> {
8    fs::read_to_string(path)
9}
10
11/// Write text file contents
12pub fn write_text(path: impl AsRef<Path>, contents: &str) -> std::io::Result<()> {
13    fs::write(path, contents)
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use std::io::Write;
20    use tempfile::NamedTempFile;
21
22    #[test]
23    fn test_read_write() {
24        let mut file = NamedTempFile::new().unwrap();
25        file.write_all(b"test content").unwrap();
26        
27        let contents = read_text(file.path()).unwrap();
28        assert_eq!(contents, "test content");
29    }
30}