Skip to main content

munin_msbuild/
error.rs

1// Copyright (c) Michael Grier
2
3//! Error types for the munin binlog reader.
4
5use std::{fmt, io};
6
7/// Errors that can occur when reading or decoding a binlog file.
8#[derive(Debug)]
9pub enum MuninError {
10    /// An I/O error occurred while reading the stream.
11    Io(io::Error),
12
13    /// The file does not appear to be a valid binlog (bad magic, truncated header, etc.).
14    InvalidFormat(String),
15
16    /// The binlog's format version is not supported by this reader.
17    UnsupportedVersion {
18        file_version: i32,
19        min_reader_version: i32,
20    },
21
22    /// A record references a string or name-value-list index that has not been seen.
23    InvalidIndex { kind: &'static str, index: i32 },
24
25    /// A 7-bit encoded integer exceeded the maximum allowed byte count.
26    OverlongVarInt,
27
28    /// A string record contains invalid UTF-8.
29    InvalidUtf8,
30}
31
32impl fmt::Display for MuninError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            MuninError::Io(e) => write!(f, "I/O error: {e}"),
36            MuninError::InvalidFormat(msg) => write!(f, "invalid binlog format: {msg}"),
37            MuninError::UnsupportedVersion {
38                file_version,
39                min_reader_version,
40            } => write!(
41                f,
42                "unsupported binlog version {file_version} \
43                 (minimum reader version {min_reader_version}, we support >= 18)"
44            ),
45            MuninError::InvalidIndex { kind, index } => {
46                write!(f, "invalid {kind} index: {index}")
47            }
48            MuninError::OverlongVarInt => write!(f, "7-bit encoded integer is too long"),
49            MuninError::InvalidUtf8 => write!(f, "string record contains invalid UTF-8"),
50        }
51    }
52}
53
54impl std::error::Error for MuninError {
55    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56        match self {
57            MuninError::Io(e) => Some(e),
58            _ => None,
59        }
60    }
61}
62
63impl From<io::Error> for MuninError {
64    fn from(e: io::Error) -> Self {
65        MuninError::Io(e)
66    }
67}