os_bridge/common/
utils.rs

1use rand::Rng;
2
3use crate::{common::errors::BridgeError, BridgeResult};
4
5/// Get the PID of the process supports Windows, macOS, and Linux
6pub fn get_pid() -> BridgeResult<u32> {
7    let pid = sysinfo::get_current_pid().map_err(|err| BridgeError::WithMsg(err.to_string()))?;
8    Ok(pid.as_u32())
9}
10
11
12// Generate a random string
13#[allow(unused)]
14pub fn random_string(len: usize) -> String {
15    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
16  abcdefghijklmnopqrstuvwxyz\
17  0123456789)(*&^%$#@!~";
18    let mut rng = rand::rng();
19    let password: String = (0..len)
20        .map(|_| {
21            let idx = rng.random_range(0..CHARSET.len());
22            CHARSET[idx] as char
23        })
24        .collect();
25    return password;
26}
27
28// Generate a 16 bit random password
29#[allow(unused)]
30pub fn get_random_key16() -> BridgeResult<[u8; 16]> {
31    let mut arr: [u8; 16] = [0u8; 16];
32    rand::rng().fill(&mut arr[..]);
33    Ok(arr)
34}
35
36// Convert an array to a string
37#[allow(unused)]
38fn array_to_string(arr: [u8; 16]) -> String {
39    let mut s = String::new();
40    for &byte in &arr {
41        if byte == 0 {
42            break;
43        }
44        s.push(byte as char);
45    }
46    s
47}