grafbase_sdk/types/
authorization.rs1use crate::{
2 types::{AsHeaderName, AsHeaderValue, Headers},
3 wit,
4};
5
6use super::Error;
7
8#[derive(Clone, Copy)]
11pub struct ErrorId(u32);
12
13pub struct AuthorizeQueryOutput {
16 pub(crate) decisions: AuthorizationDecisions,
18 pub(crate) context: Vec<u8>,
20 pub(crate) state: Vec<u8>,
22 pub(crate) additional_headers: Option<Headers>,
24}
25
26impl AuthorizeQueryOutput {
27 pub fn new(decisions: AuthorizationDecisions) -> Self {
29 Self {
30 decisions,
31 context: Vec::new(),
32 state: Vec::new(),
33 additional_headers: None,
34 }
35 }
36
37 pub fn context(mut self, context: impl Into<Vec<u8>>) -> Self {
40 self.context = context.into();
41 self
42 }
43
44 pub fn state(mut self, state: impl Into<Vec<u8>>) -> Self {
48 self.state = state.into();
49 self
50 }
51
52 pub fn header(mut self, name: impl AsHeaderName, value: impl AsHeaderValue) -> Self {
54 let headers = self.additional_headers.get_or_insert_default();
55 headers.append(name, value);
56 self
57 }
58
59 pub fn headers(mut self, headers: Headers) -> Self {
61 self.additional_headers = Some(headers);
62 self
63 }
64}
65
66pub struct AuthorizationDecisions(wit::AuthorizationDecisions);
68
69impl From<AuthorizationDecisions> for wit::AuthorizationDecisions {
70 fn from(value: AuthorizationDecisions) -> Self {
71 value.0
72 }
73}
74
75impl AuthorizationDecisions {
76 pub fn grant_all() -> Self {
78 Self(wit::AuthorizationDecisions::GrantAll)
79 }
80
81 pub fn deny_all(error: impl Into<Error>) -> Self {
83 Self(wit::AuthorizationDecisions::DenyAll(Into::<Error>::into(error).into()))
84 }
85
86 pub fn deny_some_builder() -> DenySomeBuilder {
89 DenySomeBuilder(wit::AuthorizationDecisionsDenySome {
90 element_to_error: Vec::new(),
91 errors: Vec::new(),
92 })
93 }
94}
95
96pub struct DenySomeBuilder(wit::AuthorizationDecisionsDenySome);
98
99impl DenySomeBuilder {
100 pub fn deny(&mut self, x: impl private::QueryElementOrResponseItem, error: impl Into<Error>) {
102 let error_id = self.push_error(error);
103 self.deny_with_error_id(x, error_id)
104 }
105
106 pub fn deny_with_error_id(&mut self, x: impl private::QueryElementOrResponseItem, error_id: ErrorId) {
108 self.0.element_to_error.push((x.ix(), error_id.0));
109 }
110
111 pub fn push_error(&mut self, error: impl Into<Error>) -> ErrorId {
113 let error_ix = self.0.errors.len() as u32;
114 self.0.errors.push(Into::<Error>::into(error).into());
115 ErrorId(error_ix)
116 }
117
118 pub fn build(self) -> AuthorizationDecisions {
120 AuthorizationDecisions(wit::AuthorizationDecisions::DenySome(self.0))
121 }
122}
123
124pub(super) mod private {
125 pub trait QueryElementOrResponseItem: crate::sealed::Sealed {
127 #[doc(hidden)]
128 fn ix(&self) -> u32;
129 }
130}