utils_box/
lib.rs

1//! # Summary
2//! A toolbox library that holds a useful collection of small unitilies written in Rust that make our life easier when writting Rust applications.
3//!
4//! # Utilities provided:
5//!
6//! ## Mathematics
7//! A collection of useful methematic methods used in various DSP and other applications
8//!
9//! ## Archives
10//! Extract files from Tar, Gz and Zip Files
11//!
12//! Mininal Example:
13//! ```ignore
14//! let archive: PathBuf = std::env::current_exe()
15//!     .unwrap()
16//!     .parent()
17//!     .unwrap()
18//!     .join("test_archive.tar.gz");
19//!
20//! let file: PathBuf = "treasure.hex".into();
21//!
22//! let destination: PathBuf = std::env::current_exe()
23//!     .unwrap()
24//!     .parent()
25//!     .unwrap();
26//!
27//! archives::extract_file(archive, ArchiveType::Gz, file, destination).unwrap();
28//!
29//! ```
30//!
31//! ## Bits
32//! Convertions between different representations of raw bit streams
33//!
34//! Mininal Example:
35//! ```ignore
36//! let received_bit_stream: u64 = 0b110101000100111010110;
37//!
38//! let bytes = bits::bits_to_vec(received_bit_stream,21);
39//!
40//! println!("Received bit stream: {} ", bits::bit_vec_to_hex_string(&bytes));
41//!
42//! ```
43//!
44//! ## Config
45//! Manipulate INI-style configuration files by checking for changes, updates etc
46//!
47//! Mininal Example:
48//! ```ignore
49//!     let mut config_changes = ini_compare(
50//!         &old_config_path.to_path_buf(),
51//!         &new_config_path.to_path_buf(),
52//!     )
53//!     .unwrap();
54//!
55//!    println!("{:#?}", config_changes);
56//!
57//! ```
58//!
59//! ## Debug
60//! Print in log or stdout debug information from vectors, hashmaps in a human readable way.
61//! Pause execution at specific moments to make debugging easier.
62//!
63//! Mininal Example:
64//! ```ignore
65//!     
66//!     // Complex data operations before [..]
67//!
68//!     let data: Vec<f64> = (0..100).iter().map(|&x| x * f64::PI).collect();
69//!
70//!     // Print debug information from data vector
71//!     vector_display(&data[0..10],"Mult_PI", IdxMode::Based1);
72//!     // Pause execution to check values
73//!     pause();
74//!
75//!     // Complex data operations after [..]
76//!
77//! ```
78//!
79//! ## Logger
80//! Initialize terminal and file loggers fast. Macros for log printing to either log or stdout (if a global logger is not initialized)
81//!
82//! Mininal Example:
83//! ```ignore
84//!     log_info!("INFO Test TO PRINTLN!");
85//!     log_debug!("DEBUG Test TO PRINTLN!");
86//!
87//!     terminal_logger_init(LevelFilter::Debug);
88//!
89//!     log_info!("INFO Test TO LOGGER!");
90//!     log_debug!("DEBUG Test TO LOGGER!");
91//!
92//! ```
93//!
94//! ## Paths
95//! Search paths for a specific file in directories with known or unknown paths
96//!
97//! Mininal Example:
98//! ```ignore
99//!     let paths = IncludePathsBuilder::new()
100//!             .include_exe_dir()
101//!             .include_known("/home/user/")
102//!             .include_unknown("utils-box")
103//!             .build();
104//!
105//!     let pattern = "test_*.tar";
106//!
107//!     let file_found_in = paths.search_glob(pattern);
108//!
109//! ```
110//!
111//! ## Stopwatch and Timekeper
112//! Keep track of execution times in various points in binaries. Print records.
113//!
114//! Minimal Example:
115//! ```ignore
116//!    let mut s = TimeKeeper::init();
117//!    let mut t = TimeKeeper::init();
118//!
119//!    s.totals();
120//!
121//!    s.lap("init");
122//!
123//!    for _ in 0..5 {
124//!        std::thread::sleep(Duration::from_millis(5));
125//!        s.lap("loop");
126//!        t.lap("loop");
127//!    }
128//!    s.lap_totals("loop");
129//!    std::thread::sleep(Duration::from_millis(1234));
130//!    s.lap("after");
131//!
132//!    s.totals();
133//!    t.totals();
134//!
135//!    s.merge(t);
136//!
137//!    s.totals();
138//!
139//! ```
140//!
141//! ## Versions
142//! version parser from strings using the `semver.org` notations
143//!
144//! Mininal Example:
145//! ```ignore
146//!    let version = "0.9.2-1e341234";
147//!
148//!     let mut expected = Version::new(0, 9, 2);
149//!     expected.pre = Prerelease::new("1e341234").unwrap();
150//!
151//!     assert_eq!(semver_parse(version).unwrap(), expected);
152//!
153//! ```
154//!
155//! ## SSH Client
156//! Connect via SSH to a server to perform commands, upload & download files
157//!
158//! Mininal Example:
159//! ```ignore
160//!     let ssh = SshClient::local("user".to_string(), "1234".to_string()).unwrap();
161//!
162//!     let stdout = ssh.execute_cmd("ls").unwrap();
163//!
164//!     println!("{:?}", stdout);
165//!
166//! ```
167//!
168//! ## TCP Client
169//! Connect via TCP to a socket to send and receive data
170//!
171//! Mininal Example:
172//! ```ignore
173//!     let mut tcp_client = TcpClient::new("192.168.1.17".to_string(), 36457)?;
174//!
175//!     let data: Vec<u8> = vec![8, 30, 15, 30, 5, 19, 0, 7];
176//!
177//!     tcp_client.send(&data)?;
178//!
179//!     // Block and wait for response
180//!     let resp = tcp_client.receive()?;
181//!
182//!      println!("{:?}", resp);
183//!
184//! ```
185//!
186//! ## TCP Client
187//! Connect via UDP to a socket to send and receive data
188//!
189//! Mininal Example:
190//! ```ignore
191//!     let mut udp =
192//!             UdpClient::new("0.0.0.0".to_string(), "192.168.1.31".to_string(), 6123).unwrap();
193//!
194//!     udp.send(b"\r").unwrap();
195//!
196//!     // Block and wait for response
197//!     let data = udp.receive().unwrap();
198//!
199//!     println!("{:?} => {}", data, String::from_utf8_lossy(&data));
200//!
201//! ```
202//!
203//! ## ZMQ Client
204//! Connect to a ZMQ server to send and receive data
205//!
206//! Mininal Example:
207//! ```ignore
208//!     let zmq_client = ZmqClient::new(("192.168.1.17".to_string(), 36457)?;
209//!
210//!     let data: Vec<u8> = vec![8, 30, 15, 30, 5, 19, 0, 7];
211//!
212//!     zmq_client.send(&data)?;
213//!
214//!     // Block and wait for response
215//!     let resp = zmq_client.receive()?;
216//!
217//!     println!("{:?}", resp);
218//!
219//! ```
220//!
221
222#[cfg(feature = "archives")]
223pub use utils_box_archives::*;
224#[cfg(feature = "bits")]
225pub use utils_box_bits::*;
226#[cfg(feature = "config")]
227pub use utils_box_config::*;
228#[cfg(feature = "connections")]
229pub use utils_box_connections::*;
230#[cfg(feature = "debug")]
231pub use utils_box_debug::*;
232#[cfg(feature = "linux")]
233pub use utils_box_linux::*;
234#[cfg(feature = "logger")]
235pub use utils_box_logger::*;
236#[cfg(feature = "mathematics")]
237pub use utils_box_mathematics::*;
238#[cfg(feature = "pathfinder")]
239pub use utils_box_pathfinder::*;
240#[cfg(feature = "stopwatch")]
241pub use utils_box_stopwatch::*;
242#[cfg(feature = "versions")]
243pub use utils_box_versions::*;
244
245#[cfg(test)]
246mod tests {
247    use crate::paths::IncludePathsBuilder;
248    use crate::stopwatch::TimeKeeper;
249
250    #[test]
251    fn utils_box_smoke_test() {
252        let mut timer = TimeKeeper::init();
253
254        let paths = IncludePathsBuilder::new()
255            .include_exe_dir()
256            .include_unknown("utils-box-archives/")
257            .build();
258
259        let archive = paths.search_glob("test_archives.tar");
260
261        assert_eq!(1, archive.len());
262
263        timer.lap("smoke_test1");
264    }
265}