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
7pub trait ApiPath {
11 fn path_segment() -> &'static str;
13}
14
15impl 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
34impl 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
53impl ApiPath for EncryptedPEPJSONValue {
55 fn path_segment() -> &'static str {
56 "json"
57 }
58}
59
60pub const API_BASE: &str = ""; pub 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
68pub fn pseudonymize_path<T: ApiPath>() -> String {
70 format!("/pseudonymize/{}", T::path_segment())
71}
72
73pub fn pseudonymize_batch_path<T: ApiPath + HasStructure>() -> String {
76 format!("/pseudonymize_batch/{}", T::path_segment())
77}
78
79pub fn rekey_path<T: ApiPath>() -> String {
81 format!("/rekey/{}", T::path_segment())
82}
83
84pub fn rekey_batch_path<T: ApiPath + HasStructure>() -> String {
87 format!("/rekey_batch/{}", T::path_segment())
88}
89
90pub fn transcrypt_path<T: ApiPath>() -> String {
92 format!("/transcrypt/{}", T::path_segment())
93}
94
95pub 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 assert_eq!(rekey_path::<EncryptedPseudonym>(), "/rekey/pseudonym");
175 assert_eq!(
176 rekey_path::<LongEncryptedPseudonym>(),
177 "/rekey/long_pseudonym"
178 );
179 }
180}