kernal-api 0.1.14

Async OS HAL, profiling, symbolization, and allocator instrumentation
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
//! Bounded declarative command parsing. Applications own their command names,
//! field mapping, and effect policy; Clap is a private implementation detail.

use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fmt::Write as _;

/// Maximum number of command-line words accepted by [`Command::parse`].
pub const MAX_ARGUMENTS: usize = 1_024;
/// Maximum UTF-8 bytes accepted for one command-line word.
pub const MAX_ARGUMENT_BYTES: usize = 64 * 1024;
/// Maximum total UTF-8 bytes accepted for all command-line words.
pub const MAX_TOTAL_ARGUMENT_BYTES: usize = 1024 * 1024;

/// A facade-owned scalar validation rule.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ValueKind {
    /// Any non-NUL UTF-8 string within the input limits.
    String,
    /// An operating-system string, retained without UTF-8 conversion.
    OsString,
    /// One of the declared strings.
    Enumeration(Vec<String>),
    /// A finite IEEE-754 double accepted by Rust's `f64` parser.
    F64,
    /// A non-negative 32-bit integer.
    U32,
}

impl ValueKind {
    /// Accept an arbitrary string value.
    pub const fn string() -> Self {
        Self::String
    }

    /// Accept a non-NUL operating-system string within the input limits.
    pub const fn os_string() -> Self {
        Self::OsString
    }

    /// Accept exactly one of `values`.
    pub fn enumeration<I, S>(values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self::Enumeration(values.into_iter().map(Into::into).collect())
    }

    /// Accept a Rust `f64` value.
    pub const fn f64() -> Self {
        Self::F64
    }

    /// Accept a Rust `u32` value.
    pub const fn u32() -> Self {
        Self::U32
    }
}

/// One long option in a [`Command`] schema.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OptionSpec {
    name: String,
    kind: Option<ValueKind>,
    default: Option<String>,
    default_missing: Option<String>,
    repeated: bool,
    conflicts: Vec<String>,
    requires_any: Vec<String>,
    help: Option<String>,
    hidden: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct PositionalSpec {
    name: String,
    kind: ValueKind,
    optional: bool,
}

impl OptionSpec {
    /// Declare a boolean `--name` flag.
    pub fn flag(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            kind: None,
            default: None,
            default_missing: None,
            repeated: false,
            conflicts: Vec::new(),
            requires_any: Vec::new(),
            help: None,
            hidden: false,
        }
    }

    /// Declare a string-valued `--name VALUE` option.
    pub fn value(name: impl Into<String>, kind: ValueKind) -> Self {
        Self {
            name: name.into(),
            kind: Some(kind),
            default: None,
            default_missing: None,
            repeated: false,
            conflicts: Vec::new(),
            requires_any: Vec::new(),
            help: None,
            hidden: false,
        }
    }

    /// Supply a value used when this option is absent.
    pub fn default(mut self, value: impl Into<String>) -> Self {
        self.default = Some(value.into());
        self
    }

    /// Allow this value option without a value and use `value` in that case.
    pub fn optional_value(mut self, value: impl Into<String>) -> Self {
        self.default_missing = Some(value.into());
        self
    }

    /// Preserve every occurrence of this value option in declaration order.
    pub fn repeated(mut self) -> Self {
        self.repeated = true;
        self
    }

    /// Reject this option when `other` is also present.
    pub fn conflicts(mut self, other: impl Into<String>) -> Self {
        self.conflicts.push(other.into());
        self
    }

    /// Require one of these option names whenever this option is present.
    pub fn requires_any<I, S>(mut self, names: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.requires_any.extend(names.into_iter().map(Into::into));
        self
    }

    /// Describe this option in facade-rendered help.
    pub fn help(mut self, text: impl Into<String>) -> Self {
        self.help = Some(text.into());
        self
    }

    /// Keep this option parseable but omit it from facade-rendered help.
    pub fn hidden(mut self) -> Self {
        self.hidden = true;
        self
    }
}

/// A declarative command and its nested subcommands.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Command {
    name: String,
    about: Option<String>,
    version: Option<String>,
    options: Vec<OptionSpec>,
    positionals: Vec<PositionalSpec>,
    subcommands: Vec<Self>,
    exclusive_groups: Vec<(String, Vec<String>)>,
}

impl Command {
    /// Start a command schema.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            about: None,
            version: None,
            options: Vec::new(),
            positionals: Vec::new(),
            subcommands: Vec::new(),
            exclusive_groups: Vec::new(),
        }
    }

    /// Add a long option.
    pub fn option(mut self, option: OptionSpec) -> Self {
        self.options.push(option);
        self
    }

    /// Describe this command in facade-rendered help.
    pub fn about(mut self, text: impl Into<String>) -> Self {
        self.about = Some(text.into());
        self
    }

    /// Set the version rendered by [`Command::render_version`].
    pub fn version(mut self, value: impl Into<String>) -> Self {
        self.version = Some(value.into());
        self
    }

    /// Render deterministic, backend-independent version text.
    pub fn render_version(&self) -> String {
        match &self.version {
            Some(version) => format!("{} {version}\n", self.name),
            None => format!("{}\n", self.name),
        }
    }

    /// Render deterministic, backend-independent help for this command.
    pub fn render_help(&self) -> String {
        let mut output = format!("Usage: {}", self.name);
        if !self.options.is_empty() {
            output.push_str(" [OPTIONS]");
        }
        if !self.positionals.is_empty() {
            for positional in &self.positionals {
                let token = format!("<{}>", positional.name);
                if positional.optional {
                    let _ = write!(output, " [{token}]");
                } else {
                    let _ = write!(output, " {token}");
                }
            }
        }
        if !self.subcommands.is_empty() {
            output.push_str(" [COMMAND]");
        }
        output.push('\n');
        if let Some(about) = &self.about {
            let _ = write!(output, "\n{about}\n");
        }
        if !self.options.is_empty() {
            output.push_str("\nOptions:\n");
            for option in &self.options {
                if option.hidden {
                    continue;
                }
                let suffix = match &option.kind {
                    None => String::new(),
                    Some(_) if option.default_missing.is_some() => " [VALUE]".to_owned(),
                    Some(_) => " <VALUE>".to_owned(),
                };
                let repeat = if option.repeated { "..." } else { "" };
                let help = option.help.as_deref().unwrap_or("");
                let _ = writeln!(output, "  --{}{suffix}{repeat}\t{help}", option.name);
            }
        }
        if !self.subcommands.is_empty() {
            output.push_str("\nCommands:\n");
            for command in &self.subcommands {
                let about = command.about.as_deref().unwrap_or("");
                let _ = writeln!(output, "  {}\t{about}", command.name);
            }
        }
        output
    }

    /// Add a required positional value in declaration order.
    pub fn positional(mut self, name: impl Into<String>, kind: ValueKind) -> Self {
        self.positionals.push(PositionalSpec {
            name: name.into(),
            kind,
            optional: false,
        });
        self
    }

    /// Add an optional positional value in declaration order.
    pub fn optional_positional(mut self, name: impl Into<String>, kind: ValueKind) -> Self {
        self.positionals.push(PositionalSpec {
            name: name.into(),
            kind,
            optional: true,
        });
        self
    }

    /// Add a nested subcommand.
    pub fn subcommand(mut self, command: Self) -> Self {
        self.subcommands.push(command);
        self
    }

    /// Require at most one named option from this group.
    pub fn exclusive_group<I, S>(mut self, name: impl Into<String>, options: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.exclusive_groups
            .push((name.into(), options.into_iter().map(Into::into).collect()));
        self
    }

    /// Parse one bounded command line into facade-owned values.
    ///
    /// The private parser never runs a shell. Input limits are checked before
    /// it receives any owned words; errors deliberately do not echo input.
    pub fn parse<I, S>(&self, arguments: I) -> Result<ParsedCommand, CommandError>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut total = 0usize;
        let mut words = Vec::new();
        for argument in arguments {
            if words.len() == MAX_ARGUMENTS {
                return Err(CommandError::TooManyArguments);
            }
            let value = argument.as_ref();
            let bytes = value.as_encoded_bytes();
            if bytes.contains(&b'\0') {
                return Err(CommandError::ContainsNul);
            }
            if bytes.len() > MAX_ARGUMENT_BYTES {
                return Err(CommandError::ArgumentTooLarge);
            }
            total = total
                .checked_add(bytes.len())
                .ok_or(CommandError::InputTooLarge)?;
            if total > MAX_TOTAL_ARGUMENT_BYTES {
                return Err(CommandError::InputTooLarge);
            }
            words.push(value.to_os_string());
        }
        let explicit_options = explicit_option_names(&words)?;
        self.validate()?;
        let command = self.clap_command();
        let matches = command
            .try_get_matches_from(words)
            .map_err(|_| CommandError::InvalidArguments)?;
        let mut path = vec![self.name.clone()];
        let mut schema = self;
        let mut selected = vec![(self, &matches)];
        let mut final_matches = &matches;
        while let Some((name, next)) = final_matches.subcommand() {
            let Some(next_schema) = schema.subcommands.iter().find(|child| child.name == name)
            else {
                return Err(CommandError::InvalidArguments);
            };
            path.push(name.to_owned());
            schema = next_schema;
            final_matches = next;
            selected.push((schema, final_matches));
        }
        let mut values = BTreeMap::new();
        for (command, command_matches) in &selected {
            command.collect_values(command_matches, &mut values)?;
        }
        let selected_schemas = selected
            .iter()
            .map(|(command, _)| *command)
            .collect::<Vec<_>>();
        Self::validate_selected_relations(&selected_schemas, &explicit_options)?;
        Ok(ParsedCommand { path, values })
    }

    fn clap_command(&self) -> clap::Command {
        let mut command = clap::Command::new(self.name.clone())
            .disable_help_flag(false)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .subcommand_precedence_over_arg(true);
        for option in &self.options {
            let mut argument = clap::Arg::new(option.name.clone())
                .long(option.name.clone())
                .global(true);
            match &option.kind {
                None => argument = argument.action(clap::ArgAction::SetTrue),
                Some(ValueKind::String) => argument = argument.action(clap::ArgAction::Set),
                Some(ValueKind::OsString) => {
                    argument = argument
                        .action(clap::ArgAction::Set)
                        .value_parser(clap::value_parser!(std::ffi::OsString));
                }
                Some(ValueKind::Enumeration(values)) => {
                    argument = argument
                        .action(clap::ArgAction::Set)
                        .value_parser(values.clone());
                }
                Some(ValueKind::F64) => {
                    argument = argument
                        .action(clap::ArgAction::Set)
                        .value_parser(clap::value_parser!(f64));
                }
                Some(ValueKind::U32) => {
                    argument = argument
                        .action(clap::ArgAction::Set)
                        .value_parser(clap::value_parser!(u32));
                }
            }
            if let Some(default) = &option.default {
                argument = argument.default_value(default);
            }
            if let Some(default_missing) = &option.default_missing {
                argument = argument
                    .num_args(0..=1)
                    .default_missing_value(default_missing);
            }
            if option.repeated {
                argument = argument.action(clap::ArgAction::Append);
            }
            command = command.arg(argument);
        }
        for (index, positional) in self.positionals.iter().enumerate() {
            let mut argument = clap::Arg::new(positional.name.clone())
                .index(index + 1)
                .required(!positional.optional)
                .action(clap::ArgAction::Set);
            match &positional.kind {
                ValueKind::String => {}
                ValueKind::OsString => {
                    argument = argument.value_parser(clap::value_parser!(std::ffi::OsString));
                }
                ValueKind::Enumeration(values) => argument = argument.value_parser(values.clone()),
                ValueKind::F64 => argument = argument.value_parser(clap::value_parser!(f64)),
                ValueKind::U32 => argument = argument.value_parser(clap::value_parser!(u32)),
            }
            command = command.arg(argument);
        }
        for child in &self.subcommands {
            command = command.subcommand(child.clap_command());
        }
        command
    }

    fn validate(&self) -> Result<(), CommandError> {
        let mut option_names = std::collections::BTreeSet::new();
        self.validate_into(&mut option_names)?;
        self.validate_references(&option_names)
    }

    fn validate_into(
        &self,
        option_names: &mut std::collections::BTreeSet<String>,
    ) -> Result<(), CommandError> {
        if !valid_name(&self.name) || self.name == "help" {
            return Err(CommandError::InvalidSchema);
        }
        let mut child_names = std::collections::BTreeSet::new();
        for option in &self.options {
            if !valid_name(&option.name)
                || option.name == "help"
                || !option_names.insert(option.name.clone())
            {
                return Err(CommandError::InvalidSchema);
            }
            match (&option.kind, &option.default, &option.default_missing) {
                (None, Some(_), _) | (None, _, Some(_)) => return Err(CommandError::InvalidSchema),
                (None, _, _) if option.repeated => return Err(CommandError::InvalidSchema),
                (Some(_), Some(_), Some(_)) | (Some(_), Some(_), _) if option.repeated => {
                    return Err(CommandError::InvalidSchema)
                }
                (Some(ValueKind::Enumeration(values)), default, default_missing)
                    if values.is_empty()
                        || values.iter().any(|value| value.contains('\0'))
                        || default
                            .as_ref()
                            .is_some_and(|value| !values.contains(value))
                        || default_missing
                            .as_ref()
                            .is_some_and(|value| !values.contains(value)) =>
                {
                    return Err(CommandError::InvalidSchema);
                }
                (_, Some(value), _) if value.contains('\0') => {
                    return Err(CommandError::InvalidSchema)
                }
                (_, _, Some(value)) if value.contains('\0') => {
                    return Err(CommandError::InvalidSchema)
                }
                _ => {}
            }
            if let Some(kind) = &option.kind {
                if (option.repeated && !matches!(kind, ValueKind::String | ValueKind::OsString))
                    || option
                        .default
                        .as_ref()
                        .is_some_and(|value| !valid_value(kind, value))
                    || option
                        .default_missing
                        .as_ref()
                        .is_some_and(|value| !valid_value(kind, value))
                {
                    return Err(CommandError::InvalidSchema);
                }
            }
        }
        let mut optional_positional_seen = false;
        for positional in &self.positionals {
            if !valid_name(&positional.name)
                || positional.name == "help"
                || !option_names.insert(positional.name.clone())
                || (optional_positional_seen && !positional.optional)
                || matches!(&positional.kind, ValueKind::Enumeration(values) if values.is_empty() || values.iter().any(|value| value.contains('\0')))
            {
                return Err(CommandError::InvalidSchema);
            }
            optional_positional_seen |= positional.optional;
        }
        for child in &self.subcommands {
            if child.name == "help" || !child_names.insert(child.name.clone()) {
                return Err(CommandError::InvalidSchema);
            }
            child.validate_into(option_names)?;
        }
        Ok(())
    }

    fn validate_references(
        &self,
        option_names: &std::collections::BTreeSet<String>,
    ) -> Result<(), CommandError> {
        for option in &self.options {
            if option
                .conflicts
                .iter()
                .chain(&option.requires_any)
                .any(|name| !option_names.contains(name))
            {
                return Err(CommandError::InvalidSchema);
            }
        }
        let mut group_names = std::collections::BTreeSet::new();
        for (name, options) in &self.exclusive_groups {
            if !valid_name(name)
                || !group_names.insert(name)
                || options.len() < 2
                || options.iter().any(|option| !option_names.contains(option))
            {
                return Err(CommandError::InvalidSchema);
            }
        }
        for child in &self.subcommands {
            child.validate_references(option_names)?;
        }
        Ok(())
    }

    fn collect_values(
        &self,
        matches: &clap::ArgMatches,
        values: &mut BTreeMap<String, ParsedValue>,
    ) -> Result<(), CommandError> {
        for option in &self.options {
            let value = match option.kind {
                None => ParsedValue::Flag(
                    matches
                        .try_get_one::<bool>(&option.name)
                        .map_err(|_| CommandError::InvalidArguments)?
                        .copied()
                        .unwrap_or(false),
                ),
                Some(ValueKind::String) if option.repeated => matches
                    .try_get_many::<String>(&option.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .map(|items| ParsedValue::Strings(items.cloned().collect()))
                    .unwrap_or(ParsedValue::Absent),
                Some(ValueKind::String) | Some(ValueKind::Enumeration(_)) => matches
                    .try_get_one::<String>(&option.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .cloned()
                    .map(ParsedValue::String)
                    .unwrap_or(ParsedValue::Absent),
                Some(ValueKind::OsString) if option.repeated => matches
                    .try_get_many::<std::ffi::OsString>(&option.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .map(|items| ParsedValue::OsStrings(items.cloned().collect()))
                    .unwrap_or(ParsedValue::Absent),
                Some(ValueKind::OsString) => matches
                    .try_get_one::<std::ffi::OsString>(&option.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .cloned()
                    .map(ParsedValue::OsString)
                    .unwrap_or(ParsedValue::Absent),
                Some(ValueKind::F64) => match matches
                    .try_get_one::<f64>(&option.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .copied()
                {
                    Some(value) if value.is_finite() => ParsedValue::F64(value),
                    Some(_) => return Err(CommandError::InvalidArguments),
                    None => ParsedValue::Absent,
                },
                Some(ValueKind::U32) => matches
                    .try_get_one::<u32>(&option.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .copied()
                    .map(ParsedValue::U32)
                    .unwrap_or(ParsedValue::Absent),
            };
            values.insert(option.name.clone(), value);
        }
        for positional in &self.positionals {
            let value = match &positional.kind {
                ValueKind::String | ValueKind::Enumeration(_) => matches
                    .try_get_one::<String>(&positional.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .cloned()
                    .map(ParsedValue::String)
                    .unwrap_or(ParsedValue::Absent),
                ValueKind::OsString => matches
                    .try_get_one::<std::ffi::OsString>(&positional.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .cloned()
                    .map(ParsedValue::OsString)
                    .unwrap_or(ParsedValue::Absent),
                ValueKind::F64 => match matches
                    .try_get_one::<f64>(&positional.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .copied()
                {
                    Some(value) if value.is_finite() => ParsedValue::F64(value),
                    Some(_) => return Err(CommandError::InvalidArguments),
                    None => ParsedValue::Absent,
                },
                ValueKind::U32 => matches
                    .try_get_one::<u32>(&positional.name)
                    .map_err(|_| CommandError::InvalidArguments)?
                    .copied()
                    .map(ParsedValue::U32)
                    .unwrap_or(ParsedValue::Absent),
            };
            values.insert(positional.name.clone(), value);
        }
        Ok(())
    }

    fn validate_selected_relations(
        selected: &[&Self],
        explicit_options: &std::collections::BTreeSet<String>,
    ) -> Result<(), CommandError> {
        for command in selected {
            for option in &command.options {
                if explicit_options.contains(&option.name)
                    && option
                        .conflicts
                        .iter()
                        .any(|name| explicit_options.contains(name))
                {
                    return Err(CommandError::InvalidArguments);
                }
                if explicit_options.contains(&option.name)
                    && !option.requires_any.is_empty()
                    && !option
                        .requires_any
                        .iter()
                        .any(|name| explicit_options.contains(name))
                {
                    return Err(CommandError::InvalidArguments);
                }
            }
            for (_, options) in &command.exclusive_groups {
                if options
                    .iter()
                    .filter(|name| explicit_options.contains(*name))
                    .take(2)
                    .count()
                    > 1
                {
                    return Err(CommandError::InvalidArguments);
                }
            }
        }
        Ok(())
    }
}

fn explicit_option_names(
    words: &[std::ffi::OsString],
) -> Result<std::collections::BTreeSet<String>, CommandError> {
    let mut names = std::collections::BTreeSet::new();
    let mut options_enabled = true;
    for word in words.iter().skip(1) {
        let bytes = word.as_encoded_bytes();
        if options_enabled && bytes == b"--" {
            options_enabled = false;
        } else if options_enabled {
            if let Some(name) = bytes.strip_prefix(b"--") {
                let name = name.split(|byte| *byte == b'=').next().unwrap_or_default();
                if let Ok(name) = std::str::from_utf8(name) {
                    names.insert(name.to_owned());
                }
            }
        }
    }
    Ok(names)
}

fn valid_name(name: &str) -> bool {
    !name.is_empty()
        && !name.starts_with('-')
        && name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
}

fn valid_value(kind: &ValueKind, value: &str) -> bool {
    match kind {
        ValueKind::String => !value.contains('\0'),
        ValueKind::OsString => !value.contains('\0'),
        ValueKind::Enumeration(values) => values.iter().any(|candidate| candidate == value),
        ValueKind::F64 => value.parse::<f64>().is_ok_and(f64::is_finite),
        ValueKind::U32 => value.parse::<u32>().is_ok(),
    }
}

/// Parsed scalar values independent of the private parser backend.
#[derive(Clone, Debug, PartialEq)]
pub enum ParsedValue {
    /// A declared flag.
    Flag(bool),
    /// A declared value option.
    String(String),
    /// A declared lossless operating-system-string option or positional.
    OsString(std::ffi::OsString),
    /// A repeated value option, in command-line order.
    Strings(Vec<String>),
    /// A repeated declared lossless operating-system-string option.
    OsStrings(Vec<std::ffi::OsString>),
    /// A declared `f64` option or positional.
    F64(f64),
    /// A declared `u32` option or positional.
    U32(u32),
    /// A declared value option was absent and has no default.
    Absent,
}

/// A facade-owned parsed command line.
#[derive(Clone, Debug, PartialEq)]
pub struct ParsedCommand {
    path: Vec<String>,
    values: BTreeMap<String, ParsedValue>,
}

impl ParsedCommand {
    /// Selected command names including the root.
    pub fn command_path(&self) -> &[String] {
        &self.path
    }

    /// Read a declared boolean flag.
    pub fn flag(&self, name: &str) -> Option<bool> {
        match self.values.get(name) {
            Some(ParsedValue::Flag(value)) => Some(*value),
            _ => None,
        }
    }

    /// Read a declared string option or its default.
    pub fn value(&self, name: &str) -> Option<&str> {
        match self.values.get(name) {
            Some(ParsedValue::String(value)) => Some(value),
            _ => None,
        }
    }

    /// Read all values of a repeated option.
    pub fn values(&self, name: &str) -> Option<&[String]> {
        match self.values.get(name) {
            Some(ParsedValue::Strings(values)) => Some(values),
            _ => None,
        }
    }

    /// Read a declared operating-system-string option or positional.
    pub fn os_value(&self, name: &str) -> Option<&OsStr> {
        match self.values.get(name) {
            Some(ParsedValue::OsString(value)) => Some(value.as_os_str()),
            _ => None,
        }
    }

    /// Read all values of a repeated declared operating-system-string option.
    pub fn os_values(&self, name: &str) -> Option<&[std::ffi::OsString]> {
        match self.values.get(name) {
            Some(ParsedValue::OsStrings(values)) => Some(values),
            _ => None,
        }
    }

    /// Read a declared `f64` option or positional.
    pub fn f64(&self, name: &str) -> Option<f64> {
        match self.values.get(name) {
            Some(ParsedValue::F64(value)) => Some(*value),
            _ => None,
        }
    }

    /// Read a declared `u32` option or positional.
    pub fn u32(&self, name: &str) -> Option<u32> {
        match self.values.get(name) {
            Some(ParsedValue::U32(value)) => Some(*value),
            _ => None,
        }
    }
}

/// Parsing or schema failures without backend diagnostics or input echo.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub enum CommandError {
    #[error("command schema is invalid")]
    InvalidSchema,
    #[error("command line contains a NUL character")]
    ContainsNul,
    #[error("command line contains a non-UTF-8 argument")]
    InvalidUtf8,
    #[error("command line has too many arguments")]
    TooManyArguments,
    #[error("command-line argument exceeds byte limit")]
    ArgumentTooLarge,
    #[error("command line exceeds byte limit")]
    InputTooLarge,
    #[error("invalid command-line arguments")]
    InvalidArguments,
}