singlefile_formats/data/
toml_serde.rs1#![cfg_attr(docsrs, doc(cfg(feature = "toml-serde")))]
2#![cfg(feature = "toml-serde")]
3
4pub extern crate toml as original;
7
8use serde::ser::Serialize;
9use serde::de::DeserializeOwned;
10use singlefile::{FileFormat, FileFormatUtf8};
11use thiserror::Error;
12
13use std::io::{Read, Write};
14
15#[derive(Debug, Error)]
17pub enum TomlError {
18 #[error(transparent)]
20 IoError(#[from] std::io::Error),
21 #[error(transparent)]
23 SerializeError(#[from] toml::ser::Error),
24 #[error(transparent)]
26 DeserializeError(#[from] toml::de::Error)
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
34pub struct Toml<const PRETTY: bool = true>;
35
36impl<T, const PRETTY: bool> FileFormat<T> for Toml<PRETTY>
38where T: Serialize + DeserializeOwned {
39 type FormatError = TomlError;
40
41 fn from_reader<R: Read>(&self, mut reader: R) -> Result<T, Self::FormatError> {
42 let mut buf = String::new();
43 reader.read_to_string(&mut buf)?;
44 toml::de::from_str(&buf).map_err(From::from)
45 }
46
47 #[inline]
48 fn from_reader_buffered<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
49 self.from_reader(reader)
51 }
52
53 fn to_writer<W: Write>(&self, mut writer: W, value: &T) -> Result<(), Self::FormatError> {
54 let buf = self.to_buffer(value)?;
55 writer.write_all(&buf).map_err(From::from)
56 }
57
58 #[inline]
59 fn to_writer_buffered<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
60 self.to_writer(writer, value)
62 }
63
64 #[inline]
65 fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
66 self.to_string_buffer(value).map(String::into_bytes)
67 }
68}
69
70impl<T, const PRETTY: bool> FileFormatUtf8<T> for Toml<PRETTY>
71where T: Serialize + DeserializeOwned {
72 fn from_string_buffer(&self, buf: &str) -> Result<T, Self::FormatError> {
73 Ok(toml::de::from_str(buf)?)
74 }
75
76 fn to_string_buffer(&self, value: &T) -> Result<String, Self::FormatError> {
77 Ok(match PRETTY {
78 true => toml::ser::to_string_pretty(value),
79 false => toml::ser::to_string(value)
80 }?)
81 }
82}
83
84pub type PrettyToml = Toml<true>;
86pub type RegularToml = Toml<false>;
88
89#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
92#[cfg(feature = "compression")]
93pub type CompressedToml<C, const PRETTY: bool = false> = crate::compression::Compressed<C, Toml<PRETTY>>;