Skip to content
1        10        20        30        40        50        60        701use std::fs;2use std::io;3use std::path::Path;45pub fn read_document(path: &Path) -> io::Result<String> {6    fs::read_to_string(path)7}89pub fn write_document(path: &Path, contents: &str) -> io::Result<()> {10    fs::write(path, contents)11}1213#[cfg(test)]14mod tests {15    use std::fs;16    use std::time::{SystemTime, UNIX_EPOCH};1718    use super::{read_document, write_document};1920    #[test]21    fn writes_and_reads_utf8_markdown() {22        let timestamp = SystemTime::now()23            .duration_since(UNIX_EPOCH)24            .expect("system clock should be after the Unix epoch")25            .as_nanos();26        let path = std::env::temp_dir().join(format!(27            "markdown-editor-{}-{timestamp}.md",28            std::process::id()29        ));30        let contents = "# Café\n\nA **small** document.\n";3132        write_document(&path, contents).expect("document should save");33        let loaded = read_document(&path).expect("document should open");34        fs::remove_file(path).expect("test document should be removed");3536        assert_eq!(loaded, contents);37    }38}