From 968f749cbc1311b0a37986b731b3a56ef39bba55 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Aug 2024 14:14:23 +0200 Subject: [PATCH] Remove unwrap and make pretty connection.json (Not sure why it was not working) --- lib/core/src/platform/connection_info/file.rs | 10 +++++++++- .../boot/connection_info_loading_process/mod.rs | 7 ++++++- lib/core/src/platform/services/mod.rs | 13 +++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/core/src/platform/connection_info/file.rs b/lib/core/src/platform/connection_info/file.rs index 15a2ea7a..30f7e390 100644 --- a/lib/core/src/platform/connection_info/file.rs +++ b/lib/core/src/platform/connection_info/file.rs @@ -2,6 +2,8 @@ use std::env; use std::io::Write; use std::path::PathBuf; +use serde_json::json; + use super::Info; use super::Error; use super::serde; @@ -76,8 +78,14 @@ pub fn export_file(info: &Info) -> FunctionResult { // Create the JSON string let json_string = serde::serialize(info)?; + // Transform the json string in pretty json string + let json_value: serde_json::Value = serde_json::from_str(&json_string) + .map_err(|e| __platform_error!(e.to_string()))?; + let pretty_json_string = serde_json::to_string_pretty(&json_value) + .map_err(|e| __platform_error!(e.to_string()) )?; + // Write file - file.write_all(json_string.as_bytes()) + file.write_all(pretty_json_string.as_bytes()) .map_err(|e| __platform_error!(e.to_string()) )?; Ok(()) diff --git a/lib/core/src/platform/services/boot/connection_info_loading_process/mod.rs b/lib/core/src/platform/services/boot/connection_info_loading_process/mod.rs index 4375bb50..cc556bcc 100644 --- a/lib/core/src/platform/services/boot/connection_info_loading_process/mod.rs +++ b/lib/core/src/platform/services/boot/connection_info_loading_process/mod.rs @@ -1,4 +1,6 @@ use std::io::{self, Read}; +use crate::platform_error; +use crate::platform_error_result; use crate::FunctionResult; use crate::platform::services::Services; @@ -62,7 +64,10 @@ fn ask_user_about_default_connection_info_creation(services: &mut Services) // Get input from user let mut input = [0; 1]; - io::stdin().read(&mut input).unwrap(); + if let Err(err) = io::stdin().read(&mut input) { + return __platform_error_result!(format!("Read failed : {:?}", err)); + } + let char = input[0] as char; // Check if user answer Yes diff --git a/lib/core/src/platform/services/mod.rs b/lib/core/src/platform/services/mod.rs index ea905ed6..e017b952 100644 --- a/lib/core/src/platform/services/mod.rs +++ b/lib/core/src/platform/services/mod.rs @@ -1,4 +1,5 @@ use serde_json; +use std::env::var; use std::sync::Arc; use tokio::sync::Mutex; use bitflags::bitflags; @@ -7,6 +8,7 @@ use std::cmp::PartialEq; use crate::platform::connection_info::export_file; +use crate::{FunctionResult, __platform_error_result}; pub mod boot; @@ -189,10 +191,17 @@ impl Services { /// Set the default connection info /// - pub fn generate_default_connection_info(&mut self) -> Result<(), std::io::Error> { + pub fn generate_default_connection_info(&mut self) -> FunctionResult { self.connection_info = Some(ConnectionInfo::default()); - export_file(self.connection_info.as_ref().unwrap()).unwrap(); + match self.connection_info.as_ref() { + None => return __platform_error_result!("Default ConnectionInfo is None ???"), + Some(connection_info_ref) => { + if let Err(err) = export_file(connection_info_ref) { + return __platform_error_result!(format!("Export file failed : {:?}", err)); + } + } + } Ok(()) }