Skip to main content

perspective_migrate/
version.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::fmt;
14
15/// A Perspective release, ordered by `major.minor.patch` with any
16/// pre-release or build suffix ignored.
17#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
18pub struct Version {
19    pub major: u64,
20    pub minor: u64,
21    pub patch: u64,
22}
23
24impl Version {
25    pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
26        Self {
27            major,
28            minor,
29            patch,
30        }
31    }
32
33    /// Parse `x.y.z`, `x.y.z-pre` or `x.y.z+build`.
34    pub fn parse(version: &str) -> Option<Self> {
35        let core = version.trim().split(['-', '+']).next()?;
36        let mut parts = core.split('.').map(|x| x.parse::<u64>().ok());
37        let version = Self::new(parts.next()??, parts.next()??, parts.next()??);
38        parts.next().is_none().then_some(version)
39    }
40
41    pub(crate) fn crate_version() -> Self {
42        Self::parse(env!("CARGO_PKG_VERSION")).expect("CARGO_PKG_VERSION")
43    }
44}
45
46impl fmt::Display for Version {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
49    }
50}