summaryrefslogtreecommitdiffstats
path: root/src/formatter/version.rs
blob: 808fa15fff559030f8138f0658463bcffa0efd38 (plain)
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
use super::string_formatter::StringFormatterError;
use super::StringFormatter;
use once_cell::sync::Lazy;
use std::ops::Deref;
use versions::Versioning;

pub struct VersionFormatter<'a> {
    formatter: StringFormatter<'a>,
}

impl<'a> VersionFormatter<'a> {
    /// Creates an instance of a VersionFormatter from a format string
    ///
    /// Like the StringFormatter, this will throw an error when the string isn't
    /// parseable.
    pub fn new(format: &'a str) -> Result<Self, StringFormatterError> {
        let formatter = StringFormatter::new(format)?;

        Ok(Self { formatter })
    }

    /// Format version string using provided format
    pub fn format_version(
        version: &'a str,
        format: &'a str,
    ) -> Result<String, StringFormatterError> {
        Self::new(format).and_then(|formatter| formatter.format(version))
    }

    /// Formats a version structure into a readable string
    pub fn format(self, version: &'a str) -> Result<String, StringFormatterError> {
        let parsed = Lazy::new(|| Versioning::new(version));
        let formatted = self
            .formatter
            .map(|variable| match variable {
                "raw" => Some(Ok(version.to_string())),
                "major" => match parsed.deref().as_ref() {
                    Some(Versioning::Ideal(v)) => Some(Ok(v.major.to_string())),
                    Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(0)?.to_string())),
                    _ => None,
                },
                "minor" => match parsed.deref().as_ref() {
                    Some(Versioning::Ideal(v)) => Some(Ok(v.minor.to_string())),
                    Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(1)?.to_string())),
                    _ => None,
                },
                "patch" => match parsed.deref().as_ref() {
                    Some(Versioning::Ideal(v)) => Some(Ok(v.patch.to_string())),
                    Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(2)?.to_string())),
                    _ => None,
                },
                _ => None,
            })
            .parse(None);

        formatted.map(|segments| {
            segments
                .iter()
                .map(|segment| segment.value.as_str())
                .collect::<String>()
        })
    }

    pub fn format_module_version(
        module_name: &str,
        version: &str,
        version_format: &str,
    ) -> Option<String> {
        match VersionFormatter::format_version(version, version_format) {
            Ok(formatted) => Some(formatted),
            Err(error) => {
                log::warn!("Error formatting `{}` version:\n{}", module_name, error);
                Some(format!("v{}", version))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const VERSION_FORMAT: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";

    #[test]
    fn test_semver_full() {
        assert_eq!(
            VersionFormatter::format_version("1.2.3", VERSION_FORMAT),
            Ok("major:1 minor:2 patch:3 raw:1.2.3".to_string())
        );
    }

    #[test]
    fn test_semver_partial() {
        assert_eq!(
            VersionFormatter::format_version("1.2", VERSION_FORMAT),
            Ok("major:1 minor:2 patch: raw:1.2".to_string())
        );
    }

    #[test]
    fn test_general() {
        assert_eq!(
            VersionFormatter::format_version("1.2-a.3", VERSION_FORMAT),
            Ok("major:1 minor:2 patch: raw:1.2-a.3".to_string())
        );
    }

    #[test]
    fn test_dummy() {
        assert_eq!(
            VersionFormatter::format_version("dummy version", VERSION_FORMAT),
            Ok("major: minor: patch: raw:dummy version".to_string())
        );
    }
}