Skip to content
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

Remove expect #321

Merged
merged 2 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#![deny(clippy::get_unwrap)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::indexing_slicing)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::todo)]

mod config;
mod migration;
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/gog/gog_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ pub fn default_location() -> PathBuf {
#[cfg(target_os = "windows")]
{
let key = "PROGRAMDATA";
let program_data = std::env::var(key).expect("Expected a APPDATA variable to be defined");
let program_data = std::env::var(key).unwrap_or_default();
Path::new(&program_data).join("GOG.com").join("Galaxy")
}
#[cfg(target_family = "unix")]
{
let home = std::env::var("HOME").expect("Expected a home variable to be defined");
let home = std::env::var("HOME").unwrap_or_default();
Path::new(&home).join("Games/gog-galaxy/drive_c/ProgramData/GOG.com/Galaxy")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/itch/itch_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn dbpath_to_game(paths: &DbPaths) -> Option<ItchGame> {
#[cfg(target_family = "unix")]
pub fn get_default_location() -> String {
//If we don't have a home drive we have to just die
let home = std::env::var("HOME").expect("Expected a home variable to be defined");
let home = std::env::var("HOME").unwrap_or_default();
format!("{}/.config/itch/", home)
}

Expand Down
44 changes: 20 additions & 24 deletions src/steam/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ impl ActualSteamCollection {
let key = format!("user-collections.{}", name_to_key(name));
let value = serialize_collection_value(name, ids);
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let since_the_epoch = start.duration_since(UNIX_EPOCH).unwrap_or_default();
let timestamp = since_the_epoch.as_secs();

ActualSteamCollection {
Expand Down Expand Up @@ -129,8 +127,10 @@ pub fn write_collections<S: AsRef<str>>(
save_category(category_key, collections, &mut write_batch)?;

if let Some(path) = get_vdf_path(steam_user_id) {
let content = std::fs::read_to_string(&path).expect("Should be able to read this file");
if let Some(mut vdf_collections) = parse_vdf_collection(content) {
let content = std::fs::read_to_string(&path)
.ok()
.and_then(parse_vdf_collection);
if let Some(mut vdf_collections) = content {
let boilr_keys: Vec<String> = vdf_collections
.keys()
.filter(|k| k.contains(BOILR_TAG))
Expand Down Expand Up @@ -320,7 +320,7 @@ fn get_level_db_location() -> Option<PathBuf> {

fn serialize_collection_value<S: AsRef<str>>(name: S, game_ids: &[usize]) -> String {
let value = ValueCollection::new(name, game_ids);
serde_json::to_string(&value).expect("Should be able to serialize known type")
serde_json::to_string(&value).unwrap_or_default()
}

fn name_to_key<S: AsRef<str>>(name: S) -> String {
Expand Down Expand Up @@ -360,24 +360,20 @@ pub fn write_vdf_collection_to_string<S: AsRef<str>>(
vdf: &HashMap<String, VdfCollection>,
) -> Option<String> {
let input = input.as_ref();
let str = serde_json::to_string(vdf).expect("Should be able to serialize known type");
let encoded_json = format!("\"{}\"", str.replace('\\', "\\\""));
let key = "\t\"user-collections\"\t\t";
if let Some(start_index) = input.find_substring(key) {
let start_index_plus_key = start_index + key.len();
if let Some(line_index) = input.get(start_index_plus_key..).and_then(|i| i.find('\n')) {
let end_index_in_full = line_index + start_index_plus_key;
if let (Some(before), Some(after)) = (
input.get(..start_index_plus_key),
input.get(end_index_in_full..),
) {
let result = format!(
"{}{}{}",
before,
encoded_json,
after
);
return Some(result);
if let Ok(str) = serde_json::to_string(vdf) {
let encoded_json = format!("\"{}\"", str.replace('\\', "\\\""));
let key = "\t\"user-collections\"\t\t";
if let Some(start_index) = input.find_substring(key) {
let start_index_plus_key = start_index + key.len();
if let Some(line_index) = input.get(start_index_plus_key..).and_then(|i| i.find('\n')) {
let end_index_in_full = line_index + start_index_plus_key;
if let (Some(before), Some(after)) = (
input.get(..start_index_plus_key),
input.get(end_index_in_full..),
) {
let result = format!("{}{}{}", before, encoded_json, after);
return Some(result);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/steam/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn get_shortcuts_paths(settings: &SteamSettings) -> eyre::Result<Vec<SteamUs
let folder_path = folder.path();
let folder_str = folder_path
.to_str()
.expect("We just checked that this was there");
.unwrap_or_default();
let path = format!("{}//config//shortcuts.vdf", folder_str);
let shortcuts_path = Path::new(path.as_str());
let folder_string = folder_str.to_string();
Expand Down