epoch/
epoch.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use serde::{Deserialize, Serialize};
6
7/// Wrapper for serializing and deserializing the epoch.json file. For more context, see
8/// [RFC-0071](https://fuchsia.dev/fuchsia-src/contribute/governance/rfcs/0071_ota_backstop).
9#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
10#[serde(tag = "version", deny_unknown_fields)]
11#[allow(missing_docs)]
12pub enum EpochFile {
13    #[serde(rename = "1")]
14    Version1 { epoch: u64 },
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use proptest::prelude::*;
21    use serde_json::json;
22
23    proptest! {
24        #[test]
25        fn test_json_serialize_roundtrip(epoch: u64) {
26            // Generate json and show that it successfully deserializes into the wrapper object.
27            let starting_json_value =
28            json!({
29                "version": "1",
30                "epoch": epoch
31            });
32
33            let deserialized_object: EpochFile =
34                serde_json::from_value(starting_json_value.clone())
35                    .expect("json to deserialize");
36            assert_eq!(deserialized_object, EpochFile::Version1 { epoch });
37
38            // Serialize back into serde_json::Value object & show we get same json we started with.
39            // Note: even though serialize generally means "convert to string", in this case we're
40            // serializing to a serde_json::Value to ignore ordering when we check equality.
41            let final_json_value =
42                serde_json::to_value(&deserialized_object)
43                    .expect("serialize to value");
44            assert_eq!(final_json_value, starting_json_value);
45        }
46    }
47}