diff --git a/tox_node/src/main.rs b/tox_node/src/main.rs index 32dfa0a8..7c7c0e82 100644 --- a/tox_node/src/main.rs +++ b/tox_node/src/main.rs @@ -40,6 +40,23 @@ const ONION_CHANNEL_SIZE: usize = 32; /// Channel size for DHT packets. const DHT_CHANNEL_SIZE: usize = 32; +/// The tox crate version string in the form "major.minor.patch" (e.g. "1.2.3") +pub fn crate_version() -> String { + env!("CARGO_PKG_VERSION").to_string() +} +/// The tox crate major version represented as unsigned integer +pub fn crate_version_major() -> u32 { + env!("CARGO_PKG_VERSION_MAJOR").parse().expect("Invalid major version") +} +/// The tox crate minor version represented as unsigned integer +pub fn crate_version_minor() -> u32 { + env!("CARGO_PKG_VERSION_MINOR").parse().expect("Invalid minor version") +} +/// The tox crate patch version represented as unsigned integer +pub fn crate_version_patch() -> u32 { + env!("CARGO_PKG_VERSION_PATCH").parse().expect("Invalid patch version") +} + /// Get version in format 3AAABBBCCC, where A B and C are major, minor and patch /// versions of node. `tox-bootstrapd` uses similar scheme but with leading 1. /// Before it used format YYYYMMDDVV so the leading numeral was 2. To make a @@ -383,3 +400,29 @@ fn main() { run(future, config.threads); } + +#[cfg(test)] +mod tests { + #[test] + fn crate_version_is_not_empty() { + assert_ne!(crate::crate_version(), ""); + } + + #[test] + fn crate_version_major() { + let v = crate::crate_version_major(); + assert_eq!(v, env!("CARGO_PKG_VERSION_MAJOR").parse::().unwrap()); + } + + #[test] + fn crate_version_minor() { + let v = crate::crate_version_minor(); + assert_eq!(v, env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap()); + } + + #[test] + fn crate_version_patch() { + let v = crate::crate_version_patch(); + assert_eq!(v, env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap()); + } +}