1use hpr_aero::AeroError;
4use hpr_atmos::AtmosError;
5use hpr_core::CoreError;
6use hpr_design::{DesignError, Finding, Severity};
7use thiserror::Error;
8
9use crate::integrator::{IntegrationError, SettingsError};
10
11#[derive(Debug, Error)]
13#[non_exhaustive]
14pub enum SimError {
15 #[error("{what} is outside its domain: {value}")]
17 Domain {
18 what: &'static str,
20 value: f64,
22 },
23 #[error("unsupported: {what}")]
25 Unsupported {
26 what: &'static str,
28 },
29 #[error("{what}: `{component}`")]
32 Parting {
33 what: &'static str,
35 component: String,
37 },
38 #[error("{what}: `{component}`")]
40 Shift {
41 what: &'static str,
43 component: String,
45 },
46 #[error("{what}: `{component}`")]
48 MassRelease {
49 what: &'static str,
51 component: String,
53 },
54 #[error("the {history} history can't be flown: {why}")]
57 History {
58 history: &'static str,
60 why: &'static str,
62 },
63 #[error(
66 "the design has {} error finding(s); the first is {}",
67 error_count(.0),
68 first_error(.0)
69 )]
70 DesignChecks(Vec<Finding>),
71 #[error(transparent)]
73 Design(#[from] DesignError),
74 #[error(transparent)]
77 Aero(#[from] AeroError),
78 #[error(transparent)]
80 Atmosphere(#[from] AtmosError),
81 #[error(transparent)]
83 Core(#[from] CoreError),
84 #[error(transparent)]
86 Settings(#[from] SettingsError),
87 #[error("the integration failed")]
89 Integration(#[source] Box<IntegrationError<SimError>>),
90}
91
92fn error_count(findings: &[Finding]) -> usize {
94 findings
95 .iter()
96 .filter(|finding| finding.severity() == Severity::Error)
97 .count()
98}
99
100fn first_error(findings: &[Finding]) -> String {
102 findings
103 .iter()
104 .find(|finding| finding.severity() == Severity::Error)
105 .map_or_else(|| "missing".to_owned(), |finding| format!("{finding:?}"))
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
114 fn design_checks_message_counts_and_names_only_errors() {
115 let warning = Finding::MotorPastMountTop {
116 configuration: "c".to_owned(),
117 mount: "mount".to_owned(),
118 excess_m: 0.01,
119 };
120 let error = Finding::MotorWiderThanMount {
121 configuration: "c".to_owned(),
122 mount: "mount".to_owned(),
123 motor_diameter_m: 0.038,
124 mount_inner_diameter_m: 0.029,
125 };
126 let message = SimError::DesignChecks(vec![warning, error.clone()]).to_string();
127 assert_eq!(
128 message,
129 format!("the design has 1 error finding(s); the first is {error:?}")
130 );
131 }
132}