1pub use http::StatusCode;
4use minicbor_serde::error::DecodeError;
5pub use serde::Deserialize;
6use serde::Serialize;
7
8use crate::wit;
9
10pub struct Directive(crate::wit::Directive);
12
13impl Directive {
14 pub fn name(&self) -> &str {
16 &self.0.name
17 }
18
19 pub fn subgraph_name(&self) -> &str {
21 &self.0.subgraph_name
22 }
23
24 pub fn arguments<'de, T>(&'de self) -> Result<T, DecodeError>
29 where
30 T: Deserialize<'de>,
31 {
32 minicbor_serde::from_slice(&self.0.arguments)
33 }
34}
35
36impl From<crate::wit::Directive> for Directive {
37 fn from(value: crate::wit::Directive) -> Self {
38 Self(value)
39 }
40}
41
42pub struct FieldDefinition(crate::wit::FieldDefinition);
44
45impl FieldDefinition {
46 pub fn name(&self) -> &str {
48 self.0.name.as_str()
49 }
50
51 pub fn type_name(&self) -> &str {
53 self.0.type_name.as_str()
54 }
55}
56
57impl From<crate::wit::FieldDefinition> for FieldDefinition {
58 fn from(value: crate::wit::FieldDefinition) -> Self {
59 Self(value)
60 }
61}
62
63pub struct FieldOutput(crate::wit::FieldOutput);
65
66impl Default for FieldOutput {
67 fn default() -> Self {
68 Self::new()
69 }
70}
71
72impl FieldOutput {
73 pub fn new() -> Self {
75 Self(crate::wit::FieldOutput { outputs: Vec::new() })
76 }
77
78 pub fn with_capacity(capacity: usize) -> Self {
83 Self(crate::wit::FieldOutput {
84 outputs: Vec::with_capacity(capacity),
85 })
86 }
87
88 pub fn push_value<T>(&mut self, output: T)
90 where
91 T: Serialize,
92 {
93 let output = crate::cbor::to_vec(output).expect("serialization error is Infallible, so it should never happen");
94
95 self.0.outputs.push(Ok(output));
96 }
97
98 pub fn push_error(&mut self, error: crate::wit::Error) {
100 self.0.outputs.push(Err(error))
101 }
102}
103
104impl From<FieldOutput> for crate::wit::FieldOutput {
105 fn from(value: FieldOutput) -> Self {
106 value.0
107 }
108}
109
110#[derive(Debug)]
112pub struct FieldInputs(Vec<Vec<u8>>);
113
114impl FieldInputs {
115 pub(crate) fn new(inputs: Vec<Vec<u8>>) -> Self {
116 Self(inputs)
117 }
118
119 pub fn deserialize<'de, T>(&'de self) -> Result<Vec<T>, Box<dyn std::error::Error>>
121 where
122 T: Deserialize<'de>,
123 {
124 self.0
125 .iter()
126 .map(|input| minicbor_serde::from_slice(input).map_err(|e| Box::new(e) as Box<dyn std::error::Error>))
127 .collect()
128 }
129}
130
131pub struct Configuration(Vec<u8>);
133
134impl Configuration {
135 pub(crate) fn new(config: Vec<u8>) -> Self {
137 Self(config)
138 }
139
140 pub fn deserialize<'de, T>(&'de self) -> Result<T, Box<dyn std::error::Error>>
146 where
147 T: Deserialize<'de>,
148 {
149 minicbor_serde::from_slice(&self.0).map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
150 }
151}
152
153pub struct Cache;
155
156pub struct ErrorResponse(crate::wit::ErrorResponse);
158
159impl From<ErrorResponse> for crate::wit::ErrorResponse {
160 fn from(resp: ErrorResponse) -> Self {
161 resp.0
162 }
163}
164
165impl ErrorResponse {
166 pub fn new(status_code: http::StatusCode) -> Self {
168 Self(crate::wit::ErrorResponse {
169 status_code: status_code.as_u16(),
170 errors: Vec::new(),
171 })
172 }
173
174 #[must_use]
176 pub fn with_error(mut self, error: crate::wit::Error) -> Self {
177 self.0.errors.push(error);
178 self
179 }
180
181 pub fn push_error(&mut self, error: crate::wit::Error) {
183 self.0.errors.push(error);
184 }
185}
186
187pub struct Token {
189 claims: Vec<u8>,
190}
191
192impl From<Token> for wit::Token {
193 fn from(token: Token) -> wit::Token {
194 wit::Token { raw: token.claims }
195 }
196}
197
198impl Token {
199 pub fn new<T>(claims: T) -> Self
203 where
204 T: serde::Serialize,
205 {
206 Self {
207 claims: crate::cbor::to_vec(&claims).expect("serialization error is Infallible, so it should never happen"),
208 }
209 }
210}