cli-engine 0.2.2

Rust CLI framework for consistent command modules
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
use std::{
    collections::{BTreeMap, BTreeSet},
    sync::{OnceLock, RwLock},
};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Compact field summary used in help text and schema output.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct FieldInfo {
    /// Field name.
    pub name: String,
    /// Field type label.
    #[serde(rename = "type")]
    pub field_type: String,
    /// Whether the field is optional or nullable.
    pub optional: bool,
}

impl FieldInfo {
    /// Creates a compact field summary.
    #[must_use]
    pub fn new(name: impl Into<String>, field_type: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            field_type: field_type.into(),
            optional: false,
        }
    }

    /// Marks the field as optional or nullable.
    #[must_use]
    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }
}

/// Schema information returned by `--schema`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SchemaInfo {
    /// Colon-separated command path.
    pub command: String,
    /// Compact field summary for help and quick inspection.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<FieldInfo>,
    /// Full JSON Schema when available.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<Value>,
}

impl SchemaInfo {
    /// Creates an empty schema record for a command path.
    #[must_use]
    pub fn new(command: impl Into<String>) -> Self {
        Self {
            command: command.into(),
            fields: Vec::new(),
            schema: None,
        }
    }

    /// Adds compact field summaries.
    #[must_use]
    pub fn with_fields(mut self, fields: impl Into<Vec<FieldInfo>>) -> Self {
        self.fields = fields.into();
        self
    }

    /// Adds a full JSON Schema document.
    #[must_use]
    pub fn with_schema(mut self, schema: Value) -> Self {
        self.schema = Some(schema);
        self
    }
}

/// Manual output field descriptor.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OutputField {
    /// Field name.
    pub name: &'static str,
    /// Field type label.
    pub field_type: &'static str,
    /// Whether the field is optional.
    pub optional: bool,
}

impl OutputField {
    /// Creates a field descriptor.
    #[must_use]
    pub const fn new(name: &'static str, field_type: &'static str) -> Self {
        Self {
            name,
            field_type,
            optional: false,
        }
    }

    /// Marks the field optional.
    #[must_use]
    pub const fn optional(mut self) -> Self {
        self.optional = true;
        self
    }

    /// Creates a string field.
    #[must_use]
    pub const fn string(name: &'static str) -> Self {
        Self::new(name, "string")
    }

    /// Creates an integer field.
    #[must_use]
    pub const fn int(name: &'static str) -> Self {
        Self::new(name, "int")
    }

    /// Creates a float field.
    #[must_use]
    pub const fn float(name: &'static str) -> Self {
        Self::new(name, "float")
    }

    /// Creates a boolean field.
    #[must_use]
    pub const fn bool(name: &'static str) -> Self {
        Self::new(name, "bool")
    }

    /// Creates a list field with a custom type label.
    #[must_use]
    pub const fn list(name: &'static str, field_type: &'static str) -> Self {
        Self::new(name, field_type)
    }

    /// Creates a string-list field.
    #[must_use]
    pub const fn string_list(name: &'static str) -> Self {
        Self::new(name, "[]string")
    }

    /// Creates an integer-list field.
    #[must_use]
    pub const fn int_list(name: &'static str) -> Self {
        Self::new(name, "[]int")
    }

    /// Creates a float-list field.
    #[must_use]
    pub const fn float_list(name: &'static str) -> Self {
        Self::new(name, "[]float")
    }

    /// Creates a boolean-list field.
    #[must_use]
    pub const fn bool_list(name: &'static str) -> Self {
        Self::new(name, "[]bool")
    }

    /// Creates an object-list field.
    #[must_use]
    pub const fn object_list(name: &'static str) -> Self {
        Self::new(name, "[]object")
    }

    /// Creates an object field.
    #[must_use]
    pub const fn object(name: &'static str) -> Self {
        Self::new(name, "object")
    }

    /// Creates a field with unknown or mixed type.
    #[must_use]
    pub const fn any(name: &'static str) -> Self {
        Self::new(name, "any")
    }
}

/// Trait for manually declared output schemas.
pub trait OutputSchema {
    /// Returns the schema's compact field descriptors.
    fn fields() -> &'static [OutputField];
}

/// Registry for command output schemas.
#[derive(Clone, Debug, Default)]
pub struct SchemaRegistry {
    by_path: BTreeMap<String, SchemaInfo>,
}

impl SchemaRegistry {
    /// Creates an empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers a manual schema for a command path.
    pub fn register<T: OutputSchema>(&mut self, command_path: impl Into<String>) {
        self.register_fields(command_path, fields_for::<T>());
    }

    /// Registers a `schemars` JSON Schema for a command path.
    pub fn register_json_schema<T: JsonSchema>(&mut self, command_path: impl Into<String>) {
        let command_path = command_path.into();
        self.register_info(command_path.clone(), json_schema_info::<T>(command_path));
    }

    /// Registers compact field summaries for a command path.
    pub fn register_fields(
        &mut self,
        command_path: impl Into<String>,
        fields: impl Into<Vec<FieldInfo>>,
    ) {
        let command_path = command_path.into();
        self.register_info(
            command_path.clone(),
            SchemaInfo {
                command: command_path,
                fields: fields.into(),
                schema: None,
            },
        );
    }

    /// Registers a complete schema record for a command path.
    pub fn register_info(&mut self, command_path: impl Into<String>, mut info: SchemaInfo) {
        let command_path = command_path.into();
        info.command = command_path.clone();
        self.by_path.insert(command_path, info);
    }

    /// Merges another registry into this one.
    pub fn merge(&mut self, other: &Self) {
        self.by_path.extend(other.by_path.clone());
    }

    /// Looks up schema information by colon-separated or space-separated path.
    #[must_use]
    pub fn get_by_path(&self, command_path: &str) -> Option<SchemaInfo> {
        if let Some(info) = self.by_path.get(command_path) {
            return Some(schema_with_command(info, command_path));
        }

        let space_path = command_path.replace(':', " ");
        self.by_path.iter().find_map(|(registered, info)| {
            let matches = registered == &space_path
                || registered
                    .split_once(' ')
                    .is_some_and(|(_, without_root)| without_root == space_path);
            matches.then(|| schema_with_command(info, command_path))
        })
    }
}

fn schema_with_command(info: &SchemaInfo, command_path: &str) -> SchemaInfo {
    SchemaInfo {
        command: command_path.to_owned(),
        fields: info.fields.clone(),
        schema: info.schema.clone(),
    }
}

/// Converts an [`OutputSchema`] implementation to compact field info.
#[must_use]
pub fn fields_for<T: OutputSchema>() -> Vec<FieldInfo> {
    T::fields()
        .iter()
        .map(|field| FieldInfo {
            name: field.name.to_owned(),
            field_type: field.field_type.to_owned(),
            optional: field.optional,
        })
        .collect()
}

/// Generates JSON Schema for a Rust type.
#[must_use]
pub fn json_schema_for<T: JsonSchema>() -> Value {
    serde_json::to_value(schemars::schema_for!(T)).unwrap_or(Value::Null)
}

/// Builds schema info from a Rust type's JSON Schema.
#[must_use]
pub fn json_schema_info<T: JsonSchema>(command_path: impl Into<String>) -> SchemaInfo {
    let schema = json_schema_for::<T>();
    SchemaInfo {
        command: command_path.into(),
        fields: fields_from_json_schema(&schema),
        schema: Some(schema),
    }
}

/// Extracts compact field summaries from a JSON Schema object.
#[must_use]
pub fn fields_from_json_schema(schema: &Value) -> Vec<FieldInfo> {
    let Some(properties) = schema.get("properties").and_then(Value::as_object) else {
        return Vec::new();
    };
    let required = schema
        .get("required")
        .and_then(Value::as_array)
        .map(|items| {
            items
                .iter()
                .filter_map(Value::as_str)
                .collect::<BTreeSet<_>>()
        })
        .unwrap_or_default();

    properties
        .iter()
        .map(|(name, property)| FieldInfo {
            name: name.clone(),
            field_type: json_schema_type_name(property, schema),
            optional: !required.contains(name.as_str()) || schema_allows_null(property),
        })
        .collect()
}

fn json_schema_type_name(schema: &Value, root: &Value) -> String {
    let schema = non_null_schema(schema);
    let schema = resolve_local_ref(schema, root).unwrap_or(schema);
    match primary_json_type(schema).as_deref() {
        Some("string") => "string".to_owned(),
        Some("integer") => "int".to_owned(),
        Some("number") => "float".to_owned(),
        Some("boolean") => "bool".to_owned(),
        Some("array") => {
            let item_type = schema
                .get("items")
                .map(|items| json_schema_type_name(items, root))
                .unwrap_or_else(|| "any".to_owned());
            format!("[]{item_type}")
        }
        Some("object") => "object".to_owned(),
        Some(other) => other.to_owned(),
        None => {
            if schema.get("properties").is_some() {
                "object".to_owned()
            } else {
                "any".to_owned()
            }
        }
    }
}

fn non_null_schema(schema: &Value) -> &Value {
    for key in ["anyOf", "oneOf"] {
        if let Some(items) = schema.get(key).and_then(Value::as_array)
            && let Some(non_null) = items
                .iter()
                .find(|item| item.get("type").and_then(Value::as_str) != Some("null"))
        {
            return non_null;
        }
    }
    schema
}

fn resolve_local_ref<'schema>(
    schema: &'schema Value,
    root: &'schema Value,
) -> Option<&'schema Value> {
    let reference = schema.get("$ref").and_then(Value::as_str)?;
    let pointer = reference.strip_prefix('#')?;
    root.pointer(pointer)
}

fn primary_json_type(schema: &Value) -> Option<String> {
    match schema.get("type") {
        Some(Value::String(value)) => Some(value.clone()),
        Some(Value::Array(values)) => values
            .iter()
            .filter_map(Value::as_str)
            .find(|value| *value != "null")
            .map(str::to_owned),
        Some(Value::Null | Value::Bool(_) | Value::Number(_) | Value::Object(_)) | None => None,
    }
}

fn schema_allows_null(schema: &Value) -> bool {
    matches!(schema.get("type"), Some(Value::String(value)) if value == "null")
        || schema
            .get("type")
            .and_then(Value::as_array)
            .is_some_and(|items| items.iter().any(|item| item.as_str() == Some("null")))
        || ["anyOf", "oneOf"].iter().any(|key| {
            schema
                .get(key)
                .and_then(Value::as_array)
                .is_some_and(|items| {
                    items
                        .iter()
                        .any(|item| item.get("type").and_then(Value::as_str) == Some("null"))
                })
        })
}

static GLOBAL_SCHEMA_REGISTRY: OnceLock<RwLock<SchemaRegistry>> = OnceLock::new();

fn global_schema_registry() -> &'static RwLock<SchemaRegistry> {
    GLOBAL_SCHEMA_REGISTRY.get_or_init(|| RwLock::new(SchemaRegistry::new()))
}

/// Registers a process-global manual schema.
pub fn register_global_schema<T: OutputSchema>(command_path: impl Into<String>) {
    let mut registry = global_schema_registry()
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    registry.register::<T>(command_path);
}

/// Registers a process-global JSON Schema.
pub fn register_global_json_schema<T: JsonSchema>(command_path: impl Into<String>) {
    let mut registry = global_schema_registry()
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    registry.register_json_schema::<T>(command_path);
}

/// Registers process-global compact field summaries.
pub fn register_global_schema_fields(
    command_path: impl Into<String>,
    fields: impl Into<Vec<FieldInfo>>,
) {
    let mut registry = global_schema_registry()
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    registry.register_fields(command_path, fields);
}

/// Registers process-global schema info.
pub fn register_global_schema_info(command_path: impl Into<String>, info: SchemaInfo) {
    let mut registry = global_schema_registry()
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    registry.register_info(command_path, info);
}

/// Looks up a process-global schema by command path.
#[must_use]
pub fn get_global_schema_by_path(command_path: &str) -> Option<SchemaInfo> {
    global_schema_registry()
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .get_by_path(command_path)
}

/// Returns a snapshot of the process-global schema registry.
#[must_use]
pub fn global_schema_registry_snapshot() -> SchemaRegistry {
    global_schema_registry()
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .clone()
}

/// Formats compact field summaries for command long help.
#[must_use]
pub fn format_help_section(fields: &[FieldInfo]) -> String {
    if fields.is_empty() {
        return String::new();
    }
    let max_name = fields
        .iter()
        .map(|field| field.name.len())
        .max()
        .unwrap_or_default();
    let mut out = String::from("Output fields:\n");
    for field in fields {
        let optional = if field.optional { "  (optional)" } else { "" };
        out.push_str(&format!(
            "  {:<width$}  {}{}\n",
            field.name,
            field.field_type,
            optional,
            width = max_name
        ));
    }

    let first_string = fields
        .iter()
        .find(|field| field.field_type == "string")
        .map(|field| field.name.as_str());
    let first_bool = fields
        .iter()
        .find(|field| field.field_type == "bool")
        .map(|field| field.name.as_str());
    if first_string.is_some() || first_bool.is_some() {
        out.push_str("\nFilter examples:\n");
        if let Some(name) = first_string {
            out.push_str(&format!("  --filter \"contains({name}, 'example')\"\n"));
        }
        if let Some(name) = first_bool {
            out.push_str(&format!("  --filter '{name}'\n"));
        }
    }

    out.push_str("\nExpr examples:\n");
    out.push_str("  --expr 'length(@)'\n");
    if let Some(name) = first_string {
        out.push_str(&format!("  --expr '[].{name}'\n"));
    }
    out
}