-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve broker + Update core to 0.2.0 (#205)
* improve broker * import old files for futur devs * update core 0.2.0 and prepare 0.5.4
- Loading branch information
1 parent
2a1beb4
commit 5f46914
Showing
7 changed files
with
571 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// --- | ||
// Documentation | ||
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/ | ||
// --- | ||
|
||
#[derive(Debug)] | ||
pub enum ErrorType { | ||
// COVER:REQ_CONN_INFO_0030_00 | ||
ContentBadFormat, | ||
// COVER:REQ_CONN_INFO_0040_00 | ||
MandatoryFieldMissing, | ||
// COVER:REQ_CONN_INFO_0050_00 | ||
FileDoesNotExist, | ||
} | ||
|
||
/// Error for connection info crate | ||
#[derive(Debug)] | ||
pub struct Error { | ||
/// Type of the error | ||
pub err_type: ErrorType, | ||
/// Platform error message | ||
pub plt_error: crate::Error, | ||
} | ||
|
||
impl Error { | ||
pub fn new(err_type: ErrorType, plt_error: crate::Error) -> Self { | ||
Self { | ||
err_type: err_type, | ||
plt_error: plt_error | ||
} | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! connection_info_content_bad_format_error { | ||
($msg:expr) => { | ||
super::Error::new ( | ||
crate::platform::connection_info::error::ErrorType::ContentBadFormat, | ||
crate::Error::new(file!(), line!(), $msg.to_string()) | ||
) | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! connection_info_mandatory_field_missing_error { | ||
($msg:expr) => { | ||
super::Error::new ( | ||
crate::platform::connection_info::error::ErrorType::MandatoryFieldMissing, | ||
crate::Error::new(file!(), line!(), $msg.to_string()) | ||
) | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! connection_info_file_does_not_exist_error_result { | ||
($msg:expr) => { | ||
Err( | ||
super::Error::new ( | ||
crate::platform::connection_info::error::ErrorType::FileDoesNotExist, | ||
crate::Error::new(file!(), line!(), $msg.to_string()) | ||
) | ||
) | ||
}; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::env; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
|
||
use super::Info; | ||
use super::Error; | ||
use super::serde; | ||
use crate::FunctionResult; | ||
|
||
use crate::__platform_error; | ||
use crate::connection_info_content_bad_format_error; | ||
use crate::connection_info_file_does_not_exist_error_result; | ||
|
||
// --- | ||
// Documentation | ||
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/ | ||
// --- | ||
|
||
/// Return the system path of the connection.json file | ||
/// | ||
/// COVER:PLATF_00001_00 - File System Paths | ||
/// | ||
pub fn system_file_path() -> PathBuf { | ||
// Define the paths | ||
let filename = "connection.json"; | ||
let unix_path = | ||
PathBuf::from("/etc/panduza").join(filename); | ||
let windows_path = | ||
PathBuf::from(dirs::public_dir().unwrap()).join("panduza").join(filename); | ||
|
||
// Return the file path depeding on the OS | ||
match env::consts::OS { | ||
"windows" => { return windows_path; } | ||
"unix" => { return unix_path; } | ||
"linux" => { return unix_path; } | ||
_ => { | ||
tracing::warn!("Unsupported system ({:?}) but try with unix path anyway !", env::consts::OS); | ||
return unix_path; | ||
} | ||
} | ||
} | ||
|
||
/// Create a new Info object from a JSON file | ||
/// | ||
/// COVER:PLATF_00002_00 - File is JSON | ||
/// | ||
pub async fn import_file(file_path: PathBuf) -> Result<Info, Error> { | ||
|
||
// Check if the file exists | ||
if !file_path.exists() { | ||
return connection_info_file_does_not_exist_error_result!(file_path.to_str().unwrap()); | ||
} | ||
|
||
// Try to read the file content | ||
tokio::fs::read_to_string(&file_path).await | ||
.map_err(|e| connection_info_content_bad_format_error!(e.to_string().as_str()) ) | ||
.and_then(|v| serde::deserialize(v.as_str()) ) | ||
} | ||
|
||
/// Create a new Info file from a info data | ||
/// | ||
/// COVER:PLATF_00002_00 - File is JSON | ||
/// | ||
pub fn export_file(info: &Info) -> FunctionResult { | ||
|
||
// Create the file directory | ||
let mut filedir_path = std::path::PathBuf::from(info.file_path.clone()); | ||
filedir_path.pop(); | ||
std::fs::create_dir_all(filedir_path) | ||
.map_err(|e| __platform_error!(e.to_string()) )?; | ||
|
||
// Write new file | ||
let mut file = std::fs::File::create(&info.file_path) | ||
.map_err(|e| __platform_error!(e.to_string()) )?; | ||
|
||
// Create the JSON string | ||
let json_string = serde::serialize(info)?; | ||
|
||
// Write file | ||
file.write_all(json_string.as_bytes()) | ||
.map_err(|e| __platform_error!(e.to_string()) )?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::file::system_file_path; | ||
|
||
// --- | ||
// Documentation | ||
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/ | ||
// --- | ||
|
||
/// This object is responsible of the connection information | ||
/// | ||
/// It must manage the data but also the file used to store them | ||
/// | ||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
pub struct Info { | ||
// Path of the file | ||
pub file_path: String, | ||
|
||
// broker info | ||
pub broker_addr: String, | ||
pub broker_port: u16, | ||
|
||
// credential | ||
pub credentials_user: Option<String>, | ||
pub credentials_pass: Option<String>, | ||
|
||
// Platform info | ||
pub platform_name: String, | ||
|
||
// Services info | ||
pub services_retry_delay: u32, | ||
pub services_enable_plbd: bool, | ||
} | ||
|
||
impl Info { | ||
|
||
/// Create a new Info object with default values | ||
/// | ||
pub fn default() -> Self { | ||
Self { | ||
file_path: system_file_path().to_str().unwrap().to_string(), | ||
broker_addr: "localhost".to_string(), | ||
broker_port: 1883, | ||
credentials_user: None, | ||
credentials_pass: None, | ||
platform_name: "panduza_platform".to_string(), | ||
services_retry_delay: 1, | ||
services_enable_plbd: false, | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
mod info; | ||
mod file; | ||
mod error; | ||
mod serde; | ||
|
||
use std::path::PathBuf; | ||
use crate::FunctionResult; | ||
|
||
// --- | ||
// Documentation | ||
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/ | ||
// --- | ||
|
||
pub type Info = info::Info; | ||
pub type Error = error::Error; | ||
pub type ErrorType = error::ErrorType; | ||
|
||
#[inline(always)] | ||
pub fn system_file_path() -> PathBuf { | ||
file::system_file_path() | ||
} | ||
|
||
#[inline(always)] | ||
pub async fn import_file(file_path: PathBuf) -> Result<Info, Error> { | ||
file::import_file(file_path).await | ||
} | ||
|
||
#[inline(always)] | ||
pub fn export_file(info: &Info) -> FunctionResult { | ||
file::export_file(info) | ||
} |
Oops, something went wrong.