1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::fmt;

/// Error parsing a PostgreSQL version number.
#[derive(thiserror::Error, miette::Diagnostic, Debug, PartialEq)]
pub enum VersionError {
    BadlyFormed { text: Option<String> },
    NotFound { text: Option<String> },
}

impl VersionError {
    pub fn text(&self) -> Option<&str> {
        match self {
            Self::BadlyFormed { text: Some(text) } => Some(text.as_str()),
            Self::NotFound { text: Some(text) } => Some(text.as_str()),
            _ => None,
        }
    }
}

impl fmt::Display for VersionError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            VersionError::BadlyFormed { text: Some(text) } => {
                write!(fmt, "version string {text:?} is badly formed")
            }
            VersionError::BadlyFormed { text: None } => {
                write!(fmt, "version string is badly formed")
            }
            VersionError::NotFound { text: Some(text) } => {
                write!(fmt, "version not found in {text:?}")
            }
            VersionError::NotFound { text: None } => {
                write!(fmt, "version not found")
            }
        }
    }
}