Skip to main content

singlefile_formats/data/
ron_serde.rs

1#![cfg_attr(docsrs, doc(cfg(feature = "ron-serde")))]
2#![cfg(feature = "ron-serde")]
3
4//! Defines a [`FileFormat`] using the RON data format.
5
6pub 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
16/// An error that can occur while using [`Ron`] or [`RonPretty`].
17pub type RonError = ron::error::Error;
18
19/// A [`FileFormat`] corresponding to the JSON data format.
20/// Implemented using the [`ron`] crate, only compatible with [`serde`] types.
21///
22/// This will not pretty-print code when serializing.
23#[derive(Debug, Clone, Default)]
24pub struct Ron {
25  /// The [`Options`] to use when serializing or deserializing.
26  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/// A [`FileFormat`] corresponding to the JSON data format.
62/// Implemented using the [`ron`] crate, only compatible with [`serde`] types.
63///
64/// This will pretty-print code when serializing, using the provided pretty-print config.
65#[derive(Debug, Clone, Default)]
66pub struct RonPretty {
67  /// The [`Options`] to use when serializing or deserializing.
68  pub options: Options,
69  /// The [`PrettyConfig`] to use when serializing.
70  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/// A shortcut type to a [`Compressed`][crate::compression::Compressed] [`Ron`].
106///
107/// This will not pretty-print code when serializing.
108///
109/// Provides a single parameter for compression format.
110#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
111#[cfg(feature = "compression")]
112pub type CompressedRon<C> = crate::compression::Compressed<C, Ron>;
113
114/// A shortcut type to a [`Compressed`][crate::compression::Compressed] [`Ron`].
115///
116/// This will pretty-print code when serializing, so consider using [`CompressedRon`] instead.
117///
118/// Provides a single parameter for compression format.
119#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
120#[cfg(feature = "compression")]
121pub type CompressedRonPretty<C> = crate::compression::Compressed<C, RonPretty>;