From 99ff863166cfff1848e356bfcf22e34cfcf4de18 Mon Sep 17 00:00:00 2001 From: Natalie Dorshimer Date: Sat, 19 Feb 2022 16:19:48 -0500 Subject: [PATCH] simplify json error --- src/main.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2281693..83fb034 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 @@ -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) } @@ -38,9 +39,12 @@ 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) } @@ -48,6 +52,6 @@ fn get_user_name_from_args() -> String { env::args() .collect::>() .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() }