1#![doc = include_str!("./.crate-docs.md")]
2#![forbid(unsafe_code)]
3#![warn(
4 clippy::cargo,
5 missing_docs,
6 clippy::pedantic,
8 future_incompatible,
9 rust_2018_idioms,
10)]
11#![allow(
12 clippy::missing_errors_doc, clippy::option_if_let_else,
14)]
15
16use std::io::{Read, Write};
17
18pub use pot;
19use serde::{de::DeserializeOwned, Deserialize, Serialize};
20pub use transmog;
21use transmog::{BorrowedDeserializer, Format, OwnedDeserializer};
22
23#[derive(Clone, Default)]
25pub struct Pot(pot::Config);
26
27impl From<pot::Config> for Pot {
28 fn from(config: pot::Config) -> Self {
29 Self(config)
30 }
31}
32
33impl<'a, T> Format<'a, T> for Pot
34where
35 T: Serialize,
36{
37 type Error = pot::Error;
38
39 fn serialize(&self, value: &T) -> Result<Vec<u8>, Self::Error> {
40 self.0.serialize(value)
41 }
42
43 fn serialize_into<W: Write>(&self, value: &T, writer: W) -> Result<(), Self::Error> {
44 self.0.serialize_into(value, writer)
45 }
46}
47
48impl<'a, T> BorrowedDeserializer<'a, T> for Pot
49where
50 T: Serialize + Deserialize<'a>,
51{
52 fn deserialize_borrowed(&self, data: &'a [u8]) -> Result<T, Self::Error> {
53 self.0.deserialize(data)
54 }
55}
56
57impl<T> OwnedDeserializer<T> for Pot
58where
59 T: Serialize + DeserializeOwned,
60{
61 fn deserialize_owned(&self, data: &[u8]) -> Result<T, Self::Error> {
62 self.0.deserialize(data)
63 }
64 fn deserialize_from<R: Read>(&self, reader: R) -> Result<T, Self::Error> {
65 self.0.deserialize_from(reader)
66 }
67}
68
69#[test]
70fn format_tests() {
71 transmog::test_util::test_format(&Pot::default());
72 transmog::test_util::test_format(&Pot::from(pot::Config::default().allocation_budget(64)));
73}
74
75#[test]
76fn borrowed_deserialization() {
77 use std::borrow::Cow;
78 #[derive(Serialize, Deserialize)]
79 struct Test<'a> {
80 #[serde(borrow)]
81 value: Cow<'a, str>,
82 }
83 let pot = Pot::default();
84 let value: Test<'static> = Test {
85 value: Cow::Owned(String::from("hello")),
86 };
87 let serialized = pot.serialize(&value).unwrap();
88 let pot = Pot::default();
89 let deserialized: Test<'_> = pot.deserialize_borrowed(&serialized).unwrap();
90 assert_eq!(deserialized.value, "hello");
91 assert!(matches!(deserialized.value, Cow::Borrowed(_)));
92}