#![cfg_attr(docsrs, doc(cfg(feature = "toml-serde")))]
#![cfg(feature = "toml-serde")]
pub extern crate toml as original;
use serde::ser::Serialize;
use serde::de::DeserializeOwned;
use singlefile::{FileFormat, FileFormatUtf8};
use thiserror::Error;
use std::io::{Read, Write};
#[derive(Debug, Error)]
pub enum TomlError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
SerializeError(#[from] toml::ser::Error),
#[error(transparent)]
DeserializeError(#[from] toml::de::Error)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Toml<const PRETTY: bool = true>;
impl<T, const PRETTY: bool> FileFormat<T> for Toml<PRETTY>
where T: Serialize + DeserializeOwned {
type FormatError = TomlError;
fn from_reader<R: Read>(&self, mut reader: R) -> Result<T, Self::FormatError> {
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
toml::de::from_str(&buf).map_err(From::from)
}
#[inline]
fn from_reader_buffered<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
self.from_reader(reader)
}
fn to_writer<W: Write>(&self, mut writer: W, value: &T) -> Result<(), Self::FormatError> {
let buf = self.to_buffer(value)?;
writer.write_all(&buf).map_err(From::from)
}
#[inline]
fn to_writer_buffered<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
self.to_writer(writer, value)
}
#[inline]
fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
self.to_string_buffer(value).map(String::into_bytes)
}
}
impl<T, const PRETTY: bool> FileFormatUtf8<T> for Toml<PRETTY>
where T: Serialize + DeserializeOwned {
fn from_string_buffer(&self, buf: &str) -> Result<T, Self::FormatError> {
Ok(toml::de::from_str(buf)?)
}
fn to_string_buffer(&self, value: &T) -> Result<String, Self::FormatError> {
Ok(match PRETTY {
true => toml::ser::to_string_pretty(value),
false => toml::ser::to_string(value)
}?)
}
}
pub type PrettyToml = Toml<true>;
pub type RegularToml = Toml<false>;
#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
#[cfg(feature = "compression")]
pub type CompressedToml<C, const PRETTY: bool = false> = crate::compression::Compressed<C, Toml<PRETTY>>;