mtp/example/server/src/clients.rs
Alois 89a20044a5
Some checks failed
CI / rustfmt (push) Successful in 17s
CI / clippy (push) Failing after 1m16s
CI / wasm build (push) Successful in 1m17s
CI / example (push) Successful in 1m29s
CI / test (push) Successful in 1m49s
CI / duplicate code (push) Successful in 12s
CI / web client (push) Failing after 27s
CI / cargo-machete (push) Successful in 1m10s
CI / cargo-deny (push) Failing after 2m20s
(feat): rename example-usage to just example
(feat): add the example's web-client dist folder to a gitignore
(fix): format issues
(fix): a lot of duplicate code
2026-06-27 03:20:25 +02:00

25 lines
871 B
Rust

use std::collections::HashMap;
use std::fs;
use std::sync::{Arc, Mutex};
use mtp::crypto::PublicKeyBundle;
pub fn load_client_db(
path: &str,
) -> Result<(Arc<Mutex<HashMap<u64, PublicKeyBundle>>>, Arc<Mutex<u64>>), Box<dyn std::error::Error>>
{
let clients_map = match fs::read_to_string(path) {
Ok(data) => match serde_json::from_str(&data) {
Ok(clients) => clients,
Err(e) => {
eprintln!("Failed to parse {path}; starting with empty client database: {e}");
HashMap::new()
}
},
Err(_) => HashMap::new(),
};
let clients: Arc<Mutex<HashMap<u64, PublicKeyBundle>>> = Arc::new(Mutex::new(clients_map));
let next_value = clients.lock().unwrap().keys().max().unwrap_or(&999) + 1;
let next_id = Arc::new(Mutex::new(next_value));
Ok((clients, next_id))
}