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 = 13;
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: "parquet-storage",
63        version: crate::store::manifest::FORMAT,
64    },
65    Format {
66        id: "export-manifest",
67        version: digits(crate::export::MANIFEST_FORMAT as i64),
68    },
69    Format {
70        id: "export-columns",
71        version: crate::export::columns::SCHEMA,
72    },
73];
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    fn version(id: &str) -> &'static str {
80        FORMATS
81            .iter()
82            .find(|f| f.id == id)
83            .unwrap_or_else(|| panic!("{id} is listed"))
84            .version
85    }
86
87    #[test]
88    fn each_version_is_its_formats_own_constant() {
89        assert_eq!(
90            version("catalog"),
91            crate::catalog::SCHEMA_VERSION.to_string(),
92            "catalog"
93        );
94        assert_eq!(
95            version("envelope"),
96            crate::envelope::FORMAT.to_string(),
97            "envelope"
98        );
99        assert_eq!(
100            version("parquet-storage"),
101            crate::store::manifest::FORMAT,
102            "parquet"
103        );
104        assert_eq!(
105            version("export-manifest"),
106            crate::export::MANIFEST_FORMAT.to_string(),
107            "export manifest"
108        );
109        assert_eq!(
110            version("export-columns"),
111            crate::export::columns::SCHEMA,
112            "export columns"
113        );
114        assert_eq!(version("postgres"), POSTGRES_SCHEMA.to_string(), "postgres");
115    }
116
117    #[test]
118    fn digits_writes_each_number_it_knows() {
119        for n in 1..=16 {
120            assert_eq!(digits(n), n.to_string(), "{n}");
121        }
122    }
123
124    #[test]
125    #[should_panic(expected = "add the number")]
126    fn digits_refuses_a_number_it_does_not_know() {
127        let _ = digits(17);
128    }
129}