use std::io::{Read, Write};
use std::path::Path;
use crate::io::{coerce, IoError};
use crate::model::Ocel;
pub fn read_str(s: &str) -> Result<Ocel, IoError> {
let mut ocel: Ocel = serde_json::from_str(s)?;
coerce::apply_declared_types(&mut ocel);
Ok(ocel)
}
pub fn read_reader<R: Read>(reader: R) -> Result<Ocel, IoError> {
let mut ocel: Ocel = serde_json::from_reader(reader)?;
coerce::apply_declared_types(&mut ocel);
Ok(ocel)
}
pub fn read_path<P: AsRef<Path>>(path: P) -> Result<Ocel, IoError> {
let file = std::fs::File::open(path)?;
read_reader(std::io::BufReader::new(file))
}
pub fn write_string(ocel: &Ocel) -> Result<String, IoError> {
serde_json::to_string_pretty(ocel).map_err(IoError::from)
}
pub fn write_writer<W: Write>(ocel: &Ocel, writer: W) -> Result<(), IoError> {
serde_json::to_writer_pretty(writer, ocel)?;
Ok(())
}
pub fn write_path<P: AsRef<Path>>(ocel: &Ocel, path: P) -> Result<(), IoError> {
let file = std::fs::File::create(path)?;
write_writer(ocel, std::io::BufWriter::new(file))
}