agent-first-data 0.28.0

A naming convention that lets AI agents understand your data without being told what it means, plus a CLI and library for reading and safely editing structured JSON, TOML, YAML, dotenv, and INI documents.
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use super::build::validate_spec;
use super::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Serializable version-one closed-world CLI registry.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CliSpec {
    pub schema: String,
    pub name: String,
    pub version: String,
    /// Human-facing product name, distinct from the binary identity in `name`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    /// Opaque build identifier (a git SHA, for example). The core only carries
    /// it; what it means is the host's business.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub build: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub about: Option<String>,
    pub lifecycle_output: OutputSpec,
    pub commands: Vec<CommandSpec>,
}

impl CliSpec {
    /// Start a `cli-spec-v1` registry.
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            schema: "cli-spec-v1".to_string(),
            name: name.into(),
            version: version.into(),
            display_name: None,
            build: None,
            about: None,
            lifecycle_output: OutputSpec::protocol_finite(
                ["json", "yaml", "plain"],
                ["split", "stdout", "stderr"],
                "json",
                "split",
            ),
            commands: Vec::new(),
        }
    }

    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = nonempty(about.into());
        self
    }

    pub fn display_name(mut self, display_name: impl Into<String>) -> Self {
        self.display_name = nonempty(display_name.into());
        self
    }

    /// Record an opaque build identifier. Named `build_id` because `build()`
    /// already compiles the registry.
    pub fn build_id(mut self, build: impl Into<String>) -> Self {
        self.build = nonempty(build.into());
        self
    }

    pub fn lifecycle_output(mut self, output: OutputSpec) -> Self {
        self.lifecycle_output = output;
        self
    }

    pub fn command(mut self, command: CommandSpec) -> Self {
        self.commands.push(command);
        self
    }

    /// Validate and compile the registry.
    pub fn build(self) -> Result<BuiltCliSpec, CliSpecError> {
        validate_spec(&self)?;
        Ok(BuiltCliSpec { spec: self })
    }
}

/// One exact command path in a CLI registry.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CommandSpec {
    pub command_path: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub about: Option<String>,
    pub arguments: Vec<ArgSpec>,
    pub combinations: Vec<Combination>,
}

impl CommandSpec {
    pub fn root() -> Self {
        Self::new(std::iter::empty::<String>())
    }

    pub fn new<I, S>(command_path: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            command_path: command_path.into_iter().map(Into::into).collect(),
            about: None,
            arguments: Vec::new(),
            combinations: Vec::new(),
        }
    }

    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = nonempty(about.into());
        self
    }

    pub fn arg(mut self, argument: ArgSpec) -> Self {
        self.arguments.push(argument);
        self
    }

    pub fn combination(mut self, combination: Combination) -> Self {
        self.combinations.push(combination);
        self
    }
}

/// An argument's exact command-local spelling.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ArgSyntax {
    Long { name: String },
    Positional { index: usize },
}

/// Closed portable value type used by CLI specs and resolved invocations.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArgValueType {
    Flag,
    String,
    I64,
    FiniteF64,
    Enum,
    Json,
}

/// A typed value produced by a built CLI registry.
///
/// `Json` deliberately holds the argument's raw source text rather than a
/// parsed `serde_json::Value`. AFDATA turns on `serde_json/arbitrary_precision`
/// and that feature unifies across a whole binary, so a parsed value's number
/// semantics would depend on which other crates happen to be linked in. The
/// text is validated as one JSON value at parse time; deciding what its numbers
/// mean is the caller's choice.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CliValue {
    Bool(bool),
    String(String),
    I64(i64),
    FiniteF64(f64),
    Json(String),
    List(Vec<CliValue>),
}

impl CliValue {
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(value) => Some(*value),
            _ => None,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            Self::String(value) => Some(value),
            _ => None,
        }
    }

    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Self::I64(value) => Some(*value),
            _ => None,
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Self::FiniteF64(value) => Some(*value),
            _ => None,
        }
    }

    /// The raw, still-unparsed JSON source text of a `json` argument.
    pub fn as_json_str(&self) -> Option<&str> {
        match self {
            Self::Json(value) => Some(value),
            _ => None,
        }
    }

    pub fn as_list(&self) -> Option<&[CliValue]> {
        match self {
            Self::List(values) => Some(values),
            _ => None,
        }
    }
}

/// One command-local application argument.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ArgSpec {
    pub argument_id: String,
    pub syntax: ArgSyntax,
    pub value_type: ArgValueType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_name: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub enum_values: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<CliValue>,
    #[serde(default, skip_serializing_if = "is_false")]
    pub repeatable: bool,
    /// This argument's value must never be echoed back.
    ///
    /// The core only consumes the bit: it suppresses help defaults, keeps the
    /// value out of rendered templates, and rejects a serializable default.
    /// Which arguments deserve it is a host convention — `crate::cli_afdata`
    /// derives it from AFDATA's `_secret` suffix.
    #[serde(default, skip_serializing_if = "is_false")]
    pub sensitive: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub about: Option<String>,
}

impl ArgSpec {
    pub fn flag(long: impl Into<String>) -> Self {
        Self::long(long, ArgValueType::Flag, None::<String>)
    }

    pub fn option(long: impl Into<String>, value_name: impl Into<String>) -> Self {
        Self::long(long, ArgValueType::String, Some(value_name.into()))
    }

    pub fn option_i64(long: impl Into<String>, value_name: impl Into<String>) -> Self {
        Self::long(long, ArgValueType::I64, Some(value_name.into()))
    }

    pub fn option_f64(long: impl Into<String>, value_name: impl Into<String>) -> Self {
        Self::long(long, ArgValueType::FiniteF64, Some(value_name.into()))
    }

    pub fn option_json(long: impl Into<String>, value_name: impl Into<String>) -> Self {
        Self::long(long, ArgValueType::Json, Some(value_name.into()))
    }

    pub fn option_enum<I, S>(long: impl Into<String>, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut spec = Self::long(long, ArgValueType::Enum, Some("VALUE".to_string()));
        spec.enum_values = values.into_iter().map(Into::into).collect();
        spec
    }

    pub fn positional(
        argument_id: impl Into<String>,
        index: usize,
        value_name: impl Into<String>,
    ) -> Self {
        Self {
            argument_id: argument_id.into(),
            syntax: ArgSyntax::Positional { index },
            value_type: ArgValueType::String,
            value_name: nonempty(value_name.into()),
            enum_values: Vec::new(),
            default: None,
            repeatable: false,
            sensitive: false,
            about: None,
        }
    }

    pub fn positional_json(
        argument_id: impl Into<String>,
        index: usize,
        value_name: impl Into<String>,
    ) -> Self {
        Self {
            value_type: ArgValueType::Json,
            ..Self::positional(argument_id, index, value_name)
        }
    }

    pub fn positional_enum<I, S>(
        argument_id: impl Into<String>,
        index: usize,
        value_name: impl Into<String>,
        values: I,
    ) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut spec = Self {
            value_type: ArgValueType::Enum,
            ..Self::positional(argument_id, index, value_name)
        };
        spec.enum_values = values.into_iter().map(Into::into).collect();
        spec
    }

    fn long(long: impl Into<String>, value_type: ArgValueType, value_name: Option<String>) -> Self {
        let long = long.into();
        let argument_id = long
            .strip_prefix("--")
            .unwrap_or(long.as_str())
            .replace('-', "_");
        Self {
            argument_id,
            syntax: ArgSyntax::Long { name: long },
            value_type,
            value_name: value_name.and_then(nonempty),
            enum_values: Vec::new(),
            default: None,
            repeatable: false,
            sensitive: false,
            about: None,
        }
    }

    pub fn value_name(mut self, value_name: impl Into<String>) -> Self {
        self.value_name = nonempty(value_name.into());
        self
    }

    pub fn default(mut self, value: impl Into<String>) -> Self {
        self.default = Some(CliValue::String(value.into()));
        self
    }

    pub fn default_i64(mut self, value: i64) -> Self {
        self.default = Some(CliValue::I64(value));
        self
    }

    pub fn default_f64(mut self, value: f64) -> Self {
        self.default = Some(CliValue::FiniteF64(value));
        self
    }

    pub fn repeatable(mut self) -> Self {
        self.repeatable = true;
        self
    }

    /// Mark this argument's value as one that must never be echoed back.
    pub fn sensitive(mut self) -> Self {
        self.sensitive = true;
        self
    }

    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = nonempty(about.into());
        self
    }
}

/// A finite fixed enum constraint.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FixedValue {
    Value(String),
    OneOf { one_of: Vec<String> },
}

impl FixedValue {
    pub(super) fn values(&self) -> &[String] {
        match self {
            Self::Value(value) => std::slice::from_ref(value),
            Self::OneOf { one_of } => one_of,
        }
    }
}

/// One named legal application invocation shape.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Combination {
    pub combination_id: String,
    pub action_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub about: Option<String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub fixed: BTreeMap<String, FixedValue>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub optional: Vec<String>,
    pub output: OutputSpec,
}

impl Combination {
    pub fn new(combination_id: impl Into<String>) -> Self {
        Self {
            combination_id: combination_id.into(),
            action_id: String::new(),
            about: None,
            fixed: BTreeMap::new(),
            required: Vec::new(),
            optional: Vec::new(),
            output: OutputSpec::protocol_finite(
                ["json", "yaml", "plain"],
                ["split", "stdout", "stderr"],
                "json",
                "split",
            ),
        }
    }

    pub fn action(mut self, action_id: impl Into<String>) -> Self {
        self.action_id = action_id.into();
        self
    }

    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = nonempty(about.into());
        self
    }

    pub fn fixed(mut self, argument_id: impl Into<String>, value: impl Into<String>) -> Self {
        self.fixed
            .insert(argument_id.into(), FixedValue::Value(value.into()));
        self
    }

    pub fn fixed_one_of<I, S>(mut self, argument_id: impl Into<String>, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.fixed.insert(
            argument_id.into(),
            FixedValue::OneOf {
                one_of: values.into_iter().map(Into::into).collect(),
            },
        );
        self
    }

    pub fn required<I, S>(mut self, argument_ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.required
            .extend(argument_ids.into_iter().map(Into::into));
        self
    }

    pub fn optional<I, S>(mut self, argument_ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.optional
            .extend(argument_ids.into_iter().map(Into::into));
        self
    }

    pub fn output(mut self, output: OutputSpec) -> Self {
        self.output = output;
        self
    }
}

/// Protocol output lifecycle.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OutputLifecycle {
    Finite,
    Stream,
}

/// Closed output contract for one combination.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum OutputSpec {
    Raw {
        #[serde(default)]
        file_sinks: Vec<String>,
    },
    Protocol {
        lifecycle: OutputLifecycle,
        formats: Vec<String>,
        destinations: Vec<String>,
        default_format: String,
        default_destination: String,
        #[serde(default)]
        file_sinks: Vec<String>,
    },
}

impl OutputSpec {
    pub fn raw() -> Self {
        Self::Raw {
            file_sinks: Vec::new(),
        }
    }

    pub fn protocol_finite<FI, FS, DI, DS>(
        formats: FI,
        destinations: DI,
        default_format: impl Into<String>,
        default_destination: impl Into<String>,
    ) -> Self
    where
        FI: IntoIterator<Item = FS>,
        FS: Into<String>,
        DI: IntoIterator<Item = DS>,
        DS: Into<String>,
    {
        Self::Protocol {
            lifecycle: OutputLifecycle::Finite,
            formats: formats.into_iter().map(Into::into).collect(),
            destinations: destinations.into_iter().map(Into::into).collect(),
            default_format: default_format.into(),
            default_destination: default_destination.into(),
            file_sinks: Vec::new(),
        }
    }

    pub fn protocol_stream<FI, FS, DI, DS>(
        formats: FI,
        destinations: DI,
        default_format: impl Into<String>,
        default_destination: impl Into<String>,
    ) -> Self
    where
        FI: IntoIterator<Item = FS>,
        FS: Into<String>,
        DI: IntoIterator<Item = DS>,
        DS: Into<String>,
    {
        Self::Protocol {
            lifecycle: OutputLifecycle::Stream,
            formats: formats.into_iter().map(Into::into).collect(),
            destinations: destinations.into_iter().map(Into::into).collect(),
            default_format: default_format.into(),
            default_destination: default_destination.into(),
            file_sinks: Vec::new(),
        }
    }

    pub fn file_sinks<I, S>(mut self, sinks: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let values = sinks.into_iter().map(Into::into).collect();
        match &mut self {
            Self::Raw { file_sinks } | Self::Protocol { file_sinks, .. } => {
                *file_sinks = values;
            }
        }
        self
    }

    pub(super) fn file_sinks_ref(&self) -> &[String] {
        match self {
            Self::Raw { file_sinks } | Self::Protocol { file_sinks, .. } => file_sinks,
        }
    }
}

/// Stable build-time registry error.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CliSpecError {
    pub rule: &'static str,
    pub message: String,
}

impl CliSpecError {
    pub(super) fn new(rule: &'static str, message: impl Into<String>) -> Self {
        Self {
            rule,
            message: message.into(),
        }
    }
}

impl std::fmt::Display for CliSpecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.rule, self.message)
    }
}

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