Skip to main content

paas_api/
paths.rs

1use libpep::data::json::EncryptedPEPJSONValue;
2use libpep::data::long::{LongEncryptedAttribute, LongEncryptedPseudonym};
3use libpep::data::records::{EncryptedRecord, LongEncryptedRecord};
4use libpep::data::simple::{EncryptedAttribute, EncryptedPseudonym};
5use libpep::data::traits::HasStructure;
6
7/// Trait for encrypted types to provide their API path segment.
8/// The path format is: /{operation}[_batch]/{type_segment}
9/// Examples: /pseudonymize/pseudonym, /rekey_batch/long_attribute, /transcrypt/json
10pub trait ApiPath {
11    /// Returns the full path segment for this encrypted type (e.g., "pseudonym", "long_attribute", "json").
12    fn path_segment() -> &'static str;
13}
14
15// Simple types
16impl ApiPath for EncryptedPseudonym {
17    fn path_segment() -> &'static str {
18        "pseudonym"
19    }
20}
21
22impl ApiPath for EncryptedAttribute {
23    fn path_segment() -> &'static str {
24        "attribute"
25    }
26}
27
28impl ApiPath for EncryptedRecord {
29    fn path_segment() -> &'static str {
30        "record"
31    }
32}
33
34// Long types
35impl ApiPath for LongEncryptedPseudonym {
36    fn path_segment() -> &'static str {
37        "long_pseudonym"
38    }
39}
40
41impl ApiPath for LongEncryptedAttribute {
42    fn path_segment() -> &'static str {
43        "long_attribute"
44    }
45}
46
47impl ApiPath for LongEncryptedRecord {
48    fn path_segment() -> &'static str {
49        "long_record"
50    }
51}
52
53// JSON types
54impl ApiPath for EncryptedPEPJSONValue {
55    fn path_segment() -> &'static str {
56        "json"
57    }
58}
59
60pub const API_BASE: &str = ""; // Currently empty (may need to change)
61
62pub const STATUS: &str = "/status";
63pub const CONFIG: &str = "/config";
64pub const SESSIONS_GET: &str = "/sessions";
65pub const SESSIONS_START: &str = "/sessions/start";
66pub const SESSIONS_END: &str = "/sessions/end";
67
68/// Generate the pseudonymization path for a given encrypted type.
69pub fn pseudonymize_path<T: ApiPath>() -> String {
70    format!("/pseudonymize/{}", T::path_segment())
71}
72
73/// Generate the pseudonymization batch path for a given encrypted type.
74/// Requires the type to implement HasStructure for batch validation.
75pub fn pseudonymize_batch_path<T: ApiPath + HasStructure>() -> String {
76    format!("/pseudonymize_batch/{}", T::path_segment())
77}
78
79/// Generate the rekey path for a given encrypted type.
80pub fn rekey_path<T: ApiPath>() -> String {
81    format!("/rekey/{}", T::path_segment())
82}
83
84/// Generate the rekey batch path for a given encrypted type.
85/// Requires the type to implement HasStructure for batch validation.
86pub fn rekey_batch_path<T: ApiPath + HasStructure>() -> String {
87    format!("/rekey_batch/{}", T::path_segment())
88}
89
90/// Generate the transcrypt path for a given encrypted type.
91pub fn transcrypt_path<T: ApiPath>() -> String {
92    format!("/transcrypt/{}", T::path_segment())
93}
94
95/// Generate the transcrypt batch path for a given encrypted type.
96/// Requires the type to implement HasStructure for batch validation.
97pub fn transcrypt_batch_path<T: ApiPath + HasStructure>() -> String {
98    format!("/transcrypt_batch/{}", T::path_segment())
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use libpep::data::json::EncryptedPEPJSONValue;
105    use libpep::data::long::{LongEncryptedAttribute, LongEncryptedPseudonym};
106    use libpep::data::records::{EncryptedRecord, LongEncryptedRecord};
107    use libpep::data::simple::{EncryptedAttribute, EncryptedPseudonym};
108
109    #[test]
110    fn test_simple_paths() {
111        assert_eq!(
112            pseudonymize_path::<EncryptedPseudonym>(),
113            "/pseudonymize/pseudonym"
114        );
115        assert_eq!(
116            pseudonymize_batch_path::<EncryptedPseudonym>(),
117            "/pseudonymize_batch/pseudonym"
118        );
119        assert_eq!(rekey_path::<EncryptedAttribute>(), "/rekey/attribute");
120        assert_eq!(
121            rekey_batch_path::<EncryptedAttribute>(),
122            "/rekey_batch/attribute"
123        );
124        assert_eq!(transcrypt_path::<EncryptedRecord>(), "/transcrypt/record");
125        assert_eq!(
126            transcrypt_batch_path::<EncryptedRecord>(),
127            "/transcrypt_batch/record"
128        );
129    }
130
131    #[test]
132    fn test_long_paths() {
133        assert_eq!(
134            pseudonymize_path::<LongEncryptedPseudonym>(),
135            "/pseudonymize/long_pseudonym"
136        );
137        assert_eq!(
138            pseudonymize_batch_path::<LongEncryptedPseudonym>(),
139            "/pseudonymize_batch/long_pseudonym"
140        );
141        assert_eq!(
142            rekey_path::<LongEncryptedAttribute>(),
143            "/rekey/long_attribute"
144        );
145        assert_eq!(
146            rekey_batch_path::<LongEncryptedAttribute>(),
147            "/rekey_batch/long_attribute"
148        );
149        assert_eq!(
150            transcrypt_path::<LongEncryptedRecord>(),
151            "/transcrypt/long_record"
152        );
153        assert_eq!(
154            transcrypt_batch_path::<LongEncryptedRecord>(),
155            "/transcrypt_batch/long_record"
156        );
157    }
158
159    #[test]
160    fn test_json_paths() {
161        assert_eq!(
162            transcrypt_path::<EncryptedPEPJSONValue>(),
163            "/transcrypt/json"
164        );
165        assert_eq!(
166            transcrypt_batch_path::<EncryptedPEPJSONValue>(),
167            "/transcrypt_batch/json"
168        );
169    }
170
171    #[test]
172    fn test_pseudonym_can_be_rekeyed() {
173        // Pseudonyms can be rekeyed too, not just pseudonymized
174        assert_eq!(rekey_path::<EncryptedPseudonym>(), "/rekey/pseudonym");
175        assert_eq!(
176            rekey_path::<LongEncryptedPseudonym>(),
177            "/rekey/long_pseudonym"
178        );
179    }
180}