singlefile_formats/data/
ron_serde.rs1#![cfg_attr(docsrs, doc(cfg(feature = "ron-serde")))]
2#![cfg(feature = "ron-serde")]
3
4pub extern crate ron as original;
7
8use ron::options::Options;
9use ron::ser::PrettyConfig;
10use serde::ser::Serialize;
11use serde::de::DeserializeOwned;
12use singlefile::{FileFormat, FileFormatUtf8};
13
14use std::io::{Read, Write};
15
16pub type RonError = ron::error::Error;
18
19#[derive(Debug, Clone, Default)]
24pub struct Ron {
25 pub options: Options
27}
28
29impl<T> FileFormat<T> for Ron
30where T: Serialize + DeserializeOwned {
31 type FormatError = RonError;
32
33 fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
34 self.options.from_reader(reader).map_err(Into::into)
35 }
36
37 fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
38 self.options.to_io_writer(writer, value)
39 }
40
41 fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError> {
42 self.options.from_bytes(buf).map_err(Into::into)
43 }
44
45 fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
46 self.to_string_buffer(value).map(String::into_bytes)
47 }
48}
49
50impl<T> FileFormatUtf8<T> for Ron
51where T: Serialize + DeserializeOwned {
52 fn from_string_buffer(&self, buf: &str) -> Result<T, Self::FormatError> {
53 self.options.from_str(buf).map_err(Into::into)
54 }
55
56 fn to_string_buffer(&self, value: &T) -> Result<String, Self::FormatError> {
57 self.options.to_string(value)
58 }
59}
60
61#[derive(Debug, Clone, Default)]
66pub struct RonPretty {
67 pub options: Options,
69 pub config: PrettyConfig
71}
72
73impl<T> FileFormat<T> for RonPretty
74where T: Serialize + DeserializeOwned {
75 type FormatError = RonError;
76
77 fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
78 self.options.from_reader(reader).map_err(Into::into)
79 }
80
81 fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
82 self.options.to_io_writer_pretty(writer, value, self.config.clone())
83 }
84
85 fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError> {
86 self.options.from_bytes(buf).map_err(Into::into)
87 }
88
89 fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
90 self.to_string_buffer(value).map(String::into_bytes)
91 }
92}
93
94impl<T> FileFormatUtf8<T> for RonPretty
95where T: Serialize + DeserializeOwned {
96 fn from_string_buffer(&self, buf: &str) -> Result<T, Self::FormatError> {
97 self.options.from_str(buf).map_err(Into::into)
98 }
99
100 fn to_string_buffer(&self, value: &T) -> Result<String, Self::FormatError> {
101 self.options.to_string_pretty(value, self.config.clone())
102 }
103}
104
105#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
111#[cfg(feature = "compression")]
112pub type CompressedRon<C> = crate::compression::Compressed<C, Ron>;
113
114#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
120#[cfg(feature = "compression")]
121pub type CompressedRonPretty<C> = crate::compression::Compressed<C, RonPretty>;