singlefile_formats/data/
cbor_serde.rs1#![cfg_attr(docsrs, doc(cfg(feature = "cbor-serde")))]
2#![cfg(feature = "cbor-serde")]
3
4pub extern crate ciborium as original;
7
8use serde::ser::Serialize;
9use serde::de::DeserializeOwned;
10use singlefile::FileFormat;
11use thiserror::Error;
12
13use std::io::{Read, Write};
14
15#[derive(Debug, Error)]
17pub enum CborError {
18 #[error(transparent)]
20 SerializeError(#[from] ciborium::ser::Error<std::io::Error>),
21 #[error(transparent)]
23 DeserializeError(#[from] ciborium::de::Error<std::io::Error>)
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
29pub struct Cbor;
30
31impl<T> FileFormat<T> for Cbor
32where T: Serialize + DeserializeOwned {
33 type FormatError = CborError;
34
35 fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
36 ciborium::de::from_reader(reader).map_err(From::from)
37 }
38
39 fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
40 ciborium::ser::into_writer(value, writer).map_err(From::from)
41 }
42}
43
44#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
47#[cfg(feature = "compression")]
48pub type CompressedCbor<C> = crate::compression::Compressed<C, Cbor>;