Skip to main content

cedar_policy/proto/
api.rs

1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::super::api;
18use super::{models, traits};
19
20/// Macro that implements From<> both ways for cases where the `A` type is a
21/// simple wrapper around a different type `C` which already has From<>
22/// conversions both ways with `B`
23macro_rules! standard_conversions {
24    ( $A:ty, $A_expr:expr, $B:ty ) => {
25        impl From<&$A> for $B {
26            fn from(v: &$A) -> $B {
27                Self::from(&v.0)
28            }
29        }
30
31        impl From<&$B> for $A {
32            fn from(v: &$B) -> $A {
33                $A_expr(v.into())
34            }
35        }
36    };
37}
38
39// standard conversions
40
41standard_conversions!(api::Entity, api::Entity, models::Entity);
42standard_conversions!(api::EntityUid, api::EntityUid, models::EntityUid);
43standard_conversions!(api::Entities, api::Entities, models::Entities);
44standard_conversions!(api::Schema, api::Schema, models::Schema);
45standard_conversions!(api::EntityTypeName, api::EntityTypeName, models::Name);
46standard_conversions!(api::EntityNamespace, api::EntityNamespace, models::Name);
47standard_conversions!(api::Expression, api::Expression, models::Expr);
48standard_conversions!(api::Request, api::Request, models::Request);
49
50// nonstandard conversions
51
52impl From<&api::Template> for models::TemplateBody {
53    fn from(v: &api::Template) -> Self {
54        Self::from(&v.ast)
55    }
56}
57
58impl From<&models::TemplateBody> for api::Template {
59    fn from(v: &models::TemplateBody) -> Self {
60        Self::from_ast(v.into())
61    }
62}
63
64impl From<&api::Policy> for models::Policy {
65    fn from(v: &api::Policy) -> Self {
66        Self::from(&v.ast)
67    }
68}
69
70impl From<&api::PolicySet> for models::PolicySet {
71    fn from(v: &api::PolicySet) -> Self {
72        Self::from(&v.ast)
73    }
74}
75
76impl TryFrom<&models::PolicySet> for api::PolicySet {
77    type Error = api::PolicySetError;
78    fn try_from(v: &models::PolicySet) -> Result<Self, Self::Error> {
79        #[expect(clippy::expect_used, reason = "experimental feature")]
80        Self::from_ast(
81            v.try_into()
82                .expect("proto-encoded policy set should be a valid policy set"),
83        )
84    }
85}
86
87#[expect(clippy::use_self, reason = "readability")]
88impl From<&api::ValidationMode> for models::ValidationMode {
89    fn from(v: &api::ValidationMode) -> Self {
90        match v {
91            api::ValidationMode::Strict => models::ValidationMode::Strict,
92            #[cfg(feature = "permissive-validate")]
93            api::ValidationMode::Permissive => models::ValidationMode::Permissive,
94            #[cfg(feature = "partial-validate")]
95            api::ValidationMode::Partial => models::ValidationMode::Partial,
96        }
97    }
98}
99
100#[expect(clippy::use_self, reason = "readability")]
101impl From<&models::ValidationMode> for api::ValidationMode {
102    fn from(v: &models::ValidationMode) -> Self {
103        match v {
104            models::ValidationMode::Strict => api::ValidationMode::Strict,
105            #[cfg(feature = "permissive-validate")]
106            models::ValidationMode::Permissive => api::ValidationMode::Permissive,
107            #[cfg(not(feature = "permissive-validate"))]
108            models::ValidationMode::Permissive => panic!("Protobuf specifies permissive validation, but `permissive-validate` feature not enabled in this build"),
109            #[cfg(feature = "partial-validate")]
110            models::ValidationMode::Partial => api::ValidationMode::Partial,
111            #[cfg(not(feature = "partial-validate"))]
112            models::ValidationMode::Partial => panic!("Protobuf specifies partial validation, but `partial-validate` feature not enabled in this build"),
113        }
114    }
115}
116
117/// Macro that implements `traits::Protobuf` for cases where From<> conversions
118/// exist both ways between the api type `$api` and the protobuf model type `$model`
119macro_rules! standard_protobuf_impl {
120    ( $api:ty, $model:ty ) => {
121        impl traits::Protobuf for $api {
122            fn encode(&self) -> Vec<u8> {
123                traits::encode_to_vec::<$model>(self)
124            }
125            fn decode(buf: impl prost::bytes::Buf) -> Result<Self, prost::DecodeError> {
126                traits::decode::<$model, _>(buf)
127            }
128        }
129    };
130}
131
132// standard implementations of `traits::Protobuf`
133
134standard_protobuf_impl!(api::Entity, models::Entity);
135standard_protobuf_impl!(api::Entities, models::Entities);
136standard_protobuf_impl!(api::Schema, models::Schema);
137standard_protobuf_impl!(api::EntityTypeName, models::Name);
138standard_protobuf_impl!(api::EntityNamespace, models::Name);
139standard_protobuf_impl!(api::Template, models::TemplateBody);
140standard_protobuf_impl!(api::Expression, models::Expr);
141standard_protobuf_impl!(api::Request, models::Request);
142
143// nonstandard implementations of `traits::Protobuf`
144
145impl traits::Protobuf for api::PolicySet {
146    fn encode(&self) -> Vec<u8> {
147        traits::encode_to_vec::<models::PolicySet>(self)
148    }
149    fn decode(buf: impl prost::bytes::Buf) -> Result<Self, prost::DecodeError> {
150        #[expect(clippy::expect_used, reason = "experimental feature")]
151        Ok(traits::try_decode::<models::PolicySet, _, Self>(buf)?
152            .expect("protobuf-encoded policy set should be a valid policy set"))
153    }
154}