pgdo/version/
error.rs

1use std::fmt;
2
3/// Error parsing a PostgreSQL version number.
4#[derive(thiserror::Error, miette::Diagnostic, Debug, PartialEq)]
5pub enum VersionError {
6    BadlyFormed { text: Option<String> },
7    NotFound { text: Option<String> },
8}
9
10impl VersionError {
11    pub fn text(&self) -> Option<&str> {
12        match self {
13            Self::BadlyFormed { text: Some(text) } => Some(text.as_str()),
14            Self::NotFound { text: Some(text) } => Some(text.as_str()),
15            _ => None,
16        }
17    }
18}
19
20impl fmt::Display for VersionError {
21    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
22        match self {
23            VersionError::BadlyFormed { text: Some(text) } => {
24                write!(fmt, "version string {text:?} is badly formed")
25            }
26            VersionError::BadlyFormed { text: None } => {
27                write!(fmt, "version string is badly formed")
28            }
29            VersionError::NotFound { text: Some(text) } => {
30                write!(fmt, "version not found in {text:?}")
31            }
32            VersionError::NotFound { text: None } => {
33                write!(fmt, "version not found")
34            }
35        }
36    }
37}