hang/catalog/mode.rs
1use crate::Error;
2
3use derive_more::Display;
4use std::str::FromStr;
5
6/// How a metadata track's groups carry its payloads.
7///
8/// The two modes are equally primary and there is no safe default: reading an append log as a
9/// latest-value document silently drops every record but the last. So a
10/// [`JsonConfig`](crate::catalog::JsonConfig) or [`BinaryConfig`](crate::catalog::BinaryConfig)
11/// always states its mode, and a consumer that doesn't recognize the value
12/// ([`Unknown`](Self::Unknown)) must ignore the track rather than guess.
13#[derive(Debug, Clone, PartialEq, Eq, Display)]
14#[non_exhaustive]
15pub enum Mode {
16 /// Lossy latest-value: each group carries one complete payload and supersedes the previous
17 /// group, so a consumer reads only the newest.
18 ///
19 /// JSON tracks may follow the first frame of a group with
20 /// [RFC 7396](https://www.rfc-editor.org/rfc/rfc7396.html) merge-patch deltas; binary tracks
21 /// carry a single frame per group.
22 #[display("snapshot")]
23 Snapshot,
24
25 /// Lossless append log: a single group that is never rolled, one payload per frame, delivered
26 /// in order with nothing superseded.
27 #[display("stream")]
28 Stream,
29
30 /// A mode this build does not recognize, preserved verbatim.
31 ///
32 /// A consumer MUST ignore the track: it cannot know whether skipping to the newest group
33 /// would lose records.
34 #[display("{_0}")]
35 Unknown(String),
36}
37
38impl FromStr for Mode {
39 type Err = Error;
40
41 fn from_str(s: &str) -> Result<Self, Self::Err> {
42 Ok(match s {
43 "snapshot" => Self::Snapshot,
44 "stream" => Self::Stream,
45 _ => Self::Unknown(s.to_string()),
46 })
47 }
48}
49
50#[cfg(test)]
51mod test {
52 use super::*;
53
54 #[test]
55 fn unknown_roundtrip() {
56 let wire = serde_json::json!({ "mode": "future" });
57 let config: crate::catalog::JsonConfig = serde_json::from_value(wire.clone()).unwrap();
58 assert_eq!(config.mode, Mode::Unknown("future".to_string()));
59 assert_eq!(serde_json::to_value(&config).unwrap(), wire);
60 }
61}