Skip to main content

mf4_rs/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum MdfError {
5    /// No channel with that name (optionally: in that group) exists.
6    ///
7    /// Raised by every data operation (`read`, `values`, `time_range`,
8    /// `conversion`, `byte_ranges`). Metadata lookups (`group`, `channel`)
9    /// return `None` instead.
10    #[error("{}", channel_not_found_message(.name, .group))]
11    ChannelNotFound {
12        name: String,
13        group: Option<String>,
14    },
15
16    /// A `ReadOptions` value (or another argument) is not usable.
17    #[error("Invalid argument: {0}")]
18    InvalidArgument(String),
19
20    #[error("Buffer too small at {file}:{line}: need at least {expected} bytes, got {actual}")]
21    TooShortBuffer {
22        actual:   usize,
23        expected: usize,
24        file:     &'static str,
25        line:     u32,
26    },
27
28    #[error(r#"Invalid file identifier: Expected "MDF     ", found {0}"#)]
29    FileIdentifierError(String),
30
31    #[error(r#"File version too low: Expected "> 4.1", found {0}"#)]
32    FileVersioningError(String),
33
34    #[error("Invalid block identifier: Expected {expected:?}, got {actual:?}")]
35    BlockIDError {
36        actual: String,
37        expected: String,
38    },
39
40    #[error("I/O error: {0}")]
41    IOError(#[from] std::io::Error),
42
43    #[error("Invalid version string: {0}")]
44    InvalidVersionString(String),
45
46    #[error("Block linking error: {0}")]
47    BlockLinkError(String),
48
49    #[error("Block serialization error: {0}")]
50    BlockSerializationError(String),
51
52    #[error("Conversion chain too deep: maximum depth of {max_depth} exceeded")]
53    ConversionChainTooDeep { max_depth: usize },
54
55    #[error("Conversion chain cycle detected at block address {address:#x}")]
56    ConversionChainCycle { address: u64 },
57}
58
59fn channel_not_found_message(name: &str, group: &Option<String>) -> String {
60    match group {
61        Some(g) => format!("Channel '{}' not found in group '{}'", name, g),
62        None => format!("Channel '{}' not found", name),
63    }
64}
65
66impl MdfError {
67    /// Build a [`MdfError::ChannelNotFound`].
68    pub fn channel_not_found(name: &str, group: Option<&str>) -> Self {
69        MdfError::ChannelNotFound { name: name.to_string(), group: group.map(str::to_string) }
70    }
71}