1use std::str::FromStr;
2
3use lazy_static::lazy_static;
4use regex::Regex;
5use semver::Version;
6
7use crate::error::{Error, Result};
8
9lazy_static! {
10 static ref VERSION_WITHOUT_PATCH: Regex = Regex::new(r"^(\d+)\.(\d+)$").unwrap();
11}
12
13pub enum FileExtension {
14 Msi,
15 Tgz,
16}
17
18impl FileExtension {
19 pub fn name(&self) -> &'static str {
20 match *self {
21 FileExtension::Msi => "msi",
22 FileExtension::Tgz => "tgz",
23 }
24 }
25}
26
27#[inline]
28pub fn get_from_str<T: FromStr>(s: &str) -> Option<T> {
29 FromStr::from_str(s).ok()
30}
31
32#[macro_export]
33macro_rules! version {
34 ($major:expr, $minor:expr, $patch:expr) => {{
35 ::semver::Version {
36 major: $major,
37 minor: $minor,
38 patch: $patch,
39 pre: Vec::new(),
40 build: Vec::new(),
41 }
42 }};
43}
44
45pub fn select_newer_version(existing: Option<Version>, found: Version) -> Version {
46 if let Some(version) = existing {
47 if version > found {
48 return version;
49 }
50 }
51
52 found
53}
54
55pub fn parse_major_minor_version(version: &str) -> Option<(u64, u64)> {
56 VERSION_WITHOUT_PATCH
57 .captures(version)
58 .map(|c| (c[1].parse().unwrap(), c[2].parse().unwrap()))
59}
60
61pub fn parse_version(version: &str) -> Result<Version> {
62 Version::parse(version).map_err(|_| {
63 Error::VersionNotFound {
64 version: version.into(),
65 }
66 .into()
67 })
68}