Skip to content

Commit

Permalink
simplify json error
Browse files Browse the repository at this point in the history
  • Loading branch information
natdorshimer committed Feb 19, 2022
1 parent 870c75f commit 99ff863
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::env;
use serde::{Deserialize};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;

#[derive(Deserialize)]
#[derive(Deserialize, Serialize, Default)]
struct SlippiPaths {
user_json_folder_path: String,
slippi_folder_path: String
Expand All @@ -18,7 +18,8 @@ fn main() -> std::io::Result<()> {
let new_user_file_path = format!("{}/user.json", paths.slippi_folder_path);
let user_json_file_path = format!("{}/{}.json", paths.user_json_folder_path, user_json_name);

let new_user_json = std::fs::read_to_string(user_json_file_path).expect(&format!("Could not load user json with name {}", user_json_name));
let new_user_json = std::fs::read_to_string(user_json_file_path).unwrap();

create_new_file_with_contents(&new_user_file_path, &new_user_json)
}

Expand All @@ -38,16 +39,19 @@ fn create_new_file_with_contents(path: &str, contents: &str) -> std::io::Result<
}

fn get_slippi_paths(json_file_path: &str) -> SlippiPaths {
let file_paths_json = fs::read_to_string(json_file_path).expect("filepaths.json is required and must be found in same directory that the executable is running in");
let expected_json_format = "{ \"user_json_folder_path\": string, \"slippi_folder_path\": string }";
let parsing_error_msg = format!("Could not parse from {json_file_path}. Expected format: {expected_json_format}");
let file_paths_json =
fs::read_to_string(json_file_path)
.expect("filepaths.json is required and must be found in same directory that the executable is running in");

let json_format = serde_json::to_string(&SlippiPaths::default()).unwrap();
let parsing_error_msg = format!("Could not parse json. Expected format: {json_format}");
serde_json::from_str(&file_paths_json).expect(&parsing_error_msg)
}

fn get_user_name_from_args() -> String {
env::args()
.collect::<Vec<String>>()
.get(1)
.expect("Command line argument containing user name is required. Ex: ./change-slippi-user my-secondary")
.expect("Command line argument container user is required. Ex: `$ ./change-slippi-user my-secondary`")
.clone()
}

0 comments on commit 99ff863

Please sign in to comment.