use serde::Serialize;
use crate::snapshot::Snapshot;
pub(crate) const SCHEMA: u32 = 1;
#[derive(Debug, Clone, Serialize)]
pub struct SettledDocument {
pub schema: u32,
#[serde(flatten)]
pub snapshot: Snapshot,
}
impl SettledDocument {
pub fn new(snapshot: Snapshot) -> Self {
SettledDocument {
schema: SCHEMA,
snapshot,
}
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::SCHEMA;
use crate::cell::{Settled, Timestamp, Unknown};
use crate::git::ProbeError;
#[test]
fn wire_schema_bump_discipline_matches_the_closed_variant_counts_below() {
const SETTLED_VARIANT_COUNT: usize = 4;
const UNKNOWN_REASON_COUNT: usize = 3;
fn settled_variant_index(settled: &Settled<u32>) -> usize {
match settled {
Settled::Unknown(_) => 0,
Settled::Known {
value: _,
at: _,
stale: _,
} => 1,
Settled::Failed(_) => 2,
Settled::NotApplicable => 3,
}
}
fn unknown_reason_index(reason: &Unknown) -> usize {
match reason {
Unknown::TimedOut => 0,
Unknown::NoDefaultBranch => 1,
Unknown::SubmoduleUninitialized => 2,
}
}
let settled_examples: [Settled<u32>; SETTLED_VARIANT_COUNT] = [
Settled::Unknown(Unknown::TimedOut),
Settled::Known {
value: 0,
at: Timestamp::now(),
stale: false,
},
Settled::Failed(ProbeError::Open(Arc::from("boom"))),
Settled::NotApplicable,
];
let mut settled_indices: Vec<usize> =
settled_examples.iter().map(settled_variant_index).collect();
settled_indices.sort_unstable();
assert_eq!(
settled_indices,
(0..SETTLED_VARIANT_COUNT).collect::<Vec<_>>(),
"a `Settled` variant is missing its own example above, or `SETTLED_VARIANT_COUNT` \
is stale; bump `SCHEMA` in the same change that fixes this"
);
let unknown_examples: [Unknown; UNKNOWN_REASON_COUNT] = [
Unknown::TimedOut,
Unknown::NoDefaultBranch,
Unknown::SubmoduleUninitialized,
];
let mut unknown_indices: Vec<usize> =
unknown_examples.iter().map(unknown_reason_index).collect();
unknown_indices.sort_unstable();
assert_eq!(
unknown_indices,
(0..UNKNOWN_REASON_COUNT).collect::<Vec<_>>(),
"an `Unknown` reason is missing its own example above, or `UNKNOWN_REASON_COUNT` \
is stale; bump `SCHEMA` in the same change that fixes this"
);
assert_eq!(
SCHEMA, 1,
"`SCHEMA` moved without this test's own counts being reviewed; update \
`SETTLED_VARIANT_COUNT` and `UNKNOWN_REASON_COUNT` above for whatever changed, \
then move this literal to match"
);
}
}