tink_core/keyset/
binary_io.rs1use crate::{utils::wrap_err, TinkError};
20use std::io::{Read, Write};
21use tink_proto::prost;
22
23pub struct BinaryReader<T: Read> {
25 r: T,
26}
27
28impl<T: Read> BinaryReader<T> {
29 pub fn new(r: T) -> Self {
31 BinaryReader { r }
32 }
33}
34
35impl<T: Read> super::Reader for BinaryReader<T> {
36 fn read(&mut self) -> Result<tink_proto::Keyset, TinkError> {
39 read::<tink_proto::Keyset>(&mut self.r)
40 }
41
42 fn read_encrypted(&mut self) -> Result<tink_proto::EncryptedKeyset, TinkError> {
45 read::<tink_proto::EncryptedKeyset>(&mut self.r)
46 }
47}
48
49fn read<T>(r: &mut dyn Read) -> Result<T, TinkError>
50where
51 T: prost::Message + std::default::Default,
52{
53 let mut data = vec![];
54 r.read_to_end(&mut data)
55 .map_err(|e| wrap_err("read failed", e))?;
56 match T::decode(data.as_ref()) {
57 Ok(msg) => Ok(msg),
58 Err(e) => Err(wrap_err("decode failed", e)),
59 }
60}
61
62pub struct BinaryWriter<T: Write> {
64 w: T,
65}
66
67impl<T: Write> BinaryWriter<T> {
68 pub fn new(w: T) -> Self {
70 BinaryWriter { w }
71 }
72}
73
74impl<T: Write> super::Writer for BinaryWriter<T> {
75 fn write(&mut self, keyset: &tink_proto::Keyset) -> Result<(), TinkError> {
77 write(&mut self.w, keyset)
78 }
79
80 fn write_encrypted(&mut self, keyset: &tink_proto::EncryptedKeyset) -> Result<(), TinkError> {
82 write(&mut self.w, keyset)
83 }
84}
85
86fn write<T>(w: &mut dyn Write, msg: &T) -> Result<(), TinkError>
87where
88 T: prost::Message,
89{
90 let mut data = vec![];
91 match msg.encode(&mut data) {
92 Ok(()) => Ok(()),
93 Err(e) => Err(wrap_err("encode failed", e)),
94 }?;
95 match w.write(&data) {
96 Ok(_size) => Ok(()),
97 Err(e) => Err(wrap_err("write failed", e)),
98 }
99}