Skip to content

Commit

Permalink
Remove unwrap and make pretty connection.json (Not sure why it was no…
Browse files Browse the repository at this point in the history
…t working)
  • Loading branch information
Darcraytore1 committed Aug 7, 2024
1 parent 610d417 commit 968f749
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/core/src/platform/connection_info/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions lib/core/src/platform/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde_json;
use std::env::var;
use std::sync::Arc;
use tokio::sync::Mutex;
use bitflags::bitflags;
Expand All @@ -7,6 +8,7 @@ use std::cmp::PartialEq;


use crate::platform::connection_info::export_file;
use crate::{FunctionResult, __platform_error_result};


pub mod boot;
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit 968f749

Please sign in to comment.