-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#83] Create 'AssetError' and refactor 'select_asset' function to ret…
…urn it (#97) Resolves #83 Refactored `ToolInfo.select_asset(&assets)` to return a custom `AssetError` error instead of a string and added tests for the function. The `AssetError` enum has two possible values: - `NameUnknown`: this is when `AssetName` does not know what the asset name is for a specific OS the user uses. - `NotFound`: this is when the `AssetName` is not in the `Assets` slice passed into the `select_asset(&assets)` method. Implemented `std::fmt::Display` trait for `AssetError` and added tests for it. ### Additional tasks - [x] Documentation for changes provided/changed - [x] Tests added Co-authored-by: Dmitrii Kovanikov <[email protected]>
- Loading branch information
Showing
4 changed files
with
146 additions
and
9 deletions.
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
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