|
| 1 | +[](https://coveralls.io/github/klispap/utils-box?branch=main) |
| 2 | + |
| 3 | +# Summary |
| 4 | +A toolbox library that holds a useful collection of small unitilies written in Rust that make our life easier when writting Rust applications. |
| 5 | + |
| 6 | +# Utilities provided: |
| 7 | + |
| 8 | +## Mathematics |
| 9 | +A collection of useful methematic methods used in various DSP and other applications |
| 10 | + |
| 11 | +## Archives |
| 12 | +Extract files from Tar, Gz and Zip Files |
| 13 | + |
| 14 | +Mininal Example: |
| 15 | +```rust |
| 16 | +let archive: PathBuf = std::env::current_exe() |
| 17 | + .unwrap() |
| 18 | + .parent() |
| 19 | + .unwrap() |
| 20 | + .join("test_archive.tar.gz"); |
| 21 | + |
| 22 | +let file: PathBuf = "treasure.hex".into(); |
| 23 | + |
| 24 | +let destination: PathBuf = std::env::current_exe() |
| 25 | + .unwrap() |
| 26 | + .parent() |
| 27 | + .unwrap(); |
| 28 | + |
| 29 | +archives::extract_file(archive, ArchiveType::Gz, file, destination).unwrap(); |
| 30 | + |
| 31 | +``` |
| 32 | + |
| 33 | +## Bits |
| 34 | +Convertions between different representations of raw bit streams |
| 35 | + |
| 36 | +Mininal Example: |
| 37 | +```rust |
| 38 | +let received_bit_stream: u64 = 0b110101000100111010110; |
| 39 | + |
| 40 | +let bytes = bits::bits_to_vec(received_bit_stream,21); |
| 41 | + |
| 42 | +println!("Received bit stream: {} ", bits::bit_vec_to_hex_string(&bytes)); |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +## Config |
| 47 | +Manipulate INI-style configuration files by checking for changes, updates etc |
| 48 | + |
| 49 | +Mininal Example: |
| 50 | +```rust |
| 51 | + let mut config_changes = ini_compare( |
| 52 | + &old_config_path.to_path_buf(), |
| 53 | + &new_config_path.to_path_buf(), |
| 54 | + ) |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + println!("{:#?}", config_changes); |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +## Logger |
| 62 | +Initialize terminal and file loggers fast. Macros for log printing to either log or stdout (if a global logger is not initialized) |
| 63 | + |
| 64 | +Mininal Example: |
| 65 | +```rust |
| 66 | + log_info!("INFO Test TO PRINTLN!"); |
| 67 | + log_debug!("DEBUG Test TO PRINTLN!"); |
| 68 | + |
| 69 | + terminal_logger_init(LevelFilter::Debug); |
| 70 | + |
| 71 | + log_info!("INFO Test TO LOGGER!"); |
| 72 | + log_debug!("DEBUG Test TO LOGGER!"); |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +## Paths |
| 77 | +Search paths for a specific file in directories with known or unknown paths |
| 78 | + |
| 79 | +Mininal Example: |
| 80 | +```rust |
| 81 | + let paths = IncludePathsBuilder::new() |
| 82 | + .include_exe_dir() |
| 83 | + .include_known("/home/user/") |
| 84 | + .include_unknown("utils-box") |
| 85 | + .build(); |
| 86 | + |
| 87 | + let pattern = "test_*.tar"; |
| 88 | + |
| 89 | + let file_found_in = paths.search_glob(pattern); |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +## Stopwatch and Timekeper |
| 94 | +Keep track of execution times in various points in binaries. Print records. |
| 95 | + |
| 96 | +Minimal Example: |
| 97 | +```rust |
| 98 | + let mut s = TimeKeeper::init(); |
| 99 | + let mut t = TimeKeeper::init(); |
| 100 | + |
| 101 | + s.totals(); |
| 102 | + |
| 103 | + s.lap("init"); |
| 104 | + |
| 105 | + for _ in 0..5 { |
| 106 | + std::thread::sleep(Duration::from_millis(5)); |
| 107 | + s.lap("loop"); |
| 108 | + t.lap("loop"); |
| 109 | + } |
| 110 | + s.lap_totals("loop"); |
| 111 | + std::thread::sleep(Duration::from_millis(1234)); |
| 112 | + s.lap("after"); |
| 113 | + |
| 114 | + s.totals(); |
| 115 | + t.totals(); |
| 116 | + |
| 117 | + s.merge(t); |
| 118 | + |
| 119 | + s.totals(); |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +## Versions |
| 124 | +version parser from strings using the `semver.org` notations |
| 125 | + |
| 126 | +Mininal Example: |
| 127 | +```rust |
| 128 | + let version = "0.9.2-1e341234"; |
| 129 | + |
| 130 | + let mut expected = Version::new(0, 9, 2); |
| 131 | + expected.pre = Prerelease::new("1e341234").unwrap(); |
| 132 | + |
| 133 | + assert_eq!(semver_parse(version).unwrap(), expected); |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +## SSH Client |
| 138 | +Connect via SSH to a server to perform commands, upload & download files |
| 139 | + |
| 140 | +Mininal Example: |
| 141 | +```rust |
| 142 | + let ssh = SshClient::local("user".to_string(), "1234".to_string()).unwrap(); |
| 143 | + |
| 144 | + let stdout = ssh.execute_cmd("ls").unwrap(); |
| 145 | + |
| 146 | + println!("{:?}", stdout); |
| 147 | + |
| 148 | +``` |
| 149 | + |
| 150 | +## TCP Client |
| 151 | +Connect via TCP to a socket to send and receive data |
| 152 | + |
| 153 | +Mininal Example: |
| 154 | +```rust |
| 155 | + let mut tcp_client = TcpClient::new("192.168.1.17".to_string(), 36457)?; |
| 156 | + |
| 157 | + let data: Vec<u8> = vec![8, 30, 15, 30, 5, 19, 0, 7]; |
| 158 | + |
| 159 | + tcp_client.send(&data)?; |
| 160 | + |
| 161 | + // Block and wait for response |
| 162 | + let resp = tcp_client.receive()?; |
| 163 | + |
| 164 | + println!("{:?}", resp); |
| 165 | + |
| 166 | +``` |
| 167 | + |
| 168 | +## TCP Client |
| 169 | +Connect via UDP to a socket to send and receive data |
| 170 | + |
| 171 | +Mininal Example: |
| 172 | +```rust |
| 173 | + let mut udp = |
| 174 | + UdpClient::new("0.0.0.0".to_string(), "192.168.1.31".to_string(), 6123).unwrap(); |
| 175 | + |
| 176 | + udp.send(b"\r").unwrap(); |
| 177 | + |
| 178 | + // Block and wait for response |
| 179 | + let data = udp.receive().unwrap(); |
| 180 | + |
| 181 | + println!("{:?} => {}", data, String::from_utf8_lossy(&data)); |
| 182 | + |
| 183 | +``` |
| 184 | + |
| 185 | +## ZMQ Client |
| 186 | +Connect to a ZMQ server to send and receive data |
| 187 | + |
| 188 | +Mininal Example: |
| 189 | +```rust |
| 190 | + let zmq_client = ZmqClient::new(("192.168.1.17".to_string(), 36457)?; |
| 191 | + |
| 192 | + let data: Vec<u8> = vec![8, 30, 15, 30, 5, 19, 0, 7]; |
| 193 | + |
| 194 | + zmq_client.send(&data)?; |
| 195 | + |
| 196 | + // Block and wait for response |
| 197 | + let resp = zmq_client.receive()?; |
| 198 | + |
| 199 | + println!("{:?}", resp); |
| 200 | + |
| 201 | +``` |
| 202 | + |
| 203 | +# Tips for resolving Ubuntu 22.04 build issues: |
| 204 | + |
| 205 | +1) Make sure you have the following system-level dependencies installed: |
| 206 | + ``` |
| 207 | + sudo apt install pkg-config build-essential fontconfig libfontconfig1-dev |
| 208 | + ``` |
| 209 | +
|
| 210 | +2) Verify that `pkg-config` can detect `libstdc++` properly: |
| 211 | + ``` |
| 212 | + pkg-config --libs libstdc++ |
| 213 | + ``` |
| 214 | +
|
| 215 | +3) If `libstdc++` is not detected, add the symbolic link: |
| 216 | + ``` |
| 217 | + sudo ln -s /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.so /usr/lib/libstdc++.so |
| 218 | + ``` |
| 219 | +
|
0 commit comments