omf/omf1/
error.rs

1use super::ModelType;
2
3/// Errors specific to the OMF v1 conversion process.
4///
5/// Converted fails may also fail validation during conversion, as the checks in OMF v1 weren't
6/// as strict.
7#[derive(Debug, thiserror::Error)]
8pub enum Omf1Error {
9    /// Tried to load a file that is not in OMF1 format.
10    #[error("this is not an OMF1 file")]
11    NotOmf1,
12    /// The OMF version is not supported.
13    #[error("version '{version}' is not supported")]
14    UnsupportedVersion { version: String },
15    /// A record in the JSON data has the wrong type.
16    #[error("wrong value type, found {found} when expecting {}", join(expected))]
17    WrongType {
18        found: ModelType,
19        expected: &'static [ModelType],
20    },
21    /// A record is missing from the JSON data.
22    #[error("item '{key}' is missing from the file")]
23    MissingItem { key: String },
24    /// Non-integer values from in what should be an integer array.
25    #[error("an integer array was expected, but floating-point was found")]
26    NonIntegerArray,
27    /// An integer index is invalid.
28    #[error("index {index} is outside the range 0 to 4294967295, or -1 for null categories")]
29    IndexOutOfRange { index: i64 },
30    /// Forwards `serde_json` errors when deserializing OMF1.
31    #[error("JSON deserialization error: {0}")]
32    DeserializationFailed(#[from] serde_json::Error),
33}
34
35fn join(types: &[ModelType]) -> String {
36    match types {
37        [] => "none".to_owned(),
38        [t] => t.to_string(),
39        [t, s] => format!("{t} or {s}"),
40        [s @ .., t] => format!(
41            "{}, or {t}",
42            s.iter()
43                .map(ToString::to_string)
44                .collect::<Vec<_>>()
45                .join(", ")
46        ),
47    }
48}