1
use std::fmt;
2

            
3
use clap::Args;
4

            
5
const VERSION: &str = env!("CARGO_PKG_VERSION");
6
const BUILD_HASH: &str = env!("BUILD_HASH");
7

            
8
#[derive(Args, Clone, Copy, Debug)]
9
pub struct VersionFlag {
10
    #[arg(
11
        long,
12
        global = true,
13
        default_value_t = false,
14
        help = "Print the version of this tool"
15
    )]
16
    version: bool,
17
}
18

            
19
impl From<VersionFlag> for bool {
20
    fn from(val: VersionFlag) -> Self {
21
        val.version
22
    }
23
}
24

            
25
pub struct Version;
26

            
27
impl fmt::Display for Version {
28
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29
        // Show a short hash, but fall back to the whole string when it is
30
        // shorter than eight bytes. This is currently the UNKNOWN placeholder.
31
        let short_hash = BUILD_HASH.get(..8).unwrap_or(BUILD_HASH);
32
        write!(f, "{VERSION}-{short_hash}")
33
    }
34
}