nym_credential_proxy_lib/
helpers.rs

1// Copyright 2025 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: GPL-3.0-only
3
4use rand::RngCore;
5use rand::rngs::OsRng;
6use time::OffsetDateTime;
7use tracing::{debug, info, warn};
8use uuid::Uuid;
9
10pub fn random_uuid() -> Uuid {
11    let mut bytes = [0u8; 16];
12    let mut rng = OsRng;
13    rng.fill_bytes(&mut bytes);
14    Uuid::from_bytes(bytes)
15}
16
17pub struct LockTimer {
18    created: OffsetDateTime,
19    message: String,
20}
21
22impl LockTimer {
23    pub fn new<S: Into<String>>(message: S) -> Self {
24        LockTimer {
25            message: message.into(),
26            ..Default::default()
27        }
28    }
29}
30
31impl Drop for LockTimer {
32    fn drop(&mut self) {
33        let time_taken = OffsetDateTime::now_utc() - self.created;
34        let time_taken_formatted = humantime::format_duration(time_taken.unsigned_abs());
35        if time_taken > time::Duration::SECOND * 10 {
36            warn!(time_taken = %time_taken_formatted, "{}", self.message)
37        } else if time_taken > time::Duration::SECOND * 5 {
38            info!(time_taken = %time_taken_formatted, "{}", self.message)
39        } else {
40            debug!(time_taken = %time_taken_formatted, "{}", self.message)
41        };
42    }
43}
44
45impl Default for LockTimer {
46    fn default() -> Self {
47        LockTimer {
48            created: OffsetDateTime::now_utc(),
49            message: "released the lock".to_string(),
50        }
51    }
52}
53
54// #[allow(clippy::panic)]
55// fn build_sha_short() -> &'static str {
56//     let bin_info = bin_info!();
57//     if bin_info.commit_sha.len() < 7 {
58//         panic!("unavailable build commit sha")
59//     }
60//
61//     if bin_info.commit_sha == "VERGEN_IDEMPOTENT_OUTPUT" {
62//         error!("the binary hasn't been built correctly. it doesn't have a commit sha information");
63//         return "unknown";
64//     }
65//
66//     &bin_info.commit_sha[..7]
67// }