use std::collections::HashMap;
use std::sync::Mutex;
use lazy_static::lazy_static;
use crate::errors::{LicenseError, LicenseResult};
lazy_static! {
static ref LICENSE_DB: Mutex<HashMap<String, bool>> = Mutex::new(HashMap::new());
}
fn make_key(license_id: &str, client_id: &str) -> String {
format!("{}_{}", license_id, client_id)
}
pub fn activate_license(license_id: &str, client_id: &str) -> LicenseResult<bool> {
let mut db = LICENSE_DB
.lock()
.map_err(|_| LicenseError::ServerError("failed to acquire LICENSE_DB lock".into()))?;
db.insert(make_key(license_id, client_id), true);
Ok(true)
}
pub fn deactivate_license(license_id: &str, client_id: &str) -> LicenseResult<bool> {
let mut db = LICENSE_DB
.lock()
.map_err(|_| LicenseError::ServerError("failed to acquire LICENSE_DB lock".into()))?;
db.insert(make_key(license_id, client_id), false);
Ok(true)
}
pub fn is_license_active(license_id: &str, client_id: &str) -> LicenseResult<bool> {
let db = LICENSE_DB
.lock()
.map_err(|_| LicenseError::ServerError("failed to acquire LICENSE_DB lock".into()))?;
Ok(*db.get(&make_key(license_id, client_id)).unwrap_or(&false))
}