Skip to main content

bt_rs/common/
version_error.rs

1// version_error.rs : src/common
2
3use std::{
4    error as std_error,
5    fmt as std_fmt,
6    io as std_io,
7};
8
9
10/// Failure to obtain or parse a tool's `--version` output.
11#[derive(Debug)]
12pub enum VersionError {
13    /// Failed to execute the tool with `--version`.
14    InvocationFailed(std_io::Error),
15    /// Tool `--version` stdout was not valid UTF-8.
16    InvalidUtf8,
17    /// Output did not contain a recognisable version token.
18    UnrecognisedOutput,
19    /// A major, minor, or patch component was not a valid unsigned integer.
20    InvalidVersionComponent {
21        component : &'static str,
22        value :     String,
23    },
24}
25
26
27// Trait implementations
28
29impl std_fmt::Display for VersionError {
30    fn fmt(
31        &self,
32        f : &mut std_fmt::Formatter<'_>,
33    ) -> std_fmt::Result {
34        match self {
35            Self::InvocationFailed(e) => {
36                write!(f, "failed to execute tool `--version`: {e}")
37            },
38            Self::InvalidUtf8 => {
39                write!(f, "tool `--version` stdout was not valid UTF-8")
40            },
41            Self::UnrecognisedOutput => {
42                write!(f, "tool `--version` output was not recognised")
43            },
44            Self::InvalidVersionComponent { component, value } => {
45                write!(
46                    f,
47                    "invalid {component} component in tool `--version` output: {value:?}"
48                )
49            },
50        }
51    }
52}
53
54impl std_error::Error for VersionError {
55    fn source(&self) -> Option<&(dyn std_error::Error + 'static)> {
56        match self {
57            Self::InvocationFailed(e) => Some(e),
58            _ => None,
59        }
60    }
61}
62
63
64// ///////////////////////////// end of file //////////////////////////// //