#![cfg_attr(docsrs, doc(cfg(feature = "ron-serde")))]
#![cfg(feature = "ron-serde")]
pub extern crate ron as original;
use ron::options::Options;
use ron::ser::PrettyConfig;
use serde::ser::Serialize;
use serde::de::DeserializeOwned;
use singlefile::{FileFormat, FileFormatUtf8};
use std::io::{Read, Write};
pub type RonError = ron::error::Error;
#[derive(Debug, Clone, Default)]
pub struct Ron {
pub options: Options
}
impl<T> FileFormat<T> for Ron
where T: Serialize + DeserializeOwned {
type FormatError = RonError;
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
self.options.from_reader(reader).map_err(Into::into)
}
fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
self.options.to_io_writer(writer, value)
}
fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError> {
self.options.from_bytes(buf).map_err(Into::into)
}
fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
self.to_string_buffer(value).map(String::into_bytes)
}
}
impl<T> FileFormatUtf8<T> for Ron
where T: Serialize + DeserializeOwned {
fn from_string_buffer(&self, buf: &str) -> Result<T, Self::FormatError> {
self.options.from_str(buf).map_err(Into::into)
}
fn to_string_buffer(&self, value: &T) -> Result<String, Self::FormatError> {
self.options.to_string(value)
}
}
#[derive(Debug, Clone, Default)]
pub struct RonPretty {
pub options: Options,
pub config: PrettyConfig
}
impl<T> FileFormat<T> for RonPretty
where T: Serialize + DeserializeOwned {
type FormatError = RonError;
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
self.options.from_reader(reader).map_err(Into::into)
}
fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
self.options.to_io_writer_pretty(writer, value, self.config.clone())
}
fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError> {
self.options.from_bytes(buf).map_err(Into::into)
}
fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
self.to_string_buffer(value).map(String::into_bytes)
}
}
impl<T> FileFormatUtf8<T> for RonPretty
where T: Serialize + DeserializeOwned {
fn from_string_buffer(&self, buf: &str) -> Result<T, Self::FormatError> {
self.options.from_str(buf).map_err(Into::into)
}
fn to_string_buffer(&self, value: &T) -> Result<String, Self::FormatError> {
self.options.to_string_pretty(value, self.config.clone())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
#[cfg(feature = "compression")]
pub type CompressedRon<C> = crate::compression::Compressed<C, Ron>;
#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
#[cfg(feature = "compression")]
pub type CompressedRonPretty<C> = crate::compression::Compressed<C, RonPretty>;