Skip to main content

spatial_narrative/io/
format.rs

1//! Format trait for import/export operations.
2
3use crate::core::Narrative;
4use crate::Result;
5use std::io::{Read, Write};
6
7/// Trait for formats that can import and export narratives.
8///
9/// This trait defines a common interface for reading and writing
10/// narratives in various formats (GeoJSON, CSV, etc.).
11pub trait Format {
12    /// Import a narrative from a reader.
13    ///
14    /// # Errors
15    ///
16    /// Returns an error if the data is malformed or doesn't match
17    /// the expected format.
18    fn import<R: Read>(&self, reader: R) -> Result<Narrative>;
19
20    /// Import a narrative from a string.
21    ///
22    /// This is a convenience method that wraps the string in a reader.
23    fn import_str(&self, data: &str) -> Result<Narrative> {
24        self.import(data.as_bytes())
25    }
26
27    /// Export a narrative to a writer.
28    ///
29    /// # Errors
30    ///
31    /// Returns an error if the write operation fails.
32    fn export<W: Write>(&self, narrative: &Narrative, writer: W) -> Result<()>;
33
34    /// Export a narrative to a string.
35    ///
36    /// This is a convenience method that collects output into a String.
37    fn export_str(&self, narrative: &Narrative) -> Result<String> {
38        let mut buffer = Vec::new();
39        self.export(narrative, &mut buffer)?;
40        Ok(String::from_utf8(buffer).expect("format produced invalid UTF-8"))
41    }
42}