polyester/codecs/decode/
policies.rs1use crate::codecs::scalars::format_uint64_id;
4use crate::models::{ApiPoliciesList, ApiPolicy, SubaccountPoliciesList, SubaccountPolicy};
5use crate::proto::auth::v1::{
6 ApiPolicyView, CreateApiPolicyResponse, CreateSubaccountPolicyResponse, GetApiPolicyResponse,
7 GetSubaccountPolicyResponse, ListApiPoliciesResponse, ListSubaccountPoliciesResponse,
8 SubaccountPolicyView, UpdateApiPolicyResponse, UpdateSubaccountPolicyResponse,
9};
10
11pub fn subaccount_policy_from_proto(msg: &SubaccountPolicyView) -> SubaccountPolicy {
12 SubaccountPolicy {
13 policy_id: format_uint64_id(msg.id),
14 name: msg.name.clone(),
15 description: msg.description.clone(),
16 revision: msg.revision,
17 }
18}
19
20pub fn subaccount_policies_list_from_proto(
21 msg: &ListSubaccountPoliciesResponse,
22) -> SubaccountPoliciesList {
23 SubaccountPoliciesList {
24 policies: msg
25 .policies
26 .iter()
27 .map(subaccount_policy_from_proto)
28 .collect(),
29 }
30}
31
32pub fn get_subaccount_policy_from_proto(
33 msg: &GetSubaccountPolicyResponse,
34) -> Option<SubaccountPolicy> {
35 msg.policy.as_option().map(subaccount_policy_from_proto)
36}
37
38pub fn create_subaccount_policy_from_proto(
39 msg: &CreateSubaccountPolicyResponse,
40) -> Option<SubaccountPolicy> {
41 msg.policy.as_option().map(subaccount_policy_from_proto)
42}
43
44pub fn update_subaccount_policy_from_proto(
45 msg: &UpdateSubaccountPolicyResponse,
46) -> Option<SubaccountPolicy> {
47 msg.policy.as_option().map(subaccount_policy_from_proto)
48}
49
50pub fn api_policy_from_proto(msg: &ApiPolicyView) -> ApiPolicy {
51 ApiPolicy {
52 policy_id: format_uint64_id(msg.id),
53 name: msg.name.clone(),
54 description: msg.description.clone(),
55 revision: msg.revision,
56 }
57}
58
59pub fn api_policies_list_from_proto(msg: &ListApiPoliciesResponse) -> ApiPoliciesList {
60 ApiPoliciesList {
61 policies: msg.policies.iter().map(api_policy_from_proto).collect(),
62 }
63}
64
65pub fn get_api_policy_from_proto(msg: &GetApiPolicyResponse) -> Option<ApiPolicy> {
66 msg.policy.as_option().map(api_policy_from_proto)
67}
68
69pub fn create_api_policy_from_proto(msg: &CreateApiPolicyResponse) -> Option<ApiPolicy> {
70 msg.policy.as_option().map(api_policy_from_proto)
71}
72
73pub fn update_api_policy_from_proto(msg: &UpdateApiPolicyResponse) -> Option<ApiPolicy> {
74 msg.policy.as_option().map(api_policy_from_proto)
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn subaccount_policies_list_maps_ids() {
83 let msg = ListSubaccountPoliciesResponse {
84 policies: vec![SubaccountPolicyView {
85 id: 42,
86 name: "default".into(),
87 description: "desc".into(),
88 revision: 3,
89 ..Default::default()
90 }],
91 ..Default::default()
92 };
93 let result = subaccount_policies_list_from_proto(&msg);
94 assert_eq!(result.policies.len(), 1);
95 assert_eq!(result.policies[0].policy_id, format_uint64_id(42));
96 assert_eq!(result.policies[0].name, "default");
97 assert_eq!(result.policies[0].revision, 3);
98 }
99
100 #[test]
101 fn api_policies_list_maps_ids() {
102 let msg = ListApiPoliciesResponse {
103 policies: vec![ApiPolicyView {
104 id: 7,
105 name: "read-only".into(),
106 revision: 2,
107 ..Default::default()
108 }],
109 ..Default::default()
110 };
111 let result = api_policies_list_from_proto(&msg);
112 assert_eq!(result.policies.len(), 1);
113 assert_eq!(result.policies[0].policy_id, format_uint64_id(7));
114 assert_eq!(result.policies[0].revision, 2);
115 }
116}