/ radicle / src / version.rs
version.rs
 1  use std::io;
 2  
 3  /// Print program version.
 4  ///
 5  /// The program version follows [semantic versioning](https://semver.org).
 6  ///
 7  /// Adjust with caution, third party applications parse the string for version info.
 8  pub fn print(
 9      mut w: impl std::io::Write,
10      name: &str,
11      version: &str,
12      git_head: &str,
13  ) -> Result<(), io::Error> {
14      if version.ends_with("-dev") {
15          writeln!(w, "{name} {version}+{git_head}")?;
16      } else {
17          writeln!(w, "{name} {version} ({git_head})")?;
18      };
19      Ok(())
20  }
21  
22  #[cfg(test)]
23  mod test {
24      use super::*;
25  
26      #[test]
27      fn test_version() {
28          let mut buffer = Vec::new();
29          print(&mut buffer, "rad", "1.2.3", "28b341d").unwrap();
30          let res = std::str::from_utf8(&buffer).unwrap();
31          assert_eq!("rad 1.2.3 (28b341d)\n", res);
32  
33          let mut buffer = Vec::new();
34          print(&mut buffer, "rad", "1.2.3-dev", "28b341d").unwrap();
35          let res = std::str::from_utf8(&buffer).unwrap();
36          assert_eq!("rad 1.2.3-dev+28b341d\n", res);
37      }
38  }