schemaui 0.5.0

A Rust library for generating TUI and Web UIs from JSON Schemas for configuration management.
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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::{
    cell::{RefCell, RefMut},
    sync::Arc,
};

use serde_json::{Map, Value, json};

use crate::{
    tui::model::{CompositeField, CompositeMode, form_schema_from_ui_ast},
    ui_ast::build_ui_ast,
};

use super::{
    error::FieldCoercionError, field::components::ComponentPalette, form_state::FormState,
};

mod composite_list;

pub use composite_list::{CompositeListEditorContext, CompositeListState};

#[derive(Debug, Clone)]
pub struct CompositeState {
    pointer: String,
    mode: CompositeMode,
    variants: Vec<CompositeVariantState>,
}

#[derive(Debug, Clone)]
pub struct CompositeVariantState {
    #[allow(dead_code)]
    id: String,
    title: String,
    #[allow(dead_code)]
    description: Option<String>,
    schema: Value,
    active: bool,
    form: RefCell<Option<FormState>>,
    palette: Arc<ComponentPalette>,
    shape: VariantShape,
}

#[derive(Debug, Clone)]
pub struct CompositeEditorSession {
    pub variant_index: usize,
    pub title: String,
    pub description: Option<String>,
    pub form_state: FormState,
    pub schema: Value,
}
#[derive(Debug, Clone)]
pub struct CompositeVariantSummary {
    pub title: String,
    pub description: Option<String>,
    pub lines: Vec<String>,
}

const WRAPPED_FIELD_NAME: &str = "__value";

#[derive(Debug, Clone, Copy)]
enum VariantShape {
    Object,
    Wrapped,
}
impl CompositeState {
    pub fn new(pointer: &str, field: &CompositeField, palette: Arc<ComponentPalette>) -> Self {
        let mut variants = Vec::with_capacity(field.variants.len());
        for (index, variant) in field.variants.iter().enumerate() {
            variants.push(CompositeVariantState {
                id: variant.id.clone(),
                title: variant.title.clone(),
                description: variant.description.clone(),
                schema: variant.schema.clone(),
                active: matches!(field.mode, CompositeMode::OneOf) && index == 0,
                form: RefCell::new(None),
                palette: Arc::clone(&palette),
                shape: if variant.is_object {
                    VariantShape::Object
                } else {
                    VariantShape::Wrapped
                },
            });
        }

        Self {
            pointer: pointer.to_string(),
            mode: field.mode.clone(),
            variants,
        }
    }

    pub fn summary(&self) -> String {
        match self.mode {
            CompositeMode::OneOf => self
                .variants
                .iter()
                .enumerate()
                .find(|(_, variant)| variant.active)
                .map(|(idx, variant)| format!("Variant: #{} {}", idx + 1, variant.title))
                .unwrap_or_else(|| "Variant: <none>".to_string()),
            CompositeMode::AnyOf => {
                let active = self
                    .variants
                    .iter()
                    .enumerate()
                    .filter(|(_, variant)| variant.active)
                    .map(|(idx, variant)| format!("#{} {}", idx + 1, variant.title))
                    .collect::<Vec<_>>();
                if active.is_empty() {
                    "Variants: []".to_string()
                } else {
                    format!("Variants: {}", active.join(", "))
                }
            }
        }
    }

    pub fn pointer(&self) -> &str {
        &self.pointer
    }

    pub fn rebind_pointer(&mut self, pointer: &str) {
        self.pointer = pointer.to_string();
    }

    fn pick_variant_index(&self, value: &Value) -> usize {
        if let Value::Object(obj) = value {
            for (idx, variant) in self.variants.iter().enumerate() {
                if variant.matches_value(obj) {
                    return idx;
                }
            }
        }
        0
    }

    pub fn seed_from_value(&mut self, value: &Value) -> Result<(), FieldCoercionError> {
        if self.variants.is_empty() {
            return Ok(());
        }
        let target = self.pick_variant_index(value);
        let pointer = self.pointer.clone();
        for (idx, variant) in self.variants.iter_mut().enumerate() {
            variant.active = idx == target;
            if variant.active {
                let mut form = variant.borrow_form(&pointer)?;
                let mut scratch = None;
                let seed_value = variant.seed_payload(value, &mut scratch);
                form.seed_from_value(seed_value);
            }
        }
        Ok(())
    }

    pub fn is_multi(&self) -> bool {
        matches!(self.mode, CompositeMode::AnyOf)
    }

    pub fn active_summaries(&self) -> Vec<CompositeVariantSummary> {
        let mut summaries = Vec::new();
        for (idx, variant) in self.variants.iter().enumerate() {
            if !variant.active {
                continue;
            }
            match variant.snapshot(self.pointer()) {
                Ok(mut summary) => {
                    summary.title = format!("#{} {}", idx + 1, summary.title);
                    summaries.push(summary);
                }
                Err(err) => summaries.push(CompositeVariantSummary {
                    title: format!("#{} {}", idx + 1, variant.title.clone()),
                    description: variant.description.clone(),
                    lines: vec![format!("Error: {}", err.message)],
                }),
            }
        }
        summaries
    }

    pub fn option_titles(&self) -> Vec<String> {
        self.variants
            .iter()
            .map(|variant| variant.title.clone())
            .collect()
    }

    pub fn variant_descriptions(&self) -> Vec<Option<String>> {
        self.variants
            .iter()
            .map(|variant| variant.description.clone())
            .collect()
    }

    pub fn selected_index(&self) -> Option<usize> {
        self.variants.iter().position(|variant| variant.active)
    }

    pub fn active_flags(&self) -> Vec<bool> {
        self.variants.iter().map(|variant| variant.active).collect()
    }

    pub fn active_indices(&self) -> Vec<usize> {
        self.variants
            .iter()
            .enumerate()
            .filter_map(|(idx, variant)| if variant.active { Some(idx) } else { None })
            .collect()
    }

    pub fn apply_single(&mut self, index: usize) -> bool {
        if !matches!(self.mode, CompositeMode::OneOf) {
            return false;
        }
        if self.variants.is_empty() {
            return false;
        }
        let target = index.min(self.variants.len() - 1);
        let mut changed = false;
        for (idx, variant) in self.variants.iter_mut().enumerate() {
            let next_state = idx == target;
            if variant.active != next_state {
                variant.active = next_state;
                changed = true;
            }
        }
        changed
    }

    pub fn rotate_single(&mut self, delta: i32) -> bool {
        if !matches!(self.mode, CompositeMode::OneOf) || self.variants.is_empty() {
            return false;
        }
        let len = self.variants.len() as i32;
        let current = self.selected_index().unwrap_or(0) as i32;
        let next = (current + delta).rem_euclid(len);
        if next == current && self.selected_index().is_some() {
            return false;
        }
        self.apply_single(next as usize)
    }

    pub fn ensure_editable_variant(&mut self) {
        self.ensure_editable_variant_with_index(None);
    }

    pub fn ensure_editable_variant_with_index(&mut self, variant_index: Option<usize>) {
        if self.variants.is_empty() {
            return;
        }
        if self.active_indices().is_empty() {
            let target_index = variant_index.unwrap_or(0).min(self.variants.len() - 1);
            match self.mode {
                CompositeMode::OneOf => {
                    let _ = self.apply_single(target_index);
                }
                CompositeMode::AnyOf => {
                    if let Some(variant) = self.variants.get_mut(target_index) {
                        variant.active = true;
                    }
                }
            }
        }
    }

    pub fn take_editor_session(
        &self,
        pointer: &str,
        variant_index: usize,
    ) -> Result<CompositeEditorSession, FieldCoercionError> {
        let variant = self
            .variants
            .get(variant_index)
            .ok_or_else(|| FieldCoercionError {
                pointer: pointer.to_string(),
                message: "invalid variant selection".to_string(),
            })?;
        if !variant.active {
            return Err(FieldCoercionError {
                pointer: pointer.to_string(),
                message: "variant is not active; select it before editing".to_string(),
            });
        }
        let form_state = variant.take_form(pointer)?;
        Ok(CompositeEditorSession {
            variant_index,
            title: variant.title.clone(),
            description: variant.description.clone(),
            form_state,
            schema: variant.overlay_schema(),
        })
    }

    pub fn restore_editor_session(&self, session: CompositeEditorSession) {
        if let Some(variant) = self.variants.get(session.variant_index) {
            variant.store_form(session.form_state);
        }
    }

    pub fn apply_multi(&mut self, flags: &[bool]) -> bool {
        if !matches!(self.mode, CompositeMode::AnyOf) {
            return false;
        }
        if flags.len() != self.variants.len() {
            return false;
        }
        let mut changed = false;
        for (variant, flag) in self.variants.iter_mut().zip(flags.iter()) {
            if variant.active != *flag {
                variant.active = *flag;
                changed = true;
            }
        }
        changed
    }

    pub fn build_value(&self, required: bool) -> Result<Option<Value>, FieldCoercionError> {
        match self.mode {
            CompositeMode::OneOf => {
                if let Some(variant) = self.variants.iter().find(|variant| variant.active) {
                    let form = variant.borrow_form(self.pointer())?;
                    match form.try_build_value() {
                        Ok(value) => {
                            let actual = variant.unwrap_overlay_value(value, self.pointer())?;
                            Ok(Some(actual))
                        }
                        Err(mut err) => {
                            err.pointer = join_pointer(self.pointer(), &err.pointer);
                            Err(err)
                        }
                    }
                } else if required {
                    Err(FieldCoercionError {
                        pointer: self.pointer.clone(),
                        message: "oneOf requires a selected variant".to_string(),
                    })
                } else {
                    Ok(None)
                }
            }
            CompositeMode::AnyOf => {
                // anyOf: 选择第一个激活的变体作为主值
                // 多个激活的变体主要用于验证:告诉系统这个值应该同时符合多个 schema
                let active_variants: Vec<_> = self
                    .variants
                    .iter()
                    .filter(|variant| variant.active)
                    .collect();

                if active_variants.is_empty() {
                    if required {
                        Err(FieldCoercionError {
                            pointer: self.pointer.clone(),
                            message: "anyOf requires at least one active variant".to_string(),
                        })
                    } else {
                        Ok(None)
                    }
                } else {
                    // 返回第一个激活变体的值
                    // 注意:anyOf 不是返回数组,而是一个值可以同时满足多个 schema
                    let primary = active_variants[0];
                    let form = primary.borrow_form(self.pointer())?;
                    match form.try_build_value() {
                        Ok(value) => {
                            let actual = primary.unwrap_overlay_value(value, self.pointer())?;
                            Ok(Some(actual))
                        }
                        Err(mut err) => {
                            err.pointer = join_pointer(self.pointer(), &err.pointer);
                            Err(err)
                        }
                    }
                }
            }
        }
    }
}

fn wrap_non_object_schema(schema: &Value, title: &str, description: Option<&String>) -> Value {
    let mut property = schema.clone();
    if let Value::Object(ref mut map) = property {
        map.entry("title".to_string())
            .or_insert_with(|| Value::String(title.to_string()));
        if let Some(desc) = description
            && !map.contains_key("description")
        {
            map.insert("description".to_string(), Value::String(desc.clone()));
        }
    }
    json!({
        "type": "object",
        "title": title,
        "properties": {
            WRAPPED_FIELD_NAME: property
        },
        "required": [WRAPPED_FIELD_NAME]
    })
}

impl CompositeVariantState {
    fn overlay_schema(&self) -> Value {
        match self.shape {
            VariantShape::Object => self.schema.clone(),
            VariantShape::Wrapped => {
                wrap_non_object_schema(&self.schema, &self.title, self.description.as_ref())
            }
        }
    }

    fn seed_payload<'a>(&self, value: &'a Value, scratch: &'a mut Option<Value>) -> &'a Value {
        if matches!(self.shape, VariantShape::Wrapped) {
            *scratch = Some(json!({ WRAPPED_FIELD_NAME: value }));
            scratch.as_ref().unwrap()
        } else {
            value
        }
    }

    fn unwrap_overlay_value(
        &self,
        value: Value,
        pointer: &str,
    ) -> Result<Value, FieldCoercionError> {
        if !matches!(self.shape, VariantShape::Wrapped) {
            return Ok(value);
        }
        let object = value.as_object().ok_or_else(|| FieldCoercionError {
            pointer: pointer.to_string(),
            message: "overlay payload missing object wrapper".to_string(),
        })?;
        object
            .get(WRAPPED_FIELD_NAME)
            .cloned()
            .ok_or_else(|| FieldCoercionError {
                pointer: pointer.to_string(),
                message: "overlay payload missing wrapped value".to_string(),
            })
    }

    fn ensure_form_ready(&self, pointer: &str) -> Result<(), FieldCoercionError> {
        if self.form.borrow().is_some() {
            return Ok(());
        }
        let schema_value = self.overlay_schema();
        let ui_ast = build_ui_ast(&schema_value).map_err(|err| FieldCoercionError {
            pointer: pointer.to_string(),
            message: format!("failed to parse composite variant '{}': {err}", self.title),
        })?;
        let schema = form_schema_from_ui_ast(&ui_ast);
        *self.form.borrow_mut() = Some(FormState::from_schema_with_palette(
            &schema,
            Arc::clone(&self.palette),
        ));
        Ok(())
    }

    fn borrow_form(&self, pointer: &str) -> Result<RefMut<'_, FormState>, FieldCoercionError> {
        self.ensure_form_ready(pointer)?;
        Ok(RefMut::map(
            self.form.borrow_mut(),
            |slot: &mut Option<FormState>| {
                slot.as_mut().expect("variant form should be initialized")
            },
        ))
    }

    fn take_form(&self, pointer: &str) -> Result<FormState, FieldCoercionError> {
        self.ensure_form_ready(pointer)?;
        Ok(self
            .form
            .borrow_mut()
            .take()
            .expect("variant form should be initialized"))
    }

    fn store_form(&self, form_state: FormState) {
        *self.form.borrow_mut() = Some(form_state);
    }

    fn snapshot(&self, pointer: &str) -> Result<CompositeVariantSummary, FieldCoercionError> {
        let form = self.borrow_form(pointer)?;
        let mut lines = Vec::new();
        if form.roots.iter().all(|root| root.sections.is_empty()) {
            lines.push("No fields defined for this variant.".to_string());
        } else {
            for root in &form.roots {
                for section in &root.sections {
                    let label = if root.title.is_empty() || root.title == section.title {
                        format!("Section: {}", section.title)
                    } else {
                        format!("Section: {}{}", root.title, section.title)
                    };
                    lines.push(label);
                    if section.fields.is_empty() {
                        lines.push("  • <empty>".to_string());
                    } else {
                        for field in &section.fields {
                            lines.push(format!(
                                "{} = {}",
                                field.schema.display_label(),
                                field.display_value()
                            ));
                        }
                    }
                }
            }
        }
        Ok(CompositeVariantSummary {
            title: self.title.clone(),
            description: self.description.clone(),
            lines,
        })
    }

    fn matches_value(&self, value: &Map<String, Value>) -> bool {
        let Some(props) = self.schema.get("properties").and_then(Value::as_object) else {
            return true;
        };

        let mut inspected = false;

        for (key, schema) in props {
            if let Some(expected) = schema.get("const") {
                inspected = true;
                if value.get(key) != Some(expected) {
                    return false;
                }
                continue;
            }

            if let Some(options) = schema.get("enum").and_then(Value::as_array) {
                inspected = true;
                let Some(actual) = value.get(key) else {
                    return false;
                };
                if !options.iter().any(|candidate| candidate == actual) {
                    return false;
                }
            }
        }

        // # true
        inspected
    }
}

fn join_pointer(base: &str, child: &str) -> String {
    match (base.is_empty(), child.is_empty()) {
        (true, true) => String::new(),
        (true, false) => child.to_string(),
        (false, true) => base.to_string(),
        (false, false) =>
        {
            #[allow(clippy::if_same_then_else)]
            if child.starts_with('/') {
                format!("{base}{child}")
            } else if base.ends_with('/') {
                format!("{base}{child}")
            } else {
                format!("{base}/{child}")
            }
        }
    }
}