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
79
80
81
use semver::{Version, VersionReq};

use crate::error::*;

pub mod runner;
pub use self::runner::{ExecutableRunner, Output};

/// Details and requirements for executables.
pub trait Executable {
    /// Returns executable name in `PATH`.
    fn get_name(&self) -> String;

    /// Returns message about how to install missing executable.
    fn get_verification_hint(&self) -> String;

    /// Returns message about how to update outdated executable.
    fn get_version_hint(&self) -> String;

    /// Executable version constraint.
    fn get_required_version(&self) -> Option<VersionReq>;

    /// Returns the current version of the executable.
    fn get_current_version(&self) -> Result<Version>
    where
        Self: Sized,
    {
        self::runner::parse_executable_version(self)
    }
}

/// `cargo` command.
pub struct Cargo;

/// `ptx-linker` command.
pub struct Linker;

impl Executable for Cargo {
    fn get_name(&self) -> String {
        String::from("cargo")
    }

    fn get_verification_hint(&self) -> String {
        String::from("Please make sure you have it installed and in PATH")
    }

    fn get_version_hint(&self) -> String {
        String::from("Please update Rust and Cargo to latest nightly versions")
    }

    fn get_required_version(&self) -> Option<VersionReq> {
        Some(VersionReq::parse(">= 1.34.0-nightly").unwrap())
    }

    fn get_current_version(&self) -> Result<Version> {
        // Omit Rust channel name because it's not really semver-correct
        // https://github.com/steveklabnik/semver/issues/105

        self::runner::parse_executable_version(self).map(|mut version| {
            version.pre = vec![];
            version
        })
    }
}

impl Executable for Linker {
    fn get_name(&self) -> String {
        String::from("rust-ptx-linker")
    }

    fn get_verification_hint(&self) -> String {
        String::from("You can install it with: 'cargo install ptx-linker'")
    }

    fn get_version_hint(&self) -> String {
        String::from("You can update it with: 'cargo install -f ptx-linker'")
    }

    fn get_required_version(&self) -> Option<VersionReq> {
        Some(VersionReq::parse(">= 0.9.0").unwrap())
    }
}