Skip to main content

ferrox_security/
public_id.rs

1use uuid::Uuid;
2
3/// Helper to mask internal Database UUIDs (e.g. UUIDv7) into Public IDs for the Frontend.
4pub struct PublicId;
5
6impl PublicId {
7    /// Masks a primary UUID into a public string format with a prefix.
8    /// Example: `mask_uuid("usr", my_uuid)` -> `usr_a1b2c3d4...`
9    pub fn mask_uuid(prefix: &str, id: Uuid) -> String {
10        let hex = id.simple().to_string();
11        format!("{}_{}", prefix, hex)
12    }
13
14    /// Converts a public string back to an internal UUID.
15    /// Returns None if the format is invalid.
16    pub fn unmask_uuid(prefix: &str, public_id: &str) -> Option<Uuid> {
17        let expected_prefix = format!("{}_", prefix);
18        if public_id.starts_with(&expected_prefix) {
19            let hex = &public_id[expected_prefix.len()..];
20            Uuid::parse_str(hex).ok()
21        } else {
22            None
23        }
24    }
25}