polyhorn_cli/test/
snapshot.rs

1//! Types and implementations to write snapshots to disk.
2
3use serde::Serialize;
4use sha1::{Digest, Sha1};
5
6trait Sha1Ext {
7    fn tag(&mut self, prefix: &str, value: Option<&String>);
8}
9
10impl Sha1Ext for Sha1 {
11    fn tag(&mut self, prefix: &str, value: Option<&String>) {
12        if let Some(value) = value {
13            self.update(prefix.as_bytes());
14            self.update(format!("{}", value.len()).as_bytes());
15            self.update(value.as_bytes());
16        }
17    }
18}
19
20/// Represents the metadata of a snapshot.
21#[derive(Clone, Debug, Default, Serialize)]
22pub struct Metadata {
23    /// This is the device type. For example: "iPhone Xs".
24    pub device: Option<String>,
25
26    /// This is the operating system. For example: "iOS" or "Android".
27    pub os: Option<String>,
28
29    /// This is the version of the operating system. For example: "12.0". This
30    /// version does not have to be semver compatible.
31    pub os_version: Option<String>,
32
33    /// This is the appearance of the operating system. For example: "dark" or
34    /// "light".
35    pub os_appearance: Option<String>,
36
37    /// This is the name of the test itself.
38    pub test_name: Option<String>,
39
40    /// This is the name of the snapshot.
41    pub snapshot_name: Option<String>,
42}
43
44impl Metadata {
45    /// Computes a sha-1 digest for this metadata.
46    pub fn digest(&self) -> Digest {
47        let mut sha1 = Sha1::new();
48        sha1.tag("device", self.device.as_ref());
49        sha1.tag("os", self.os.as_ref());
50        sha1.tag("os_version", self.os_version.as_ref());
51        sha1.tag("os_appearance", self.os_appearance.as_ref());
52        sha1.tag("test_name", self.test_name.as_ref());
53        sha1.tag("snapshot_name", self.snapshot_name.as_ref());
54        sha1.digest()
55    }
56}