1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright 2025 The Drasi Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Plugin descriptor traits that define how plugins advertise their capabilities.
//!
//! Each plugin type (source, reaction, bootstrapper) has a corresponding descriptor
//! trait. A plugin crate implements one or more of these traits and returns instances
//! via [`PluginRegistration`](crate::registration::PluginRegistration).
//!
//! # Descriptor Responsibilities
//!
//! Each descriptor provides:
//!
//! 1. **Kind** — A unique string identifier (e.g., `"postgres"`, `"http"`, `"log"`).
//! 2. **Config version** — A semver string for the plugin's DTO version.
//! 3. **Config schema** — A serialized [utoipa](https://docs.rs/utoipa) `Schema` object
//! (as JSON) that describes the plugin's configuration DTO. This is used by the server
//! to generate the OpenAPI specification.
//! 4. **Factory method** — An async `create_*` method that takes raw JSON config and
//! returns a configured plugin instance.
//!
//! # Schema Generation
//!
//! Plugins generate their schema by deriving [`utoipa::ToSchema`] on their DTO struct
//! and serializing it to JSON:
//!
//! ```rust,ignore
//! use utoipa::openapi::schema::Schema;
//! use drasi_plugin_sdk::prelude::*;
//!
//! #[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
//! #[serde(rename_all = "camelCase")]
//! pub struct MySourceConfigDto {
//! #[schema(value_type = ConfigValueString)]
//! pub host: ConfigValue<String>,
//! #[schema(value_type = ConfigValueU16)]
//! pub port: ConfigValue<u16>,
//! }
//!
//! // In the descriptor implementation:
//! fn config_schema_json(&self) -> String {
//! let schema = <MySourceConfigDto as utoipa::ToSchema>::schema();
//! serde_json::to_string(&schema).unwrap()
//! }
//! ```
//!
//! # DTO Versioning
//!
//! Each plugin versions its DTO independently using semver:
//!
//! - **Major** version bump → Breaking change (field removed, type changed, renamed).
//! - **Minor** version bump → Additive change (new optional field added).
//! - **Patch** version bump → Documentation or description change.
//!
//! The server compares the plugin's `config_version()` against known versions and
//! can reject incompatible plugins at load time.
//!
//! # Dynamic Loading
//!
//! Descriptors are fully compatible with dynamic loading. When a plugin is compiled
//! as a `cdylib` shared library, the descriptor trait objects are passed to the server
//! through the [`PluginRegistration`](crate::registration::PluginRegistration) returned
//! by the `drasi_plugin_init()` entry point. The server calls the descriptor methods
//! (e.g., `kind()`, `config_schema_json()`, `create_source()`) across the shared library
//! boundary. Both plugin and server **must** be compiled with the same Rust toolchain
//! and the same `drasi-plugin-sdk` version for this to work correctly.
//!
//! # Schema Naming Convention
//!
//! Schema names follow the pattern `{component_type}.{kind}.{TypeName}` to avoid
//! collisions when multiple plugins are loaded into the same OpenAPI spec.
//!
//! Examples:
//! - `source.postgres.PostgresSourceConfig`
//! - `reaction.http.HttpReactionConfig`
//! - `bootstrap.mssql.MssqlBootstrapConfig`
//!
//! # Complete Example
//!
//! ```rust,ignore
//! use drasi_plugin_sdk::prelude::*;
//! use drasi_lib::sources::Source;
//!
//! /// Descriptor for the PostgreSQL source plugin.
//! pub struct PostgresSourceDescriptor;
//!
//! #[async_trait]
//! impl SourcePluginDescriptor for PostgresSourceDescriptor {
//! fn kind(&self) -> &str {
//! "postgres"
//! }
//!
//! fn config_version(&self) -> &str {
//! "1.0.0"
//! }
//!
//! fn config_schema_json(&self) -> String {
//! let schema = <PostgresSourceConfigDto as utoipa::ToSchema>::schema();
//! serde_json::to_string(&schema).unwrap()
//! }
//!
//! async fn create_source(
//! &self,
//! id: &str,
//! config_json: &serde_json::Value,
//! auto_start: bool,
//! ) -> anyhow::Result<Box<dyn Source>> {
//! let dto: PostgresSourceConfigDto = serde_json::from_value(config_json.clone())?;
//! let mapper = DtoMapper::new();
//! let host = mapper.resolve_string(&dto.host)?;
//! let port = mapper.resolve_typed(&dto.port)?;
//! // ... build and return the source
//! todo!()
//! }
//! }
//! ```
use async_trait;
use BootstrapProvider;
use Reaction;
use Source;
/// Descriptor for a **source** plugin.
///
/// Source plugins ingest data from external systems (databases, APIs, message queues)
/// and feed change events into the Drasi query engine.
///
/// # Implementors
///
/// Each source plugin crate (e.g., `drasi-source-postgres`) implements this trait
/// on a zero-sized descriptor struct and returns it via [`PluginRegistration`].
///
/// See the [module docs](self) for a complete example.
/// Descriptor for a **reaction** plugin.
///
/// Reaction plugins consume query results and perform side effects (webhooks,
/// logging, stored procedures, SSE streams, etc.).
///
/// # Implementors
///
/// Each reaction plugin crate (e.g., `drasi-reaction-http`) implements this trait
/// on a zero-sized descriptor struct and returns it via [`PluginRegistration`].
/// Descriptor for a **bootstrap** plugin.
///
/// Bootstrap plugins provide initial data snapshots for sources when queries
/// first subscribe. They deliver historical/current state so queries start with
/// a complete view of the data.
///
/// # Implementors
///
/// Each bootstrap plugin crate (e.g., `drasi-bootstrap-postgres`) implements this trait
/// on a zero-sized descriptor struct and returns it via [`PluginRegistration`].