-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#83] Create 'AssetError' and refactor 'select_asset' function to return it #97
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9f0f719
Refactor select_asset() to use AssetError (NameUnknown or NotFound)
zixuan-x 9bf1197
Add tests and format file
zixuan-x 41ee6ad
Add test for asset found
zixuan-x 53b7360
Remove todos from README.md
zixuan-x 7ea1c3f
Format code
zixuan-x 1a9f95d
Add documentation for select_asset
zixuan-x 184f7d9
Update src/model/release.rs
zixuan-x 87ce034
Delete error Display trait test
zixuan-x 9d14b27
Rename `NameUnknown` error to `OsSelectorUnknown`
zixuan-x 8f68103
Fix linting
zixuan-x 0f634af
Add test for OsSelectorUnknown
zixuan-x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,42 @@ | ||
use serde::Deserialize; | ||
use std::env; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Release { | ||
pub tag_name: String, | ||
pub assets: Vec<Asset>, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)] | ||
pub struct Asset { | ||
pub id: u32, | ||
pub name: String, | ||
pub size: u64, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum AssetError { | ||
/// Asset name of this OS is unknown | ||
OsSelectorUnknown, | ||
|
||
/// Asset name is not in the fetched assets | ||
NotFound(String), | ||
} | ||
|
||
impl Display for AssetError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::OsSelectorUnknown => { | ||
write!( | ||
f, | ||
"Unknown asset selector for OS: {}. Specify 'asset_name.your_os' in the cofig.", | ||
env::consts::OS | ||
) | ||
} | ||
Self::NotFound(asset_name) => { | ||
write!(f, "No asset matching name: {}", asset_name) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use super::release::Asset; | ||
use crate::infra::client::Client; | ||
use crate::model::asset_name::AssetName; | ||
use crate::model::release::AssetError; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum Tool { | ||
|
@@ -69,16 +70,15 @@ pub struct ToolInfo { | |
} | ||
|
||
impl ToolInfo { | ||
pub fn select_asset(&self, assets: &[Asset]) -> Result<Asset, String> { | ||
/// Select an Asset from all Assets based on which Operating System is used | ||
pub fn select_asset(&self, assets: &[Asset]) -> Result<Asset, AssetError> { | ||
match self.asset_name.get_name_by_os() { | ||
None => Err(String::from( | ||
"Don't know the asset name for this OS: specify it explicitly in the config", | ||
)), | ||
None => Err(AssetError::OsSelectorUnknown), | ||
Some(asset_name) => { | ||
let asset = assets.iter().find(|&asset| asset.name.contains(asset_name)); | ||
|
||
match asset { | ||
None => Err(format!("No asset matching name: {}", asset_name)), | ||
None => Err(AssetError::NotFound(asset_name.to_owned())), | ||
Some(asset) => Ok(asset.clone()), | ||
} | ||
} | ||
|
@@ -107,3 +107,112 @@ pub struct ToolAsset { | |
/// GitHub API client that produces the stream for downloading the asset | ||
pub client: Client, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
Comment on lines
+111
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great work on tests! 👏🏻 Let's also add one more test for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch. added |
||
|
||
#[test] | ||
fn asset_fount() { | ||
let asset_name = "asset"; | ||
|
||
let tool_info = ToolInfo { | ||
owner: "owner".to_string(), | ||
repo: "repo".to_string(), | ||
exe_name: "exe".to_string(), | ||
tag: ToolInfoTag::Latest, | ||
asset_name: AssetName { | ||
linux: Some(asset_name.to_string()), | ||
macos: Some(asset_name.to_string()), | ||
windows: Some(asset_name.to_string()), | ||
}, | ||
}; | ||
|
||
let assets = vec![ | ||
Asset { | ||
id: 1, | ||
name: "1".to_string(), | ||
size: 10, | ||
}, | ||
Asset { | ||
id: 2, | ||
name: asset_name.to_string(), | ||
size: 50, | ||
}, | ||
Asset { | ||
id: 3, | ||
name: "3".to_string(), | ||
size: 77, | ||
}, | ||
]; | ||
|
||
assert_eq!( | ||
tool_info.select_asset(&assets), | ||
Ok(Asset { | ||
id: 2, | ||
name: asset_name.to_string(), | ||
size: 50 | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn asset_not_found() { | ||
let asset_name = "asset"; | ||
|
||
let tool_info = ToolInfo { | ||
owner: "owner".to_string(), | ||
repo: "repo".to_string(), | ||
exe_name: "exe".to_string(), | ||
tag: ToolInfoTag::Latest, | ||
asset_name: AssetName { | ||
linux: Some(asset_name.to_string()), | ||
macos: Some(asset_name.to_string()), | ||
windows: Some(asset_name.to_string()), | ||
}, | ||
}; | ||
|
||
let assets = vec![ | ||
Asset { | ||
id: 1, | ||
name: "1".to_string(), | ||
size: 10, | ||
}, | ||
Asset { | ||
id: 2, | ||
name: "2".to_string(), | ||
size: 50, | ||
}, | ||
Asset { | ||
id: 3, | ||
name: "3".to_string(), | ||
size: 77, | ||
}, | ||
]; | ||
|
||
assert_eq!( | ||
tool_info.select_asset(&assets), | ||
Err(AssetError::NotFound(asset_name.to_string())) | ||
); | ||
} | ||
|
||
#[test] | ||
fn asset_os_selector_unknown() { | ||
let tool_info = ToolInfo { | ||
owner: "owner".to_string(), | ||
repo: "repo".to_string(), | ||
exe_name: "exe".to_string(), | ||
tag: ToolInfoTag::Latest, | ||
asset_name: AssetName { | ||
linux: None, | ||
macos: None, | ||
windows: None, | ||
}, | ||
}; | ||
|
||
assert_eq!( | ||
tool_info.select_asset(&Vec::new()), | ||
Err(AssetError::OsSelectorUnknown) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the documentation fix! 🙏🏻