drasi-plugin-sdk 0.8.4

SDK for building Drasi plugins (sources, reactions, bootstrappers)
Documentation
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
// Copyright 2026 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.

//! UI hint annotations for plugin configuration schemas.
//!
//! Since utoipa doesn't support property-level OpenAPI extensions, this module
//! provides a post-processing builder that injects `x-ui:*` extension properties
//! into schema JSON. These hints are consumed by the Drasi UI to render
//! rich, schema-driven configuration forms instead of flat YAML editors.
//!
//! # Supported Extensions
//!
//! - `x-ui:widget` — Override widget type: `"password"`, `"textarea"`, `"slider"`, `"hidden"`, `"code-editor"`
//! - `x-ui:group` — Group name for section grouping (e.g., `"Connection"`, `"Authentication"`)
//! - `x-ui:order` — Display order within a group (lower = first)
//! - `x-ui:placeholder` — Placeholder text for input fields
//! - `x-ui:help` — Help text displayed below the field
//! - `x-ui:condition` — Conditional visibility: `{"field": "fieldName", "value": "expectedValue"}` or `{"field": "fieldName", "notEmpty": true}`
//! - `x-ui:collapsed` — Whether the group containing this field starts collapsed
//!
//! # Example
//!
//! ```rust,ignore
//! use drasi_plugin_sdk::schema_ui::SchemaUiAnnotator;
//!
//! fn config_schema_json(&self) -> String {
//!     let api = MySchemas::openapi();
//!     let schemas = api.components.as_ref().unwrap().schemas.clone();
//!     let schemas_value = serde_json::to_value(&schemas).unwrap();
//!
//!     SchemaUiAnnotator::new(schemas_value, "source.postgres.PostgresSourceConfig")
//!         .expect("root schema not found")
//!         .field("host", |f| f.group("Connection").order(1).placeholder("localhost"))
//!         .field("password", |f| f.group("Authentication").widget("password"))
//!         .annotate()
//! }
//! ```

use serde_json::{Map, Value};
use std::fmt;

/// Errors that can occur when building or applying UI annotations.
#[derive(Debug)]
pub enum SchemaUiError {
    /// The `root_schema_name` was not found in the schemas map.
    RootSchemaNotFound {
        /// The schema name that was looked up.
        name: String,
    },
}

impl fmt::Display for SchemaUiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SchemaUiError::RootSchemaNotFound { name } => {
                write!(
                    f,
                    "SchemaUiAnnotator: root schema '{name}' not found in schemas map",
                )
            }
        }
    }
}

impl std::error::Error for SchemaUiError {}

/// Builder for a single field's UI annotations.
#[derive(Debug)]
pub struct FieldUiBuilder {
    annotations: Map<String, Value>,
}

impl FieldUiBuilder {
    fn new() -> Self {
        Self {
            annotations: Map::new(),
        }
    }

    /// Set the widget type override.
    pub fn widget(mut self, widget: &str) -> Self {
        self.annotations
            .insert("x-ui:widget".to_string(), Value::String(widget.to_string()));
        self
    }

    /// Set the group name for section grouping.
    pub fn group(mut self, group: &str) -> Self {
        self.annotations
            .insert("x-ui:group".to_string(), Value::String(group.to_string()));
        self
    }

    /// Set the display order within a group.
    pub fn order(mut self, order: i64) -> Self {
        self.annotations
            .insert("x-ui:order".to_string(), Value::Number(order.into()));
        self
    }

    /// Set placeholder text.
    pub fn placeholder(mut self, placeholder: &str) -> Self {
        self.annotations.insert(
            "x-ui:placeholder".to_string(),
            Value::String(placeholder.to_string()),
        );
        self
    }

    /// Set help text displayed below the field.
    pub fn help(mut self, help: &str) -> Self {
        self.annotations
            .insert("x-ui:help".to_string(), Value::String(help.to_string()));
        self
    }

    /// Set conditional visibility based on a field matching a specific value.
    pub fn condition_value(mut self, field: &str, value: &str) -> Self {
        let mut condition = Map::new();
        condition.insert("field".to_string(), Value::String(field.to_string()));
        condition.insert("value".to_string(), Value::String(value.to_string()));
        self.annotations
            .insert("x-ui:condition".to_string(), Value::Object(condition));
        self
    }

    /// Set conditional visibility based on a field being non-empty.
    pub fn condition_not_empty(mut self, field: &str) -> Self {
        let mut condition = Map::new();
        condition.insert("field".to_string(), Value::String(field.to_string()));
        condition.insert("notEmpty".to_string(), Value::Bool(true));
        self.annotations
            .insert("x-ui:condition".to_string(), Value::Object(condition));
        self
    }

    /// Sets `x-ui:collapsed` on this field, signalling that the group
    /// containing this field starts collapsed by default.
    ///
    /// This is a group-level concept expressed on the field builder for
    /// convenience. If multiple fields in the same group set conflicting
    /// values, the last one written wins.
    pub fn collapsed(mut self, collapsed: bool) -> Self {
        self.annotations
            .insert("x-ui:collapsed".to_string(), Value::Bool(collapsed));
        self
    }
}

/// Annotates an OpenAPI schema map with `x-ui:*` UI hint extensions.
#[derive(Debug)]
pub struct SchemaUiAnnotator {
    schemas: Value,
    root_schema_name: String,
    field_annotations: Vec<(String, FieldUiBuilder)>,
}

impl SchemaUiAnnotator {
    /// Create a new annotator from a `serde_json::Value` representing the schemas map.
    ///
    /// Returns `Err(SchemaUiError::RootSchemaNotFound)` if `root_schema_name` does not
    /// exist as a key in the schemas map.
    ///
    /// `root_schema_name` is the key in the map that identifies the root config schema
    /// (e.g., `"source.postgres.PostgresSourceConfig"`).
    pub fn new(schemas: Value, root_schema_name: &str) -> Result<Self, SchemaUiError> {
        if schemas.get(root_schema_name).is_none() {
            return Err(SchemaUiError::RootSchemaNotFound {
                name: root_schema_name.to_string(),
            });
        }
        Ok(Self {
            schemas,
            root_schema_name: root_schema_name.to_string(),
            field_annotations: Vec::new(),
        })
    }

    /// Add UI annotations for a field.
    pub fn field<F>(mut self, field_name: &str, builder_fn: F) -> Self
    where
        F: FnOnce(FieldUiBuilder) -> FieldUiBuilder,
    {
        let builder = builder_fn(FieldUiBuilder::new());
        self.field_annotations
            .push((field_name.to_string(), builder));
        self
    }

    /// Apply all annotations and return the modified JSON string.
    ///
    /// Fields named in `.field()` calls that do not exist in the root schema's
    /// `properties` are silently skipped (a `debug_assert!` fires in debug builds).
    pub fn annotate(mut self) -> String {
        if let Some(root) = self.schemas.get_mut(&self.root_schema_name) {
            if let Some(properties) = root.get_mut("properties") {
                for (field_name, builder) in &self.field_annotations {
                    if let Some(prop) = properties.get_mut(field_name) {
                        if let Some(obj) = prop.as_object_mut() {
                            for (key, value) in &builder.annotations {
                                obj.insert(key.clone(), value.clone());
                            }
                        }
                    } else {
                        debug_assert!(
                            false,
                            "SchemaUiAnnotator: field '{}' not found in properties of '{}'",
                            field_name, self.root_schema_name
                        );
                    }
                }
            }
        }
        serde_json::to_string(&self.schemas).expect("SchemaUiAnnotator: failed to serialize")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn test_schema() -> Value {
        json!({
            "my.Config": {
                "type": "object",
                "properties": {
                    "host": { "type": "string" },
                    "port": { "type": "integer" },
                    "password": { "type": "string" },
                    "authMode": { "type": "string" },
                    "token": { "type": "string" }
                }
            }
        })
    }

    #[test]
    fn happy_path_annotations_applied() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("host", |f| {
                f.group("Connection").order(1).placeholder("localhost")
            })
            .field("password", |f| f.group("Auth").widget("password"))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        let host = &parsed["my.Config"]["properties"]["host"];
        assert_eq!(host["x-ui:group"], "Connection");
        assert_eq!(host["x-ui:order"], 1);
        assert_eq!(host["x-ui:placeholder"], "localhost");

        let pw = &parsed["my.Config"]["properties"]["password"];
        assert_eq!(pw["x-ui:group"], "Auth");
        assert_eq!(pw["x-ui:widget"], "password");
    }

    #[test]
    fn unknown_field_silently_skipped_in_release() {
        // In debug builds, unknown fields trigger a debug_assert.
        // This test verifies that known fields are still annotated even
        // when an unknown field is also specified.
        // The debug_assert is tested separately below.
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("host", |f| f.group("Connection"))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        assert_eq!(
            parsed["my.Config"]["properties"]["host"]["x-ui:group"],
            "Connection"
        );
    }

    #[test]
    #[should_panic(expected = "not found in properties")]
    fn unknown_field_debug_asserts_in_debug_builds() {
        SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("nonexistent", |f| f.group("Oops"))
            .annotate();
    }

    #[test]
    fn missing_root_schema_returns_error() {
        let err = SchemaUiAnnotator::new(test_schema(), "wrong.Name").unwrap_err();
        match err {
            SchemaUiError::RootSchemaNotFound { name } => {
                assert_eq!(name, "wrong.Name");
            }
        }
    }

    #[test]
    fn multiple_annotations_on_same_field() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("host", |f| {
                f.group("Connection")
                    .order(1)
                    .placeholder("localhost")
                    .widget("textarea")
                    .help("Enter the hostname")
            })
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        let host = &parsed["my.Config"]["properties"]["host"];
        assert_eq!(host["x-ui:group"], "Connection");
        assert_eq!(host["x-ui:order"], 1);
        assert_eq!(host["x-ui:placeholder"], "localhost");
        assert_eq!(host["x-ui:widget"], "textarea");
        assert_eq!(host["x-ui:help"], "Enter the hostname");
    }

    #[test]
    fn condition_value_produces_correct_json() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("token", |f| f.condition_value("authMode", "token"))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        let cond = &parsed["my.Config"]["properties"]["token"]["x-ui:condition"];
        assert_eq!(cond["field"], "authMode");
        assert_eq!(cond["value"], "token");
    }

    #[test]
    fn condition_not_empty_produces_correct_json() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("token", |f| f.condition_not_empty("authMode"))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        let cond = &parsed["my.Config"]["properties"]["token"]["x-ui:condition"];
        assert_eq!(cond["field"], "authMode");
        assert_eq!(cond["notEmpty"], true);
    }

    #[test]
    fn collapsed_annotation_works() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("host", |f| f.group("Connection").collapsed(true))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        assert_eq!(
            parsed["my.Config"]["properties"]["host"]["x-ui:collapsed"],
            true
        );
    }

    #[test]
    fn help_annotation_works() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("host", |f| f.help("The server hostname or IP"))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        assert_eq!(
            parsed["my.Config"]["properties"]["host"]["x-ui:help"],
            "The server hostname or IP"
        );
    }

    #[test]
    fn unannotated_fields_unchanged() {
        let result = SchemaUiAnnotator::new(test_schema(), "my.Config")
            .unwrap()
            .field("host", |f| f.group("Connection"))
            .annotate();

        let parsed: Value = serde_json::from_str(&result).unwrap();
        // port was not annotated — should have no x-ui keys
        let port = &parsed["my.Config"]["properties"]["port"];
        assert_eq!(port["type"], "integer");
        assert!(port.get("x-ui:group").is_none());
    }

    #[test]
    fn error_display_message() {
        let err = SchemaUiError::RootSchemaNotFound {
            name: "bad.Name".to_string(),
        };
        assert!(err.to_string().contains("bad.Name"));
        assert!(err.to_string().contains("not found"));
    }
}