1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crates_io_api::{SyncClient, Version};
use thiserror;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Unable to find crate")]
    CrateDoesNotExist,
    #[error("Versions Do not exist on crates.io")]
    VersionDoesNotExistCratesIO,
    #[error("Version does not exist on Cargo.toml")]
    VersionDoesNotExist,
    #[error("Cargo.Toml does not contain name")]
    NameDoesNotExist,
}

fn get_latest_version(crate_name: &str) -> Result<String, Box<dyn std::error::Error>> {
    let client = SyncClient::new();
    let retrieved_crate = match client.get_crate(crate_name) {
        Ok(val) => val,
        Err(_) => Err(Error::CrateDoesNotExist)?,
    };
    let versions: Vec<Version> = retrieved_crate.versions;
    match versions.first() {
        Some(version) => Ok(version.num.clone()),
        None => Err(Error::VersionDoesNotExistCratesIO)?,
    }
}

/// Uses cargo environment variables to check for version and name
/// does has the same behavior as check_version after that
pub fn check_version_with_env() -> Result<(), Box<dyn std::error::Error>> {
    let version = std::env::var("CARGO_PKG_VERSION")?;
    let name = std::env::var("CARGO_PKG_NAME")?;
    println!("Name: {}, Version: {}", name, version);
    check_version(&name, &version)
}

/// Validates current version of crate
/// Takes the current name and version
/// Prints directly to stdout (Will probably change to be more asynchrounos)
pub fn check_version(name: &str, current_version: &str) -> Result<(), Box<dyn std::error::Error>> {
    let latest_version = get_latest_version(name)?;
    if latest_version != current_version {
        println!("===================================");
        println!();
        println!("A new version of {} is available!", name);
        println!(
            "Use `$ cargo install {}` to install version {}",
            name, latest_version
        );
        println!("Disregard this message of you are intentionally using an older version, or are working on an unpublished version");
        println!();
        println!("===================================");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_latest_version() {
        let latest_version = get_latest_version("asdev").unwrap();
        assert_eq!(latest_version, "0.1.3")
    }

    #[test]
    fn test_not_current_version() {
        check_version("asdev", "0.1.2").unwrap();
    }

    #[test]
    fn test_env_variables() {
        std::env::set_var("CARGO_PKG_NAME", "asdev");
        std::env::set_var("CARGO_PKG_VERSION", "0.1.2");
        check_version_with_env().unwrap();
    }
}