diff --git a/lib/modules/ws2p-v1-legacy/src/ws2p_db.rs b/lib/modules/ws2p-v1-legacy/src/ws2p_db.rs index 0b09fb1f1c79e29d91d712f66b9c5ea20d47062c..8746d8fd80916608b63956c8075e587f5583e93d 100644 --- a/lib/modules/ws2p-v1-legacy/src/ws2p_db.rs +++ b/lib/modules/ws2p-v1-legacy/src/ws2p_db.rs @@ -74,7 +74,7 @@ pub fn get_endpoints( file_path: &Path, ) -> Result<HashMap<NodeFullId, DbEndpoint>, Ws2pPeersDbError> { if file_path.exists() { - let bin_endpoints = durs_common_tools::read_bin_file(file_path)?; + let bin_endpoints = durs_common_tools::fns::bin_file::read_bin_file(file_path)?; if bin_endpoints.is_empty() { Ok(HashMap::new()) } else { @@ -91,7 +91,7 @@ pub fn write_endpoints<S: std::hash::BuildHasher>( endpoints: &HashMap<NodeFullId, DbEndpoint, S>, ) -> Result<(), Ws2pPeersDbError> { let bin_endpoints = bincode::serialize(&endpoints)?; - durs_common_tools::write_bin_file(file_path, &bin_endpoints)?; + durs_common_tools::fns::bin_file::write_bin_file(file_path, &bin_endpoints)?; Ok(()) } diff --git a/lib/modules/ws2p-v1-legacy/src/ws_connections/messages.rs b/lib/modules/ws2p-v1-legacy/src/ws_connections/messages.rs index d64651843cad411c6746336c732ad829621e31a1..08724c4aed5fa786791412b88a35fb82a8bf665c 100644 --- a/lib/modules/ws2p-v1-legacy/src/ws_connections/messages.rs +++ b/lib/modules/ws2p-v1-legacy/src/ws_connections/messages.rs @@ -80,7 +80,7 @@ pub fn ws2p_conn_message_pretreatment( .get_mut(&ws2p_full_id) .expect("WS2P: Fail to get mut ep !"); dal_ep.state = WS2PConnectionState::WSError; - dal_ep.last_check = durs_common_tools::current_timestamp(); + dal_ep.last_check = durs_common_tools::fns::time::current_timestamp(); return WS2PSignal::WSError(ws2p_full_id); } WS2PConnectionMessagePayload::TryToSendConnectMess => { @@ -96,7 +96,7 @@ pub fn ws2p_conn_message_pretreatment( .get_mut(&ws2p_full_id) .expect("WS2P: Fail to get mut ep !"); dal_ep.state = WS2PConnectionState::Unreachable; - dal_ep.last_check = durs_common_tools::current_timestamp(); + dal_ep.last_check = durs_common_tools::fns::time::current_timestamp(); } WS2PConnectionMessagePayload::WebsocketOk(sender) => { ws2p_module.websockets.insert(ws2p_full_id, sender); @@ -119,7 +119,7 @@ pub fn ws2p_conn_message_pretreatment( .get_mut(&ws2p_full_id) .expect("WS2P: Fail to get mut ep !"); dal_ep.state = WS2PConnectionState::Close; - dal_ep.last_check = durs_common_tools::current_timestamp(); + dal_ep.last_check = durs_common_tools::fns::time::current_timestamp(); } } WS2PConnectionMessagePayload::ValidAckMessage(response, new_con_state) => { @@ -230,7 +230,7 @@ pub fn ws2p_conn_message_pretreatment( .get_mut(&ws2p_full_id) .expect("WS2P: Fail to get mut ep !"); dal_ep.state = WS2PConnectionState::Unreachable; - dal_ep.last_check = durs_common_tools::current_timestamp(); + dal_ep.last_check = durs_common_tools::fns::time::current_timestamp(); } } close_connection( diff --git a/lib/modules/ws2p-v1-legacy/src/ws_connections/mod.rs b/lib/modules/ws2p-v1-legacy/src/ws_connections/mod.rs index d7fb34e72b0d1fc79eb0e71f0c905b8a9a0cfb05..dfe35c5ea7bdf01fd84bef4bbf4da5bd781ca923 100644 --- a/lib/modules/ws2p-v1-legacy/src/ws_connections/mod.rs +++ b/lib/modules/ws2p-v1-legacy/src/ws_connections/mod.rs @@ -156,7 +156,7 @@ pub fn close_connection( | WS2PCloseConnectionReason::Unknow => { if let Some(dal_ep) = ws2p_module.ws2p_endpoints.get_mut(ws2p_full_id) { dal_ep.state = WS2PConnectionState::Close; - dal_ep.last_check = durs_common_tools::current_timestamp(); + dal_ep.last_check = durs_common_tools::fns::time::current_timestamp(); } } } diff --git a/lib/tools/common-tools/src/fns/bin_file.rs b/lib/tools/common-tools/src/fns/bin_file.rs new file mode 100644 index 0000000000000000000000000000000000000000..1168d64e76b0d971890a6be89230ed71b8646f64 --- /dev/null +++ b/lib/tools/common-tools/src/fns/bin_file.rs @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Common rust functions for read/write binary files. + +use std::fs::File; +use std::io::Read; +use std::io::Write; +use std::path::Path; + +/// Read bin file +pub fn read_bin_file(file_path: &Path) -> Result<Vec<u8>, std::io::Error> { + let mut file = File::open(file_path)?; + if file.metadata()?.len() == 0 { + Ok(vec![]) + } else { + let mut bin_datas = Vec::new(); + file.read_to_end(&mut bin_datas)?; + + Ok(bin_datas) + } +} + +/// Write bin file +pub fn write_bin_file(file_path: &Path, datas: &[u8]) -> Result<(), std::io::Error> { + let mut file = File::create(file_path)?; + file.write_all(&datas[..])?; + + Ok(()) +} diff --git a/lib/tools/common-tools/src/fns/mod.rs b/lib/tools/common-tools/src/fns/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..ca694e4e23eb85f81aaff1de39b9a467c682c3e5 --- /dev/null +++ b/lib/tools/common-tools/src/fns/mod.rs @@ -0,0 +1,20 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Common rust functions for DURS project. + +pub mod bin_file; +pub mod str_escape; +pub mod time; diff --git a/lib/tools/common-tools/src/fns/str_escape.rs b/lib/tools/common-tools/src/fns/str_escape.rs new file mode 100644 index 0000000000000000000000000000000000000000..a18b0da906c6b7b0adea1549e121317849d8bb86 --- /dev/null +++ b/lib/tools/common-tools/src/fns/str_escape.rs @@ -0,0 +1,46 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Common rust functions to (un)escape strings. + +/// Unescape backslash +pub fn unescape_str(source: &str) -> String { + let mut previous_char = None; + let mut str_result = String::with_capacity(source.len()); + + for current_char in source.chars() { + if previous_char.is_some() && previous_char.unwrap() == '\\' { + match current_char { + '\\' => {} // Do nothing + _ => str_result.push(current_char), + } + } else { + str_result.push(current_char); + } + previous_char = Some(current_char); + } + + str_result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + pub fn test_unescape_str() { + assert_eq!("\\".to_owned(), unescape_str("\\\\")); + } +} diff --git a/lib/tools/common-tools/src/fns/time.rs b/lib/tools/common-tools/src/fns/time.rs new file mode 100644 index 0000000000000000000000000000000000000000..e3adf4b036b04f637dadad7edc223d22bb548aca --- /dev/null +++ b/lib/tools/common-tools/src/fns/time.rs @@ -0,0 +1,28 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Common rust functions for manage time. + +use std::time::SystemTime; +use std::time::UNIX_EPOCH; + +#[inline] +/// Get current timestamp in seconds +pub fn current_timestamp() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("SystemTime::duration_since failed") + .as_secs() +} diff --git a/lib/tools/common-tools/src/lib.rs b/lib/tools/common-tools/src/lib.rs index be0eee5c79ed280a92f9d2804510bb618aa28e8f..56b827acd788c980da9fa488a270efef4ccd4525 100644 --- a/lib/tools/common-tools/src/lib.rs +++ b/lib/tools/common-tools/src/lib.rs @@ -26,87 +26,6 @@ unused_import_braces )] -use std::fs::File; -use std::io::Read; +pub mod fns; +pub mod macros; use std::io::Write; -use std::path::Path; -use std::time::SystemTime; -use std::time::UNIX_EPOCH; - -/// Interrupts the program and log error message -/// WARNING: this macro must not be called before the logger is initialized ! -#[macro_export] -macro_rules! fatal_error { - ($msg:expr) => ({ - error!("{}", &dbg!($msg)); - panic!($msg); - }); - ($msg:expr,) => ({ - error!("{}", &dbg!($msg)); - panic!($msg); - }); - ($fmt:expr, $($arg:tt)+) => ({ - error!("{}", dbg!(format!($fmt, $($arg)+))); - panic!($fmt, $($arg)+); - }); -} - -#[inline] -/// Get current timestamp in seconds -pub fn current_timestamp() -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("SystemTime::duration_since failed") - .as_secs() -} - -/// Read bin file -pub fn read_bin_file(file_path: &Path) -> Result<Vec<u8>, std::io::Error> { - let mut file = File::open(file_path)?; - if file.metadata()?.len() == 0 { - Ok(vec![]) - } else { - let mut bin_datas = Vec::new(); - file.read_to_end(&mut bin_datas)?; - - Ok(bin_datas) - } -} - -/// Write bin file -pub fn write_bin_file(file_path: &Path, datas: &[u8]) -> Result<(), std::io::Error> { - let mut file = File::create(file_path)?; - file.write_all(&datas[..])?; - - Ok(()) -} - -/// Unescape backslash -pub fn unescape_str(source: &str) -> String { - let mut previous_char = None; - let mut str_result = String::with_capacity(source.len()); - - for current_char in source.chars() { - if previous_char.is_some() && previous_char.unwrap() == '\\' { - match current_char { - '\\' => {} // Do nothing - _ => str_result.push(current_char), - } - } else { - str_result.push(current_char); - } - previous_char = Some(current_char); - } - - str_result -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - pub fn test_unescape_str() { - assert_eq!("\\".to_owned(), unescape_str("\\\\")); - } -} diff --git a/lib/tools/common-tools/src/macros/fatal_error.rs b/lib/tools/common-tools/src/macros/fatal_error.rs new file mode 100644 index 0000000000000000000000000000000000000000..f1e51b1a8e02433b529c8a8570d2396bc41a02f4 --- /dev/null +++ b/lib/tools/common-tools/src/macros/fatal_error.rs @@ -0,0 +1,34 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Fatal error macro for DURS project. + +/// Interrupts the program and log error message +/// WARNING: this macro must not be called before the logger is initialized ! +#[macro_export] +macro_rules! fatal_error { + ($msg:expr) => ({ + error!("{}", &dbg!($msg)); + panic!($msg); + }); + ($msg:expr,) => ({ + error!("{}", &dbg!($msg)); + panic!($msg); + }); + ($fmt:expr, $($arg:tt)+) => ({ + error!("{}", dbg!(format!($fmt, $($arg)+))); + panic!($fmt, $($arg)+); + }); +} diff --git a/lib/tools/common-tools/src/macros/mod.rs b/lib/tools/common-tools/src/macros/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..9a0845950bf3af18b901c5696f509aaf1b26966b --- /dev/null +++ b/lib/tools/common-tools/src/macros/mod.rs @@ -0,0 +1,18 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Common rust macros for DURS project. + +pub mod fatal_error; diff --git a/lib/tools/documents/src/parsers/transactions.rs b/lib/tools/documents/src/parsers/transactions.rs index ceea9c1f19a04927f49f31be05bf68add13a9ec0..a7e7d95085713395c895b87f3acb1226d61e8f46 100644 --- a/lib/tools/documents/src/parsers/transactions.rs +++ b/lib/tools/documents/src/parsers/transactions.rs @@ -64,7 +64,7 @@ pub fn parse_json_transaction( .iter() .map(|i| TransactionOutput::from_str(i)) .collect::<Result<Vec<TransactionOutput>, TextDocumentParseError>>()?, - comment: &durs_common_tools::unescape_str(get_str(json_tx, "comment")?), + comment: &durs_common_tools::fns::str_escape::unescape_str(get_str(json_tx, "comment")?), hash: if let Some(hash_str) = get_optional_str(json_tx, "hash")? { Some(Hash::from_hex(hash_str)?) } else {