epoch/
epoch.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use serde::{Deserialize, Serialize};

/// Wrapper for serializing and deserializing the epoch.json file. For more context, see
/// [RFC-0071](https://fuchsia.dev/fuchsia-src/contribute/governance/rfcs/0071_ota_backstop).
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(tag = "version", deny_unknown_fields)]
#[allow(missing_docs)]
pub enum EpochFile {
    #[serde(rename = "1")]
    Version1 { epoch: u64 },
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use serde_json::json;

    proptest! {
        #[test]
        fn test_json_serialize_roundtrip(epoch: u64) {
            // Generate json and show that it successfully deserializes into the wrapper object.
            let starting_json_value =
            json!({
                "version": "1",
                "epoch": epoch
            });

            let deserialized_object: EpochFile =
                serde_json::from_value(starting_json_value.clone())
                    .expect("json to deserialize");
            assert_eq!(deserialized_object, EpochFile::Version1 { epoch });

            // Serialize back into serde_json::Value object & show we get same json we started with.
            // Note: even though serialize generally means "convert to string", in this case we're
            // serializing to a serde_json::Value to ignore ordering when we check equality.
            let final_json_value =
                serde_json::to_value(&deserialized_object)
                    .expect("serialize to value");
            assert_eq!(final_json_value, starting_json_value);
        }
    }
}