Skip to main content

pleiades_data/coverage/
body.rs

1use super::*;
2
3/// Structured coverage summary for the bodies bundled into the packaged artifact.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct PackagedBodyCoverageSummary {
6    /// Number of bodies bundled into the packaged artifact.
7    pub body_count: usize,
8    /// Bodies bundled into the packaged artifact.
9    pub bodies: Vec<CelestialBody>,
10}
11
12/// Validation error for a packaged-body coverage summary that drifted from the bundled body set.
13#[derive(Clone, Debug, Eq, PartialEq)]
14pub enum PackagedBodyCoverageSummaryValidationError {
15    /// A rendered summary field no longer matches the current packaged body set.
16    FieldOutOfSync { field: &'static str },
17}
18
19impl fmt::Display for PackagedBodyCoverageSummaryValidationError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::FieldOutOfSync { field } => write!(
23                f,
24                "the packaged body coverage summary field `{field}` is out of sync with the current bundled body set"
25            ),
26        }
27    }
28}
29
30impl std::error::Error for PackagedBodyCoverageSummaryValidationError {}
31
32impl PackagedBodyCoverageSummary {
33    /// Returns the bundled body set as a compact human-readable line.
34    pub fn summary_line(&self) -> String {
35        format!(
36            "Packaged body set: {} bundled bodies ({})",
37            self.body_count,
38            join_display(&self.bodies)
39        )
40    }
41
42    /// Returns `Ok(())` when the summary still matches the bundled body set.
43    pub fn validate(&self) -> Result<(), PackagedBodyCoverageSummaryValidationError> {
44        let expected_bodies = packaged_bodies();
45
46        if self.body_count != expected_bodies.len() {
47            return Err(PackagedBodyCoverageSummaryValidationError::FieldOutOfSync {
48                field: "body_count",
49            });
50        }
51        if self.bodies.as_slice() != expected_bodies {
52            return Err(PackagedBodyCoverageSummaryValidationError::FieldOutOfSync {
53                field: "bodies",
54            });
55        }
56
57        Ok(())
58    }
59
60    /// Returns the bundled body set as a validated compact human-readable line.
61    pub fn validated_summary_line(
62        &self,
63    ) -> Result<String, PackagedBodyCoverageSummaryValidationError> {
64        self.validate()?;
65        Ok(self.summary_line())
66    }
67}
68
69impl fmt::Display for PackagedBodyCoverageSummary {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.write_str(&self.summary_line())
72    }
73}
74
75/// Returns the structured packaged body coverage summary.
76pub fn packaged_body_coverage_summary_details() -> PackagedBodyCoverageSummary {
77    let bodies = packaged_bodies().to_vec();
78    PackagedBodyCoverageSummary {
79        body_count: bodies.len(),
80        bodies,
81    }
82}