summaryrefslogtreecommitdiffstats
path: root/src/options/version.rs
blob: 0b26c073581e810ceb5dec77fc50e7cc34368be9 (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
//! Printing the version string.
//!
//! The code that works out which string to print is done in `build.rs`.

use std::fmt;

use crate::options::flags;
use crate::options::parser::MatchedFlags;


#[derive(PartialEq, Debug)]
pub struct VersionString;
// There were options here once, but there aren’t anymore!

impl VersionString {

    /// Determines how to show the version, if at all, based on the user’s
    /// command-line arguments. This one works backwards from the other
    /// ‘deduce’ functions, returning Err if help needs to be shown.
    ///
    /// Like --help, this doesn’t bother checking for errors.
    pub fn deduce(matches: &MatchedFlags) -> Result<(), VersionString> {
        if matches.count(&flags::VERSION) > 0 {
            Err(VersionString)
        }
        else {
            Ok(())  // no version needs to be shown
        }
    }
}

impl fmt::Display for VersionString {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{}", include!(concat!(env!("OUT_DIR"), "/version_string.txt")))
    }
}


#[cfg(test)]
mod test {
    use crate::options::Options;
    use std::ffi::OsString;

    fn os(input: &'static str) -> OsString {
        let mut os = OsString::new();
        os.push(input);
        os
    }

    #[test]
    fn help() {
        let args = [ os("--version") ];
        let opts = Options::parse(&args, &None);
        assert!(opts.is_err())
    }
}