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 unwraps and create default pretty connection.json #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading