1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
//! How to interpret the contents of files.
pub mod default_formats;
pub use self::default_formats::PlainBytes;
pub use self::default_formats::PlainUtf8;
use std::io::{Cursor, BufReader, BufWriter, Read, Write};
/// A trait that describes how a file's contents should be interpreted.
///
/// Usually, you will want to implement a simple wrapper over your file format's
/// `to_writer` and `from_reader` functions, using your favorite serialization framework.
///
/// # Example
/// ```no_run
/// # use serde::ser::Serialize;
/// # use serde::de::DeserializeOwned;
/// # use singlefile::FileFormat;
/// # use singlefile_formats::json_serde::serde_json;
/// # use std::io::{Read, Write};
/// struct Json;
///
/// impl<T> FileFormat<T> for Json
/// where T: Serialize + DeserializeOwned {
/// type FormatError = serde_json::Error;
///
/// fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
/// serde_json::to_writer_pretty(writer, value).map_err(From::from)
/// }
///
/// fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
/// serde_json::from_reader(reader).map_err(From::from)
/// }
/// }
/// ```
pub trait FileFormat<T> {
/// The type of error to return from `to_writer` and `from_reader`.
type FormatError: std::error::Error;
/// Deserialize a value from a `Read` stream.
///
/// If you are reading directly from a [`File`][std::fs::File], you should consider
/// using [`from_reader_buffered`][FileFormat::from_reader_buffered] instead.
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError>;
/// Identical to [`FileFormat::from_reader`], however the provided reader is buffered with [`BufReader`].
///
/// You should override this function if your file format reads
/// to a buffer internally in order to avoid double-buffering.
#[inline]
fn from_reader_buffered<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
self.from_reader(BufReader::new(reader))
}
/// Deserialize a value from a byte vec.
#[inline]
fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError> {
self.from_reader(buf)
}
/// Serialize a value into a `Write` stream.
///
/// If you are writing directly to a [`File`][std::fs::File], you should consider
/// using [`to_writer_buffered`][FileFormat::to_writer_buffered] instead.
fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError>;
/// Identical to [`FileFormat::to_writer`], however the provided writer is buffered with [`BufWriter`].
///
/// You should override this function if your file format writes
/// to a buffer internally in order to avoid double-buffering.
#[inline]
fn to_writer_buffered<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
self.to_writer(BufWriter::new(writer), value)
}
/// Serialize a value into a byte vec.
fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
let mut buf = Cursor::new(Vec::new());
self.to_writer(&mut buf, value)?;
Ok(buf.into_inner())
}
}
macro_rules! impl_file_format_delegate {
(<$Format:ident> $Type:ty) => (
impl<T, $Format: FileFormat<T>> FileFormat<T> for $Type {
type FormatError = <$Format as FileFormat<T>>::FormatError;
#[inline]
fn from_reader_buffered<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
$Format::from_reader_buffered(self, reader)
}
#[inline]
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
$Format::from_reader(self, reader)
}
#[inline]
fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
$Format::to_writer(self, writer, value)
}
#[inline]
fn to_writer_buffered<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
$Format::to_writer_buffered(self, writer, value)
}
#[inline]
fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> {
$Format::to_buffer(self, value)
}
}
);
}
impl_file_format_delegate!(<Format> &Format);
impl_file_format_delegate!(<Format> std::boxed::Box<Format>);
impl_file_format_delegate!(<Format> std::rc::Rc<Format>);
impl_file_format_delegate!(<Format> std::sync::Arc<Format>);