tink_core/keyset/
json_io.rs

1// Copyright 2020 The Tink-Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17//! JSON I/O for keysets (requires activation of the `json` feature).
18
19use crate::{utils::wrap_err, TinkError};
20use serde::Deserialize;
21use std::io::{Read, Write};
22
23/// `JsonReader` deserializes a keyset from JSON format.
24#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
25pub struct JsonReader<T: Read> {
26    r: T,
27}
28
29impl<T: Read> JsonReader<T> {
30    /// Return a new [`JsonReader`] that will read from `r`.
31    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
32    pub fn new(r: T) -> Self {
33        JsonReader { r }
34    }
35}
36
37impl<T: Read> super::Reader for JsonReader<T> {
38    /// Return a (cleartext) [`Keyset`](tink_proto::Keyset) object from the underlying
39    /// [`std::io::Read`].
40    fn read(&mut self) -> Result<tink_proto::Keyset, TinkError> {
41        let mut de = serde_json::Deserializer::from_reader(&mut self.r);
42        tink_proto::Keyset::deserialize(&mut de).map_err(|e| wrap_err("failed to parse", e))
43    }
44
45    /// Return an [`EncryptedKeyset`](tink_proto::EncryptedKeyset) object from the underlying
46    /// [`std::io::Read`].
47    fn read_encrypted(&mut self) -> Result<tink_proto::EncryptedKeyset, TinkError> {
48        let mut de = serde_json::Deserializer::from_reader(&mut self.r);
49        tink_proto::EncryptedKeyset::deserialize(&mut de)
50            .map_err(|e| wrap_err("failed to parse", e))
51    }
52}
53
54/// `JsonWriter` serializes a keyset into JSON format.
55#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
56pub struct JsonWriter<T: Write> {
57    w: T,
58}
59
60impl<T: Write> JsonWriter<T> {
61    /// Return a new [`JsonWriter`] that will write to `w`.
62    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
63    pub fn new(w: T) -> Self {
64        JsonWriter { w }
65    }
66}
67
68impl<T: Write> super::Writer for JsonWriter<T> {
69    /// Write the keyset to the underlying [`std::io::Write`].
70    fn write(&mut self, keyset: &tink_proto::Keyset) -> Result<(), TinkError> {
71        serde_json::to_writer_pretty(&mut self.w, keyset)
72            .map_err(|e| wrap_err("failed to encode", e))
73    }
74
75    /// Write the encrypted keyset to the underlying [`std::io::Write`].
76    fn write_encrypted(&mut self, keyset: &tink_proto::EncryptedKeyset) -> Result<(), TinkError> {
77        serde_json::to_writer_pretty(&mut self.w, keyset)
78            .map_err(|e| wrap_err("failed to encode", e))
79    }
80}