exa/src/options/version.rs

58 lines
1.5 KiB
Rust
Raw Normal View History

//! Printing the version string.
//!
//! The code that works out which string to print is done in `build.rs`.
use std::fmt;
2018-12-07 23:43:31 +00:00
use crate::options::flags;
use crate::options::parser::MatchedFlags;
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct VersionString;
// There were options here once, but there arent anymore!
impl VersionString {
/// Determines how to show the version, if at all, based on the users
/// command-line arguments. This one works backwards from the other
/// deduce functions, returning Err if help needs to be shown.
///
/// Like --help, this doesnt check for errors.
2020-10-13 00:36:41 +00:00
pub fn deduce(matches: &MatchedFlags<'_>) -> Option<Self> {
if matches.count(&flags::VERSION) > 0 {
Some(Self)
}
else {
None
}
}
}
impl fmt::Display for VersionString {
2020-10-13 00:36:41 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", include_str!(concat!(env!("OUT_DIR"), "/version_string.txt")))
}
}
#[cfg(test)]
mod test {
use crate::options::{Options, OptionsResult};
2020-10-12 23:29:49 +00:00
use std::ffi::OsStr;
#[test]
fn version() {
2020-10-12 23:29:49 +00:00
let args = vec![ OsStr::new("--version") ];
let opts = Options::parse(args, &None);
assert!(matches!(opts, OptionsResult::Version(_)));
}
#[test]
fn version_with_file() {
2020-10-12 23:29:49 +00:00
let args = vec![ OsStr::new("--version"), OsStr::new("me") ];
let opts = Options::parse(args, &None);
assert!(matches!(opts, OptionsResult::Version(_)));
}
}