1use buffa::ExtensionSet;
4use buffa_proto_options as pb;
5
6use crate::generated::descriptor::FileDescriptorProto;
7
8use super::*;
9
10macro_rules! known {
13 ($value:expr) => {
14 if !$value.__buffa_unknown_fields.is_empty() {
15 return Err(ErrorReason::InvalidManifest.into());
16 }
17 };
18}
19
20pub(crate) fn descriptor_options(
25 files: &[FileDescriptorProto],
26 requested: &[String],
27) -> Result<Option<AdapterConfig>, AdapterError> {
28 let mut combined = AdapterConfig {
29 version: 1,
30 selectors: Vec::new(),
31 payloads: Vec::new(),
32 borrowed: Vec::new(),
33 ports: Vec::new(),
34 };
35 let mut found = false;
36 for file in files
37 .iter()
38 .filter(|f| f.name.as_ref().is_some_and(|n| requested.contains(n)))
39 {
40 let Some(options) = file.options.as_option() else {
41 continue;
42 };
43 if options.unknown_fields().iter().any(|field| {
44 field.number == pb::FILE.number()
45 && !matches!(field.data, buffa::UnknownFieldData::LengthDelimited(_))
46 }) {
47 return Err(ErrorReason::InvalidManifest.into());
48 }
49 if !options.has_extension(&pb::FILE) {
50 continue;
51 }
52 let options = options
55 .extension(&pb::FILE)
56 .ok_or(ErrorReason::InvalidManifest)?;
57 known!(options);
58 let Some(native) = options.adapters.as_option() else {
59 continue;
60 };
61 let config = AdapterConfig::try_from(native)?;
62 found = true;
63 combined.selectors.extend(config.selectors);
64 combined.payloads.extend(config.payloads);
65 combined.borrowed.extend(config.borrowed);
66 combined.ports.extend(config.ports);
67 }
68 Ok(found.then_some(combined))
69}
70
71impl TryFrom<&pb::NativeAdapters> for AdapterConfig {
72 type Error = AdapterError;
73
74 fn try_from(value: &pb::NativeAdapters) -> Result<Self, Self::Error> {
75 known!(value);
76 if value.version != 1 {
77 return Err(ErrorReason::UnsupportedVersion.into());
78 }
79 Ok(Self {
80 version: value.version,
81 selectors: value
82 .selectors
83 .iter()
84 .map(ClosedSelector::try_from)
85 .collect::<Result<_, _>>()?,
86 payloads: value
87 .payloads
88 .iter()
89 .map(PayloadSelector::try_from)
90 .collect::<Result<_, _>>()?,
91 borrowed: value
92 .borrowed
93 .iter()
94 .map(BorrowedAdapter::try_from)
95 .collect::<Result<_, _>>()?,
96 ports: value
97 .ports
98 .iter()
99 .map(CapabilityPort::try_from)
100 .collect::<Result<_, _>>()?,
101 })
102 }
103}
104
105impl TryFrom<&pb::NativeWireEnum> for WireEnum {
106 type Error = AdapterError;
107 fn try_from(v: &pb::NativeWireEnum) -> Result<Self, Self::Error> {
108 known!(v);
109 Ok(Self {
110 source: v.source.clone(),
111 rust_type: v.rust_type.clone(),
112 })
113 }
114}
115
116impl TryFrom<&pb::NativeErrorMapping> for ErrorMapping {
117 type Error = AdapterError;
118 fn try_from(v: &pb::NativeErrorMapping) -> Result<Self, Self::Error> {
119 known!(v);
120 Ok(Self {
121 rust_type: v.rust_type.clone(),
122 value: v.value.clone(),
123 })
124 }
125}
126
127impl TryFrom<&pb::NativeNormalization> for Normalization {
128 type Error = AdapterError;
129 fn try_from(v: &pb::NativeNormalization) -> Result<Self, Self::Error> {
130 known!(v);
131 Ok(Self {
132 source: v.source.clone(),
133 target: v.target.clone(),
134 })
135 }
136}
137
138impl TryFrom<&pb::NativeSelectorVariant> for SelectorVariant {
139 type Error = AdapterError;
140 fn try_from(v: &pb::NativeSelectorVariant) -> Result<Self, Self::Error> {
141 known!(v);
142 Ok(Self {
143 source: v.source.clone(),
144 name: v.name.clone(),
145 aliases: v.aliases.clone(),
146 identifier: v.identifier,
147 })
148 }
149}
150
151impl TryFrom<&pb::NativeLegacySelector> for LegacySelector {
152 type Error = AdapterError;
153 fn try_from(v: &pb::NativeLegacySelector) -> Result<Self, Self::Error> {
154 known!(v);
155 Ok(Self {
156 wire: WireEnum::try_from(v.wire.as_option().ok_or(ErrorReason::InvalidManifest)?)?,
157 normalizations: v
158 .normalizations
159 .iter()
160 .map(Normalization::try_from)
161 .collect::<Result<_, _>>()?,
162 rejected: v.rejected.clone(),
163 })
164 }
165}
166
167impl TryFrom<&pb::NativeClosedSelector> for ClosedSelector {
168 type Error = AdapterError;
169 fn try_from(v: &pb::NativeClosedSelector) -> Result<Self, Self::Error> {
170 known!(v);
171 Ok(Self {
172 file: v.file.clone(),
173 name: v.name.clone(),
174 wire: WireEnum::try_from(v.wire.as_option().ok_or(ErrorReason::InvalidManifest)?)?,
175 error: ErrorMapping::try_from(
176 v.error.as_option().ok_or(ErrorReason::InvalidManifest)?,
177 )?,
178 non_exhaustive: v.non_exhaustive.unwrap_or(false),
179 variants: v
180 .variants
181 .iter()
182 .map(SelectorVariant::try_from)
183 .collect::<Result<_, _>>()?,
184 normalizations: v
185 .normalizations
186 .iter()
187 .map(Normalization::try_from)
188 .collect::<Result<_, _>>()?,
189 rejected: v.rejected.clone(),
190 legacy: v
191 .legacy
192 .iter()
193 .map(LegacySelector::try_from)
194 .collect::<Result<_, _>>()?,
195 identifier_error: v
196 .identifier_error
197 .as_option()
198 .map(ErrorMapping::try_from)
199 .transpose()?,
200 })
201 }
202}
203
204impl TryFrom<&pb::NativeFieldSource> for FieldSource {
205 type Error = AdapterError;
206 fn try_from(v: &pb::NativeFieldSource) -> Result<Self, Self::Error> {
207 known!(v);
208 Ok(Self {
209 message: v.message.clone(),
210 path: v.path.clone(),
211 })
212 }
213}
214
215impl TryFrom<&pb::NativePayloadType> for PayloadType {
216 type Error = AdapterError;
217 fn try_from(v: &pb::NativePayloadType) -> Result<Self, Self::Error> {
218 known!(v);
219 match v.kind {
220 pb::NativePayloadTypeKind::NATIVE_PAYLOAD_TYPE_BYTES
221 if v.source.is_none() && v.rust_type.is_none() =>
222 {
223 Ok(Self::Bytes)
224 }
225 pb::NativePayloadTypeKind::NATIVE_PAYLOAD_TYPE_VALUE => Ok(Self::Value {
226 source: v.source.clone().ok_or(ErrorReason::InvalidManifest)?,
227 rust_type: v.rust_type.clone().ok_or(ErrorReason::InvalidManifest)?,
228 }),
229 _ => Err(ErrorReason::InvalidPayload.into()),
230 }
231 }
232}
233
234impl TryFrom<&pb::NativePayloadField> for PayloadField {
235 type Error = AdapterError;
236 fn try_from(v: &pb::NativePayloadField) -> Result<Self, Self::Error> {
237 known!(v);
238 Ok(Self {
239 name: v.name.clone(),
240 source: FieldSource::try_from(
241 v.source.as_option().ok_or(ErrorReason::InvalidManifest)?,
242 )?,
243 representation: PayloadType::try_from(
244 v.representation
245 .as_option()
246 .ok_or(ErrorReason::InvalidManifest)?,
247 )?,
248 })
249 }
250}
251
252impl TryFrom<&pb::NativePayload> for Payload {
253 type Error = AdapterError;
254 fn try_from(v: &pb::NativePayload) -> Result<Self, Self::Error> {
255 known!(v);
256 match v.kind {
257 pb::NativePayloadKind::NATIVE_PAYLOAD_UNIT
258 if v.fields.is_empty() && v.port.is_none() =>
259 {
260 Ok(Self::Unit)
261 }
262 pb::NativePayloadKind::NATIVE_PAYLOAD_TUPLE
263 if v.fields.len() == 1 && v.port.is_none() =>
264 {
265 let field = v.fields.first().ok_or(ErrorReason::InvalidPayload)?;
266 Ok(Self::Tuple {
267 field: PayloadField::try_from(field)?,
268 })
269 }
270 pb::NativePayloadKind::NATIVE_PAYLOAD_STRUCT
271 if !v.fields.is_empty() && v.port.is_none() =>
272 {
273 Ok(Self::Struct {
274 fields: v
275 .fields
276 .iter()
277 .map(PayloadField::try_from)
278 .collect::<Result<_, _>>()?,
279 })
280 }
281 pb::NativePayloadKind::NATIVE_PAYLOAD_PORT if v.fields.is_empty() => Ok(Self::Port {
282 name: v.port.clone().ok_or(ErrorReason::InvalidPayload)?,
283 }),
284 _ => Err(ErrorReason::InvalidPayload.into()),
285 }
286 }
287}
288
289impl TryFrom<&pb::NativePayloadVariant> for PayloadVariant {
290 type Error = AdapterError;
291 fn try_from(v: &pb::NativePayloadVariant) -> Result<Self, Self::Error> {
292 known!(v);
293 Ok(Self {
294 name: v.name.clone(),
295 cases: v.cases.clone(),
296 payload: Payload::try_from(v.payload.as_option().ok_or(ErrorReason::InvalidManifest)?)?,
297 })
298 }
299}
300
301impl TryFrom<&pb::NativePayloadSelector> for PayloadSelector {
302 type Error = AdapterError;
303 fn try_from(v: &pb::NativePayloadSelector) -> Result<Self, Self::Error> {
304 known!(v);
305 Ok(Self {
306 file: v.file.clone(),
307 name: v.name.clone(),
308 source: v.source.clone(),
309 variants: v
310 .variants
311 .iter()
312 .map(PayloadVariant::try_from)
313 .collect::<Result<_, _>>()?,
314 })
315 }
316}
317
318impl TryFrom<&pb::NativeBorrowedSource> for BorrowedSource {
319 type Error = AdapterError;
320 fn try_from(v: &pb::NativeBorrowedSource) -> Result<Self, Self::Error> {
321 known!(v);
322 match v.kind {
323 pb::NativeBorrowedSourceKind::NATIVE_BORROWED_SOURCE_ENUM
324 if v.message.is_none() && v.name.is_none() =>
325 {
326 Ok(Self::Enum {
327 source: v.source.clone().ok_or(ErrorReason::InvalidManifest)?,
328 })
329 }
330 pb::NativeBorrowedSourceKind::NATIVE_BORROWED_SOURCE_ONEOF if v.source.is_none() => {
331 Ok(Self::Oneof {
332 message: v.message.clone().ok_or(ErrorReason::InvalidManifest)?,
333 name: v.name.clone().ok_or(ErrorReason::InvalidManifest)?,
334 })
335 }
336 _ => Err(ErrorReason::InvalidPayload.into()),
337 }
338 }
339}
340
341impl TryFrom<&pb::NativeBorrowedAdapter> for BorrowedAdapter {
342 type Error = AdapterError;
343 fn try_from(v: &pb::NativeBorrowedAdapter) -> Result<Self, Self::Error> {
344 known!(v);
345 Ok(Self {
346 file: v.file.clone(),
347 name: v.name.clone(),
348 source: BorrowedSource::try_from(
349 v.source.as_option().ok_or(ErrorReason::InvalidManifest)?,
350 )?,
351 variants: v
352 .variants
353 .iter()
354 .map(PayloadVariant::try_from)
355 .collect::<Result<_, _>>()?,
356 })
357 }
358}
359
360impl TryFrom<&pb::NativeCapabilityPort> for CapabilityPort {
361 type Error = AdapterError;
362 fn try_from(v: &pb::NativeCapabilityPort) -> Result<Self, Self::Error> {
363 known!(v);
364 Ok(Self {
365 name: v.name.clone(),
366 rust_trait: v.rust_trait.clone(),
367 })
368 }
369}