Skip to main content

vcon_server/
formats.rs

1//! Every on-disk format this build writes, with its version: the one list.
2//!
3//! The page website/content/docs/on-disk-format.md lists each entry, and
4//! tests/formats.rs holds the page to [`FORMATS`] and opens the data each
5//! released binary wrote (tests/data/formats). Each version is read from
6//! the constant the format's own code uses, except the PostgreSQL schema's,
7//! which is defined here and used by that code, since the module that
8//! migrates it is built only with the `postgres` feature.
9
10/// One on-disk format.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct Format {
13    /// Its name on the page, in the formats table and as a column of the
14    /// release table.
15    pub id: &'static str,
16    /// The version this build writes, as the page states it.
17    pub version: &'static str,
18}
19
20/// The version of the PostgreSQL catalog's schema this build makes and
21/// reads; the migrations' count, which a test of that module holds equal.
22pub const POSTGRES_SCHEMA: i32 = 16;
23
24/// The digits of a small number, at compile time, for [`FORMATS`].
25const fn digits(n: i64) -> &'static str {
26    match n {
27        1 => "1",
28        2 => "2",
29        3 => "3",
30        4 => "4",
31        5 => "5",
32        6 => "6",
33        7 => "7",
34        8 => "8",
35        9 => "9",
36        10 => "10",
37        11 => "11",
38        12 => "12",
39        13 => "13",
40        14 => "14",
41        15 => "15",
42        16 => "16",
43        _ => panic!("add the number to formats::digits"),
44    }
45}
46
47/// Every on-disk format, in the order of the page's table.
48pub const FORMATS: &[Format] = &[
49    Format {
50        id: "catalog",
51        version: digits(crate::catalog::SCHEMA_VERSION),
52    },
53    Format {
54        id: "postgres",
55        version: digits(POSTGRES_SCHEMA as i64),
56    },
57    Format {
58        id: "envelope",
59        version: digits(crate::envelope::FORMAT as i64),
60    },
61    Format {
62        id: "key-wrap",
63        version: digits(crate::key_wrap::blob::FORMAT as i64),
64    },
65    Format {
66        id: "parquet-storage",
67        version: crate::store::manifest::FORMAT,
68    },
69    Format {
70        id: "export-manifest",
71        version: digits(crate::export::MANIFEST_FORMAT as i64),
72    },
73    Format {
74        id: "export-columns",
75        version: crate::export::columns::SCHEMA,
76    },
77];
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    fn version(id: &str) -> &'static str {
84        FORMATS
85            .iter()
86            .find(|f| f.id == id)
87            .unwrap_or_else(|| panic!("{id} is listed"))
88            .version
89    }
90
91    #[test]
92    fn each_version_is_its_formats_own_constant() {
93        assert_eq!(
94            version("catalog"),
95            crate::catalog::SCHEMA_VERSION.to_string(),
96            "catalog"
97        );
98        assert_eq!(
99            version("envelope"),
100            crate::envelope::FORMAT.to_string(),
101            "envelope"
102        );
103        assert_eq!(
104            version("parquet-storage"),
105            crate::store::manifest::FORMAT,
106            "parquet"
107        );
108        assert_eq!(
109            version("export-manifest"),
110            crate::export::MANIFEST_FORMAT.to_string(),
111            "export manifest"
112        );
113        assert_eq!(
114            version("export-columns"),
115            crate::export::columns::SCHEMA,
116            "export columns"
117        );
118        assert_eq!(version("postgres"), POSTGRES_SCHEMA.to_string(), "postgres");
119        assert_eq!(
120            version("key-wrap"),
121            crate::key_wrap::blob::FORMAT.to_string(),
122            "key-wrap"
123        );
124    }
125
126    #[test]
127    fn key_wrap_follows_envelope_among_the_formats_read_back() {
128        let ids: Vec<&str> = FORMATS.iter().map(|f| f.id).collect();
129        assert_eq!(
130            ids,
131            [
132                "catalog",
133                "postgres",
134                "envelope",
135                "key-wrap",
136                "parquet-storage",
137                "export-manifest",
138                "export-columns"
139            ],
140            "the page's order: the five read back, then the two exports"
141        );
142    }
143
144    #[test]
145    fn digits_writes_each_number_it_knows() {
146        for n in 1..=16 {
147            assert_eq!(digits(n), n.to_string(), "{n}");
148        }
149    }
150
151    #[test]
152    #[should_panic(expected = "add the number")]
153    fn digits_refuses_a_number_it_does_not_know() {
154        let _ = digits(17);
155    }
156}