use std::fs::File;
use std::io;
use super::zone::MAX_LEN;
use super::zone::Zone;
use crate::formats::pinned_dir::read_zone_stream;
use crate::formats::pinned_dir::write_zone_stream;
pub(super) type Mark = Zone;
pub(super) const SUPPORTED: bool = true;
pub(super) fn read(file: &File) -> io::Result<Option<Mark>> {
Ok(read_zone_stream(file, MAX_LEN)?.map(|content| Zone::of_stream(&content)))
}
pub(super) fn write_file(mark: Mark, file: &File) -> io::Result<()> {
write_zone_stream(file, mark.render().as_bytes())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_unmarked_file_reads_as_none() {
let file = tempfile::tempfile().unwrap();
assert_eq!(read(&file).unwrap(), None);
}
#[test]
fn a_mark_written_through_a_handle_reads_back() {
let file = tempfile::tempfile().unwrap();
write_zone_stream(&file, b"[ZoneTransfer]\r\nZoneId=3\r\n").unwrap();
let mark = read(&file).unwrap().unwrap();
let target = tempfile::tempfile().unwrap();
write_file(mark, &target).unwrap();
assert_eq!(read(&target).unwrap(), Some(mark));
}
}