junobuild_shared::assert

Function assert_version

Source
pub fn assert_version(
    user_version: Option<Version>,
    current_version: Option<Version>,
) -> Result<(), String>
Expand description

Asserts the validity of a given user version against the required version. This function checks if the provided user version matches the current system version. It is designed to ensure that operations relying on version numbers are executed with the correct versions to prevent issues with compatibility or outdated requests.

§Parameters

  • user_version: An Option<u64> representing the user-provided version. This can be None if the user did not provide a version, or Some(u64) if a version was provided.
  • current_version: An Option<u64> representing the required system version. This can be None which means no specific version requirement is needed.

§Returns

  • Ok(()) if the user_version matches the current_version or if no specific current_version is provided.
  • Err(String) if:
    • The user_version is None, indicating no version was provided. The error string will be ERROR_NO_VERSION.to_string(), where ERROR_NO_VERSION is a constant string describing the error.
    • The user_version does not match the current_version, indicating either an incorrect or incompatible version. The error string will format to include the error description (from a constant ERROR_VERSION_MISMATCH), the required version, and the provided user version.

§Examples

let current_version = Some(3); // Example version
let user_version = Some(3);
assert_eq!(assert_version(user_version, current_version), Ok(()));

let wrong_version = Some(2);
assert!(assert_version(wrong_version, current_version).is_err());

let no_version = None;
let no_current_version = None;
assert!(assert_version(no_version, no_current_version).is_ok());