flm_core/dump/meta.rs
1//! The header of a JSON output: what kind of output it is, which version of
2//! FLM wrote it, and which version of that kind's format it follows.
3//!
4//! A program that writes data about FLM as JSON (a description of an FLM
5//! environment, a stream of the messages of a build) opens the output with a [`Meta`]
6//! value, so that a reader knows what it reads before it reads the rest. Every
7//! JSON output of this kind shares this one type, whichever crate writes it.
8
9use serde::{Deserialize, Serialize};
10
11/// The header that opens a JSON output: its kind, the version of FLM that
12/// wrote it, and the version of the kind's format.
13///
14/// It serializes as a map with three keys, in this order: `kind`, `flm` and
15/// `format`. A program writes it first: as the value of a `meta` key that
16/// comes first in a single JSON document, or as the first line of a stream of
17/// JSON values written one per line.
18///
19/// # Examples
20///
21/// ```
22/// use flm_core::dump::Meta;
23///
24/// let meta = Meta::new("messages", 1);
25/// let value = serde_json::to_value(&meta).unwrap();
26/// assert_eq!(value["kind"], "messages");
27/// assert_eq!(value["format"], 1);
28/// // The version of FLM that wrote the output, such as `0.6.0`.
29/// assert_eq!(value["flm"], meta.flm.as_str());
30/// ```
31#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
32pub struct Meta {
33 /// What the output is, in one lowercase word: for example `messages`
34 /// for a stream of the messages of a build.
35 pub kind: String,
36 /// The version of FLM that wrote the output, such as `0.6.0`. Every crate
37 /// of FLM has the same version.
38 pub flm: String,
39 /// The version of the format of outputs of this kind: an integer that the
40 /// program writing the kind sets, counted from 1. A reader compares it with
41 /// the version it knows how to read.
42 pub format: u32,
43}
44
45impl Meta {
46 /// Returns the header of an output of the kind `kind` (such as `messages`)
47 /// whose format is the version `format` of that kind's format, written by
48 /// this version of FLM.
49 pub fn new(kind: impl Into<String>, format: u32) -> Meta {
50 Meta { kind: kind.into(), flm: String::from(env!("CARGO_PKG_VERSION")), format }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn a_header_serializes_its_kind_the_version_and_its_format_in_this_order() {
60 let meta = Meta::new("info", 2);
61 assert_eq!(meta.flm, env!("CARGO_PKG_VERSION"));
62 let text = serde_json::to_string(&meta).unwrap();
63 assert_eq!(text, format!("{{\"kind\":\"info\",\"flm\":\"{}\",\"format\":2}}", env!("CARGO_PKG_VERSION")));
64 let read: Meta = serde_json::from_str(&text).unwrap();
65 assert_eq!(read, meta);
66 }
67
68 #[test]
69 fn the_schema_of_a_header_requires_its_three_keys() {
70 let schema = serde_json::to_value(schemars::schema_for!(Meta)).unwrap();
71 let required: Vec<&str> =
72 schema["required"].as_array().unwrap().iter().filter_map(serde_json::Value::as_str).collect();
73 assert_eq!(required, ["kind", "flm", "format"]);
74 }
75}