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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Parse PostgreSQL version numbers.
//!
//! ```rust
//! # use pgdo::version::Version;
//! assert_eq!(Ok(Version::Pre10(9, 6, 17)), "9.6.17".parse());
//! assert_eq!(Ok(Version::Post10(14, 6)), "14.6".parse());
//! ```
//!
//! See the [PostgreSQL "Versioning Policy" page][versioning] for information on
//! PostgreSQL's versioning scheme.
//!
//! [versioning]: https://www.postgresql.org/support/versioning/

// TODO: Parse `server_version_num`/`PG_VERSION_NUM`, e.g. 120007 for version
// 12.7, 90624 for 9.6.24. See https://pgpedia.info/s/server_version_num.html
// and https://www.postgresql.org/docs/16/runtime-config-preset.html.

use std::fmt;
use std::str::FromStr;

use regex::Regex;

use super::VersionError;

/// Represents a full PostgreSQL version. This is the kind of thing we see when
/// running `pg_ctl --version` for example.
///
/// The "Current minor" column shown on the [PostgreSQL "Versioning Policy"
/// page][versioning] is what this models.
///
/// [versioning]: https://www.postgresql.org/support/versioning/
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Version {
    /// Pre-PostgreSQL 10, with major, point, and minor version numbers, e.g.
    /// 9.6.17. It is an error to create this variant with a major number >= 10.
    Pre10(u32, u32, u32),
    /// PostgreSQL 10+, with major and minor version number, e.g. 10.3. It is an
    /// error to create this variant with a major number < 10.
    Post10(u32, u32),
}

impl fmt::Display for Version {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Version::Pre10(a, b, c) => fmt.pad(&format!("{a}.{b}.{c}")),
            Version::Post10(a, b) => fmt.pad(&format!("{a}.{b}")),
        }
    }
}

impl FromStr for Version {
    type Err = VersionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        lazy_static! {
            static ref RE: Regex = Regex::new(r"(?x) \b (\d+) [.] (\d+) (?: [.] (\d+) )? \b")
                .expect("invalid regex (for matching PostgreSQL versions)");
        }
        let badly_formed = |_| VersionError::BadlyFormed { text: Some(s.into()) };
        match RE.captures(s) {
            Some(caps) => {
                let a = caps[1].parse::<u32>().map_err(badly_formed)?;
                let b = caps[2].parse::<u32>().map_err(badly_formed)?;
                match caps.get(3) {
                    None if a >= 10 => Ok(Version::Post10(a, b)),
                    None => Err(VersionError::BadlyFormed { text: Some(s.into()) }),
                    Some(_) if a >= 10 => Err(VersionError::BadlyFormed { text: Some(s.into()) }),
                    Some(m) => Ok(m
                        .as_str()
                        .parse::<u32>()
                        .map(|c| Version::Pre10(a, b, c))
                        .map_err(badly_formed)?),
                }
            }
            None => Err(VersionError::NotFound { text: Some(s.into()) }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Version::{Post10, Pre10};
    use super::{Version, VersionError::*};

    use std::cmp::Ordering;

    #[test]
    fn parses_version_below_10() {
        assert_eq!(Ok(Pre10(9, 6, 17)), "9.6.17".parse());
    }

    #[test]
    fn parses_version_above_10() {
        assert_eq!(Ok(Post10(12, 2)), "12.2".parse());
    }

    #[test]
    fn parse_returns_error_when_version_is_invalid() {
        // 4294967295 is (2^32 + 1), so won't fit in a u32.
        assert!(matches!(
            "4294967296.0".parse::<Version>(),
            Err(BadlyFormed { .. })
        ));
    }

    #[test]
    fn parse_returns_error_when_version_not_found() {
        assert!(matches!("foo".parse::<Version>(), Err(NotFound { .. })));
    }

    #[test]
    fn displays_version_below_10() {
        assert_eq!("9.6.17", format!("{}", Pre10(9, 6, 17)));
    }

    #[test]
    fn displays_version_above_10() {
        assert_eq!("12.2", format!("{}", Post10(12, 2)));
    }

    #[test]
        #[rustfmt::skip]
        fn derive_partial_ord_works_as_expected() {
            assert_eq!(Pre10(9, 10, 11).partial_cmp(&Post10(10, 11)), Some(Ordering::Less));
            assert_eq!(Post10(10, 11).partial_cmp(&Pre10(9, 10, 11)), Some(Ordering::Greater));
            assert_eq!(Pre10(9, 10, 11).partial_cmp(&Pre10(9, 10, 11)), Some(Ordering::Equal));
            assert_eq!(Post10(10, 11).partial_cmp(&Post10(10, 11)), Some(Ordering::Equal));
        }

    #[test]
    fn derive_ord_works_as_expected() {
        let mut versions = vec![
            Pre10(9, 10, 11),
            Post10(10, 11),
            Post10(14, 2),
            Pre10(9, 10, 12),
            Post10(10, 12),
        ];
        versions.sort(); // Uses `Ord`.
        assert_eq!(
            versions,
            vec![
                Pre10(9, 10, 11),
                Pre10(9, 10, 12),
                Post10(10, 11),
                Post10(10, 12),
                Post10(14, 2)
            ]
        );
    }
}