hdiff-update-core 0.2.0

Core library for signed, transactional HDiffPatch directory updates.
Documentation
use semver::Version;

use crate::{Error, Result};

pub fn validate_version_upgrade(current: &str, target: &str) -> Result<()> {
    let current_version = parse_version(current)?;
    let target_version = parse_version(target)?;
    if target_version <= current_version {
        return Err(Error::NonUpgradeVersion {
            current: current.to_string(),
            target: target.to_string(),
        });
    }
    Ok(())
}

fn parse_version(value: &str) -> Result<Version> {
    Version::parse(value.trim()).map_err(|error| Error::InvalidVersion {
        value: value.to_string(),
        message: error.to_string(),
    })
}

#[cfg(test)]
mod tests {
    use super::validate_version_upgrade;

    #[test]
    fn accepts_only_strict_semver_upgrades() {
        assert!(validate_version_upgrade("1.2.3", "1.2.4").is_ok());
        assert!(validate_version_upgrade("1.2.3-beta.1", "1.2.3").is_ok());
        assert!(validate_version_upgrade("1.2.3", "1.2.3").is_err());
        assert!(validate_version_upgrade("1.2.4", "1.2.3").is_err());
        assert!(validate_version_upgrade("latest", "next").is_err());
    }
}