reactive_graph/tooling/update/
release_tag.rs

1use crate::tooling::update::args::UpdateArgs;
2use self_update::cargo_crate_version;
3use std::fmt::Display;
4use std::fmt::Formatter;
5
6pub const RELEASE_TAG_NIGHTLY: &str = "nightly";
7pub const RELEASE_TAG_LATEST: &str = "latest";
8
9#[derive(Debug)]
10pub enum ReleaseTag {
11    Nightly,
12    Latest,
13    Current,
14    Version(String),
15}
16
17impl ReleaseTag {
18    pub fn bin_path_in_archive(&self, current_bin_name: &str) -> String {
19        match self {
20            ReleaseTag::Nightly | ReleaseTag::Latest => format!("reactive-graph-{{{{ version }}}}-{{{{ target }}}}/{current_bin_name}"),
21            ReleaseTag::Current | ReleaseTag::Version(_) => format!("reactive-graph-v{{{{ version }}}}-{{{{ target }}}}/{current_bin_name}"),
22        }
23    }
24
25    pub fn target_version_tag(&self) -> String {
26        prefix_version(match self {
27            ReleaseTag::Nightly | ReleaseTag::Latest => RELEASE_TAG_NIGHTLY,
28            ReleaseTag::Current => cargo_crate_version!(),
29            ReleaseTag::Version(version) => version,
30        })
31    }
32}
33
34impl From<&UpdateArgs> for ReleaseTag {
35    fn from(args: &UpdateArgs) -> Self {
36        if args.nightly.unwrap_or_default() {
37            return ReleaseTag::Nightly;
38        }
39        if args.latest.unwrap_or_default() {
40            return ReleaseTag::Latest;
41        }
42        if args.current.unwrap_or_default() {
43            return ReleaseTag::Current;
44        }
45        if let Some(version) = &args.version {
46            return ReleaseTag::Version(prefix_version(version));
47        }
48        ReleaseTag::Latest
49    }
50}
51
52impl Display for ReleaseTag {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        write!(
55            f,
56            "{}",
57            match self {
58                ReleaseTag::Nightly => RELEASE_TAG_NIGHTLY,
59                ReleaseTag::Latest => RELEASE_TAG_LATEST,
60                ReleaseTag::Current => cargo_crate_version!(),
61                ReleaseTag::Version(version) => &version,
62            }
63        )
64    }
65}
66
67fn prefix_version(version: &str) -> String {
68    if version.starts_with("v") || version == RELEASE_TAG_NIGHTLY {
69        version.to_string()
70    } else {
71        format!("v{version}")
72    }
73}