Skip to content

Commit

Permalink
[chshersh#32] Cleanup based on suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancisMurillo committed Sep 3, 2022
1 parent e3bafd9 commit b351764
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/config/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ mod tests {
asset_name.linux = "R2D2"
asset_name.macos = "C3-PO"
asset_name.windows = "IG-88"
tag = "4.2.0"
"#;

let res = parse_string(toml);
Expand All @@ -281,7 +282,7 @@ mod tests {
macos: Some("C3-PO".to_owned()),
windows: Some("IG-88".to_owned()),
},
tag: None,
tag: Some("4.2.0".to_owned()),
},
)]),
};
Expand Down
8 changes: 5 additions & 3 deletions src/model/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ pub enum ToolInfoTag {
Specific(String),
}

const LATEST_VERSION: &str = "latest";

impl ToolInfoTag {
pub fn as_specific_tag(&self) -> Option<&str> {
pub fn to_str_version(&self) -> String {
match self {
Self::Latest => None,
Self::Specific(version) => Some(&version),
Self::Latest => LATEST_VERSION.to_owned(),
Self::Specific(version) => format!("tags/{}", version),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sync/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn full_configure(config_asset: &ConfigAsset) -> Option<ToolInfo> {
let tag = config_asset
.tag
.clone()
.map(|version| ToolInfoTag::Specific(version))
.map(ToolInfoTag::Specific)
.unwrap_or(ToolInfoTag::Latest);

Some(ToolInfo {
Expand Down
30 changes: 12 additions & 18 deletions src/sync/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Downloader<'a> {
pub owner: &'a str,
pub repo: &'a str,
pub asset_name: &'a str,
pub specific_tag: &'a Option<&'a str>,
pub version: &'a str,
pub pb_msg: &'a ProgressBar,
pub sync_progress: &'a SyncProgress,
}
Expand All @@ -25,20 +25,12 @@ pub struct DownloadInfo {

impl<'a> Downloader<'a> {
fn release_url(&self) -> String {
if let Some(tag) = self.specific_tag {
format!(
"https://api.github.com/repos/{owner}/{repo}/releases/tags/{tag}",
owner = self.owner,
repo = self.repo,
tag = tag,
)
} else {
format!(
"https://api.github.com/repos/{owner}/{repo}/releases/latest",
owner = self.owner,
repo = self.repo,
)
}
format!(
"https://api.github.com/repos/{owner}/{repo}/releases/{version}",
owner = self.owner,
repo = self.repo,
version = self.version,
)
}

fn asset_url(&self, asset_id: u32) -> String {
Expand Down Expand Up @@ -133,13 +125,15 @@ pub fn add_auth_header(req: ureq::Request) -> ureq::Request {
mod tests {
use super::*;

use crate::model::tool::ToolInfoTag;

#[test]
fn release_url_with_no_specific_tag_is_correct() {
fn release_url_with_latest_tag_is_correct() {
let downloader = Downloader {
owner: "OWNER",
repo: "REPO",
asset_name: "ASSET_NAME",
specific_tag: &None,
version: &ToolInfoTag::Latest.to_str_version(),
pb_msg: &ProgressBar::hidden(),
sync_progress: &SyncProgress::new(vec!["tool".to_string()]),
};
Expand All @@ -156,7 +150,7 @@ mod tests {
owner: "OWNER",
repo: "REPO",
asset_name: "ASSET_NAME",
specific_tag: &Some("SPECIFIC_TAG"),
version: &ToolInfoTag::Specific("SPECIFIC_TAG".to_string()).to_str_version(),
pb_msg: &ProgressBar::hidden(),
sync_progress: &SyncProgress::new(vec!["tool".to_string()]),
};
Expand Down
2 changes: 1 addition & 1 deletion src/sync/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Installer {
let downloader = Downloader {
owner: &tool_info.owner,
repo: &tool_info.repo,
specific_tag: &tool_info.tag.as_specific_tag(),
version: &tool_info.tag.to_str_version(),
sync_progress: &self.sync_progress,
pb_msg,
asset_name,
Expand Down

0 comments on commit b351764

Please sign in to comment.