alpha2-engine 0.10.0

Player and exporter facade for alpha2.
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
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! A plugin's manifest, naming its identity, artifacts, resources and extractor descriptor.

use std::collections::HashSet;
use std::fmt;

use serde::{Deserialize, Serialize};

use super::Descriptor;
#[cfg(feature = "schema")]
use super::descriptor::PLAIN_TEXT_PATTERN;
use super::descriptor::plain_text;

/// The longest manifest text accepted.
pub const MAX_MANIFEST_BYTES: usize = 1024 * 1024;
/// Most artifacts one manifest lists.
pub const MAX_PLUGIN_ARTIFACTS: usize = 64;
/// Most resources one manifest lists.
pub const MAX_PLUGIN_RESOURCES: usize = 64;
/// The longest artifact a manifest may pin, in bytes.
pub const MAX_PLUGIN_ARTIFACT_BYTES: u64 = 256 * 1024 * 1024;
/// The longest resource a manifest may pin, in bytes: what a wasm module's memory can address.
pub const MAX_PLUGIN_RESOURCE_BYTES: u64 = 2 * 1024 * 1024 * 1024;
/// The most bytes a manifest's resources may pin together.
pub const MAX_PLUGIN_RESOURCES_TOTAL_BYTES: u64 = 4 * 1024 * 1024 * 1024;

/// A plugin's manifest.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PluginManifest {
    /// Reverse-DNS id, equal to the descriptor's.
    pub id: String,
    /// The package's semantic version.
    pub version: String,
    /// The plugin ABI, `alpha2-plugin/1`.
    #[cfg_attr(feature = "schema", schemars(extend("const" = alpha2_plugin_abi::ABI)))]
    pub abi: String,
    /// The contract the plugin implements.
    pub kind: PluginKind,
    /// The code, one artifact per target.
    #[cfg_attr(feature = "schema", schemars(length(min = 1)))]
    pub artifacts: Vec<PluginArtifact>,
    /// Files the plugin reads through `resource()`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub resources: Vec<PluginResource>,
    /// What an interface shows about the plugin. The engine ignores it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub about: Option<PluginAbout>,
    /// The descriptor `describe()` returns.
    pub descriptor: Descriptor,
}

/// The longest `about.name`, in characters.
pub const MAX_ABOUT_NAME_CHARS: usize = 80;
/// The longest `about.summary`, in characters.
pub const MAX_ABOUT_SUMMARY_CHARS: usize = 400;
/// The longest `about.author`, in characters.
pub const MAX_ABOUT_AUTHOR_CHARS: usize = 80;
/// The longest `about.homepage`, in characters.
pub const MAX_ABOUT_HOMEPAGE_CHARS: usize = 2048;
/// The longest `about.license`, in characters.
pub const MAX_ABOUT_LICENSE_CHARS: usize = 256;

/// What an interface shows about a plugin: plain text, never markup, without control characters.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PluginAbout {
    /// The plugin's name.
    #[cfg_attr(
        feature = "schema",
        schemars(length(min = 1, max = MAX_ABOUT_NAME_CHARS), regex(pattern = PLAIN_TEXT_PATTERN))
    )]
    pub name: String,
    /// What it does, in a sentence or two.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[cfg_attr(
        feature = "schema",
        schemars(length(max = MAX_ABOUT_SUMMARY_CHARS), regex(pattern = PLAIN_TEXT_PATTERN))
    )]
    pub summary: Option<String>,
    /// Who makes it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[cfg_attr(
        feature = "schema",
        schemars(length(max = MAX_ABOUT_AUTHOR_CHARS), regex(pattern = PLAIN_TEXT_PATTERN))
    )]
    pub author: Option<String>,
    /// Its web page, an `https:` URL of printable ASCII.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[cfg_attr(
        feature = "schema",
        schemars(length(max = MAX_ABOUT_HOMEPAGE_CHARS), regex(pattern = HOMEPAGE_PATTERN))
    )]
    pub homepage: Option<String>,
    /// Its license, an SPDX license expression such as `MIT OR Apache-2.0`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[cfg_attr(
        feature = "schema",
        schemars(length(max = MAX_ABOUT_LICENSE_CHARS), regex(pattern = LICENSE_PATTERN))
    )]
    pub license: Option<String>,
}

#[cfg(feature = "schema")]
const HOMEPAGE_PATTERN: &str = "^https://[!-~]+$";
#[cfg(feature = "schema")]
const LICENSE_PATTERN: &str = r"^[A-Za-z0-9.+:() -]+$";

impl PluginAbout {
    fn validate(&self) -> Result<(), String> {
        let text = |field: &str, value: &str, min: usize, max: usize| {
            if plain_text(value, min, max) {
                Ok(())
            } else {
                Err(format!(
                    "about.{field} is not {min} to {max} characters without control characters"
                ))
            }
        };
        text("name", &self.name, 1, MAX_ABOUT_NAME_CHARS)?;
        if let Some(summary) = &self.summary {
            text("summary", summary, 0, MAX_ABOUT_SUMMARY_CHARS)?;
        }
        if let Some(author) = &self.author {
            text("author", author, 0, MAX_ABOUT_AUTHOR_CHARS)?;
        }
        if let Some(homepage) = &self.homepage {
            let rest = homepage.strip_prefix("https://").unwrap_or_default();
            if homepage.len() > MAX_ABOUT_HOMEPAGE_CHARS
                || rest.is_empty()
                || !rest.bytes().all(|byte| matches!(byte, b'!'..=b'~'))
            {
                return Err(format!(
                    "about.homepage is not an https: URL of printable ASCII, at most \
                     {MAX_ABOUT_HOMEPAGE_CHARS} characters"
                ));
            }
        }
        if let Some(license) = &self.license
            && (license.len() > MAX_ABOUT_LICENSE_CHARS || !spdx_expression(license))
        {
            return Err(format!(
                "about.license {license:?} is not an SPDX license expression of at most \
                 {MAX_ABOUT_LICENSE_CHARS} characters"
            ));
        }
        Ok(())
    }
}

// SPDX 2.3 annex D license expressions; the ids are not looked up.
fn spdx_expression(text: &str) -> bool {
    let spaced = text.replace('(', " ( ").replace(')', " ) ");
    let tokens = spaced.split_ascii_whitespace().collect::<Vec<_>>();
    let mut at = 0;
    spdx_or(&tokens, &mut at, 0) && at == tokens.len()
}

const SPDX_MAX_NESTING: usize = 32;

fn spdx_id(token: &str) -> bool {
    !matches!(token, "AND" | "OR" | "WITH" | "(" | ")")
        && !token.is_empty()
        && token
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b':'))
}

fn spdx_or(tokens: &[&str], at: &mut usize, depth: usize) -> bool {
    spdx_and(tokens, at, depth)
        && loop {
            if tokens.get(*at) != Some(&"OR") {
                break true;
            }
            *at += 1;
            if !spdx_and(tokens, at, depth) {
                break false;
            }
        }
}

fn spdx_and(tokens: &[&str], at: &mut usize, depth: usize) -> bool {
    spdx_term(tokens, at, depth)
        && loop {
            if tokens.get(*at) != Some(&"AND") {
                break true;
            }
            *at += 1;
            if !spdx_term(tokens, at, depth) {
                break false;
            }
        }
}

fn spdx_term(tokens: &[&str], at: &mut usize, depth: usize) -> bool {
    let Some(&token) = tokens.get(*at) else {
        return false;
    };
    *at += 1;
    if token == "(" {
        if depth >= SPDX_MAX_NESTING
            || !spdx_or(tokens, at, depth + 1)
            || tokens.get(*at) != Some(&")")
        {
            return false;
        }
        *at += 1;
        return true;
    }
    if !spdx_id(token.strip_suffix('+').unwrap_or(token)) {
        return false;
    }
    if tokens.get(*at) == Some(&"WITH") {
        *at += 1;
        let exception = tokens.get(*at).is_some_and(|token| spdx_id(token));
        *at += 1;
        return exception;
    }
    true
}

/// The contract a plugin implements.
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum PluginKind {
    /// An extractor.
    Extractor,
}

/// One build of a plugin.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PluginArtifact {
    /// `wasm32`, or the Rust target triple a native artifact is built for.
    pub target: String,
    /// What kind of code it is.
    #[serde(rename = "type")]
    pub kind: ArtifactKind,
    /// Where it is: a path under the manifest's own directory, or an absolute `https:` URL.
    pub url: String,
    /// SHA-256 of its bytes, 64 lowercase hexadecimal digits.
    #[cfg_attr(feature = "schema", schemars(regex(pattern = r"^[0-9a-f]{64}$")))]
    pub sha256: String,
    /// Its length in bytes.
    #[cfg_attr(feature = "schema", schemars(range(max = MAX_PLUGIN_ARTIFACT_BYTES)))]
    pub bytes: u64,
}

/// What kind of code an artifact is.
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Hash, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum ArtifactKind {
    /// A core wasm module.
    Wasm,
    /// A C-ABI dynamic library.
    Cdylib,
    /// A static library an app links at build time.
    Staticlib,
}

/// The most resource slots one manifest declares, inside [`MAX_PLUGIN_RESOURCES`].
pub const MAX_PLUGIN_SLOTS: usize = 16;
/// The most file name suffixes one slot's `accept` lists.
pub const MAX_SLOT_ACCEPT: usize = 16;
/// The longest file name suffix in a slot's `accept`, in bytes.
pub const MAX_SLOT_ACCEPT_BYTES: usize = 64;
/// The longest slot name, in bytes.
pub const MAX_SLOT_NAME_BYTES: usize = 64;

/// A file a plugin reads: pinned by the manifest, or supplied by the caller at load time.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(try_from = "ResourceEntry", into = "ResourceEntry")]
pub struct PluginResource {
    /// The name the plugin asks for.
    pub name: String,
    /// Where its bytes come from.
    pub source: ResourceSource,
}

/// Where a resource's bytes come from.
#[derive(Clone, Debug, PartialEq)]
pub enum ResourceSource {
    /// A file the manifest pins by URL, SHA-256 and length.
    Pinned(PinnedResource),
    /// A file or directory the caller names when it loads the plugin.
    Supplied(SuppliedSlot),
}

/// A file the manifest pins.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PinnedResource {
    /// Where it is: a path under the manifest's own directory, or an absolute `https:` URL.
    pub url: String,
    /// SHA-256 of its bytes, 64 lowercase hexadecimal digits.
    pub sha256: String,
    /// Its length in bytes.
    pub bytes: u64,
}

/// A resource slot the caller fills at load time.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct SuppliedSlot {
    /// What the caller names.
    pub shape: SlotShape,
    /// Whether a load without it fails. An optional slot left empty reads as an undeclared name.
    #[serde(default = "required_default", skip_serializing_if = "is_true")]
    #[cfg_attr(feature = "schema", schemars(extend("default" = true)))]
    pub required: bool,
    /// The largest single file the plugin takes from the slot, in bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "schema", schemars(range(min = 1)))]
    pub max_bytes: Option<u64>,
    /// File name suffixes a picker offers, such as `.onnx`; for interfaces only.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    #[cfg_attr(
        feature = "schema",
        schemars(length(max = MAX_SLOT_ACCEPT), inner(length(min = 1, max = MAX_SLOT_ACCEPT_BYTES)))
    )]
    pub accept: Vec<String>,
}

const fn required_default() -> bool {
    true
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde passes a reference"
)]
const fn is_true(value: &bool) -> bool {
    *value
}

/// What the caller names for a slot.
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum SlotShape {
    /// One file, read as the resource named as the slot.
    File,
    /// A directory; each file under it is the resource `<slot>/<relative path>`.
    Directory,
}

impl PluginResource {
    /// The pin, for a pinned resource.
    #[must_use]
    pub const fn pinned(&self) -> Option<&PinnedResource> {
        match &self.source {
            ResourceSource::Pinned(pinned) => Some(pinned),
            ResourceSource::Supplied(_) => None,
        }
    }

    /// The slot, for a supplied resource.
    #[must_use]
    pub const fn slot(&self) -> Option<&SuppliedSlot> {
        match &self.source {
            ResourceSource::Pinned(_) => None,
            ResourceSource::Supplied(slot) => Some(slot),
        }
    }
}

/// A resource as JSON spells it: the pin's fields, or `supplied`.
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct ResourceEntry {
    name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    sha256: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    supplied: Option<SuppliedSlot>,
}

impl TryFrom<ResourceEntry> for PluginResource {
    type Error = String;

    fn try_from(entry: ResourceEntry) -> Result<Self, String> {
        let ResourceEntry {
            name,
            url,
            sha256,
            bytes,
            supplied,
        } = entry;
        let source = match (url, sha256, bytes, supplied) {
            (Some(url), Some(sha256), Some(bytes), None) => {
                ResourceSource::Pinned(PinnedResource { url, sha256, bytes })
            }
            (None, None, None, Some(slot)) => ResourceSource::Supplied(slot),
            (None, None, None, None) => {
                return Err(format!(
                    "resource {name:?} has neither url, sha256 and bytes nor supplied"
                ));
            }
            (_, _, _, Some(_)) => {
                return Err(format!(
                    "resource {name:?} has supplied and a pin; a resource is one or the other"
                ));
            }
            _ => {
                return Err(format!(
                    "resource {name:?} needs all of url, sha256 and bytes"
                ));
            }
        };
        Ok(Self { name, source })
    }
}

impl From<PluginResource> for ResourceEntry {
    fn from(resource: PluginResource) -> Self {
        let mut entry = Self {
            name: resource.name,
            url: None,
            sha256: None,
            bytes: None,
            supplied: None,
        };
        match resource.source {
            ResourceSource::Pinned(PinnedResource { url, sha256, bytes }) => {
                entry.url = Some(url);
                entry.sha256 = Some(sha256);
                entry.bytes = Some(bytes);
            }
            ResourceSource::Supplied(slot) => entry.supplied = Some(slot),
        }
        entry
    }
}

#[cfg(feature = "schema")]
mod resource_schema {
    use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};

    use super::{MAX_PLUGIN_RESOURCE_BYTES, SuppliedSlot};

    /// A file the manifest pins by URL, SHA-256 and length.
    #[derive(JsonSchema)]
    #[schemars(deny_unknown_fields, rename = "PinnedResource")]
    #[expect(dead_code, reason = "a shape for the schema alone")]
    struct PinnedResourceEntry {
        /// The name the plugin asks for.
        #[schemars(length(min = 1))]
        name: String,
        /// Where it is: a path under the manifest's own directory, or an absolute `https:` URL.
        url: String,
        /// SHA-256 of its bytes, 64 lowercase hexadecimal digits.
        #[schemars(regex(pattern = r"^[0-9a-f]{64}$"))]
        sha256: String,
        /// Its length in bytes.
        #[schemars(range(max = MAX_PLUGIN_RESOURCE_BYTES))]
        bytes: u64,
    }

    /// A slot the caller fills with a file or a directory when it loads the plugin.
    #[derive(JsonSchema)]
    #[schemars(deny_unknown_fields, rename = "SuppliedResource")]
    #[expect(dead_code, reason = "a shape for the schema alone")]
    struct SuppliedResourceEntry {
        /// The slot's name, which the plugin asks for.
        #[schemars(regex(pattern = r"^[A-Za-z0-9_-][A-Za-z0-9._-]{0,63}$"))]
        name: String,
        /// What the caller fills it with.
        supplied: SuppliedSlot,
    }

    impl JsonSchema for super::PluginResource {
        fn schema_name() -> std::borrow::Cow<'static, str> {
            "PluginResource".into()
        }

        fn json_schema(generator: &mut SchemaGenerator) -> Schema {
            json_schema!({
                "description": "A file a plugin reads: pinned by the manifest, or a slot the \
                                caller fills at load time.",
                "oneOf": [
                    generator.subschema_for::<PinnedResourceEntry>(),
                    generator.subschema_for::<SuppliedResourceEntry>(),
                ]
            })
        }
    }
}

/// Whether `name` is a slot name: 1 to 64 of `A-Z a-z 0-9 . _ -`, not starting with `.`.
#[must_use]
pub fn is_slot_name(name: &str) -> bool {
    let allowed = |byte: u8| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-');
    name.len() <= MAX_SLOT_NAME_BYTES
        && name.bytes().next().is_some_and(allowed)
        && name.bytes().all(|byte| allowed(byte) || byte == b'.')
}

/// Why a manifest was refused.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ManifestError {
    /// The manifest names an ABI this build does not implement.
    UnsupportedAbi(String),
    /// The manifest is not one: bad JSON, a missing or malformed field, or a broken rule.
    Invalid(String),
}

impl fmt::Display for ManifestError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsupportedAbi(abi) => write!(
                formatter,
                "the plugin needs ABI {abi}; this build implements {}",
                alpha2_plugin_abi::ABI
            ),
            Self::Invalid(why) => write!(formatter, "invalid plugin manifest: {why}"),
        }
    }
}

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

impl PluginManifest {
    /// Reads and checks a manifest.
    ///
    /// # Errors
    /// [`ManifestError::UnsupportedAbi`] for another ABI, checked before the rest so that a
    /// manifest of a later ABI is reported as such; [`ManifestError::Invalid`] otherwise.
    pub fn from_json(text: &str) -> Result<Self, ManifestError> {
        if text.len() > MAX_MANIFEST_BYTES {
            return Err(ManifestError::Invalid(format!(
                "longer than {MAX_MANIFEST_BYTES} bytes"
            )));
        }
        let value: serde_json::Value = serde_json::from_str(text)
            .map_err(|error| ManifestError::Invalid(error.to_string()))?;
        match value.get("abi").and_then(serde_json::Value::as_str) {
            Some(abi) if abi != alpha2_plugin_abi::ABI => {
                return Err(ManifestError::UnsupportedAbi(abi.to_owned()));
            }
            _ => {}
        }
        let manifest: Self = serde_json::from_value(value)
            .map_err(|error| ManifestError::Invalid(error.to_string()))?;
        manifest.validate().map_err(ManifestError::Invalid)?;
        Ok(manifest)
    }

    fn validate(&self) -> Result<(), String> {
        if self.id != self.descriptor.id {
            return Err(format!(
                "id {} differs from the descriptor's {}",
                self.id, self.descriptor.id
            ));
        }
        if !semver(&self.version) {
            return Err(format!(
                "version {} is not a semantic version",
                self.version
            ));
        }
        super::session::validate_descriptor(&self.descriptor).map_err(|error| error.to_string())?;
        if self.artifacts.is_empty() || self.artifacts.len() > MAX_PLUGIN_ARTIFACTS {
            return Err(format!("needs 1 to {MAX_PLUGIN_ARTIFACTS} artifacts"));
        }
        if self.resources.len() > MAX_PLUGIN_RESOURCES {
            return Err(format!("lists more than {MAX_PLUGIN_RESOURCES} resources"));
        }
        let mut targets = HashSet::new();
        for artifact in &self.artifacts {
            if artifact.target.is_empty() || !targets.insert((&artifact.target, artifact.kind)) {
                return Err(format!(
                    "artifact target {:?} is empty or listed twice for one type",
                    artifact.target
                ));
            }
            if (artifact.target == WASM_TARGET) != (artifact.kind == ArtifactKind::Wasm) {
                return Err(format!(
                    "artifact {}: only the wasm32 target is a wasm module",
                    artifact.url
                ));
            }
            check_file(&artifact.url, &artifact.sha256)?;
            if artifact.bytes > MAX_PLUGIN_ARTIFACT_BYTES {
                return Err(format!(
                    "artifact {} is longer than {MAX_PLUGIN_ARTIFACT_BYTES} bytes",
                    artifact.url
                ));
            }
        }
        self.validate_resources()?;
        self.about.as_ref().map_or(Ok(()), PluginAbout::validate)
    }

    fn validate_resources(&self) -> Result<(), String> {
        let mut names = HashSet::new();
        let mut directories = Vec::new();
        let (mut slots, mut total) = (0_usize, 0_u64);
        for resource in &self.resources {
            if resource.name.is_empty() || !names.insert(&resource.name) {
                return Err(format!(
                    "resource name {:?} is empty or repeated",
                    resource.name
                ));
            }
            match &resource.source {
                ResourceSource::Pinned(pinned) => {
                    check_file(&pinned.url, &pinned.sha256)?;
                    if pinned.bytes > MAX_PLUGIN_RESOURCE_BYTES {
                        return Err(format!(
                            "resource {} is longer than {MAX_PLUGIN_RESOURCE_BYTES} bytes",
                            resource.name
                        ));
                    }
                    total = total.saturating_add(pinned.bytes);
                }
                ResourceSource::Supplied(slot) => {
                    slots += 1;
                    check_slot(&resource.name, slot)?;
                    if slot.shape == SlotShape::Directory {
                        directories.push(format!("{}/", resource.name));
                    }
                }
            }
        }
        if slots > MAX_PLUGIN_SLOTS {
            return Err(format!(
                "declares more than {MAX_PLUGIN_SLOTS} resource slots"
            ));
        }
        if let Some(resource) = self.resources.iter().find(|resource| {
            resource.pinned().is_some()
                && directories
                    .iter()
                    .any(|prefix| resource.name.starts_with(prefix.as_str()))
        }) {
            return Err(format!(
                "pinned resource {} lies under a directory slot's names",
                resource.name
            ));
        }
        if total > MAX_PLUGIN_RESOURCES_TOTAL_BYTES {
            return Err(format!(
                "the pinned resources are longer than {MAX_PLUGIN_RESOURCES_TOTAL_BYTES} bytes \
                 together"
            ));
        }
        Ok(())
    }

    /// The artifact for `target`, by kind.
    #[must_use]
    pub fn artifact(&self, target: &str, kind: ArtifactKind) -> Option<&PluginArtifact> {
        self.artifacts
            .iter()
            .find(|artifact| artifact.target == target && artifact.kind == kind)
    }
}

/// The target of a wasm artifact.
pub const WASM_TARGET: &str = "wasm32";

/// What a manifest's `url` names.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Reference<'a> {
    /// A path under the manifest's own directory, `/`-separated.
    Relative(&'a str),
    /// An absolute `https:` URL.
    Https(&'a str),
}

/// The scheme of `url`, when it starts with one (RFC 3986).
fn scheme(url: &str) -> Option<&str> {
    let (scheme, _) = url.split_once(':')?;
    let valid = scheme.starts_with(|c: char| c.is_ascii_alphabetic())
        && scheme
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'));
    valid.then_some(scheme)
}

/// Reads a manifest's `url` as a path under the manifest's directory or an `https:` URL.
///
/// # Errors
/// For any other scheme, an absolute or drive-prefixed path, a backslash or NUL, and a path whose
/// `..` segments climb above the manifest's directory.
pub fn reference(url: &str) -> Result<Reference<'_>, String> {
    let refused = |why: &str| Err(format!("{url}: {why}"));
    if url.is_empty() {
        return Err("a URL is empty".to_owned());
    }
    match scheme(url) {
        Some("https") => return Ok(Reference::Https(url)),
        Some(scheme) if scheme.len() == 1 => {
            return refused("a drive-prefixed path is not under the manifest");
        }
        Some(_) => return refused("only https: URLs and paths under the manifest are allowed"),
        None => {}
    }
    if url.contains(['\\', '\0']) {
        return refused("a path takes neither backslashes nor NUL");
    }
    if url.starts_with('/') {
        return refused("an absolute path is not under the manifest");
    }
    let path = url.split(['?', '#']).next().unwrap_or_default();
    let mut depth = 0_usize;
    for segment in path.split('/') {
        match segment {
            "" | "." => {}
            ".." => {
                depth = depth
                    .checked_sub(1)
                    .ok_or_else(|| format!("{url}: the path climbs above the manifest"))?;
            }
            _ => depth += 1,
        }
    }
    Ok(Reference::Relative(url))
}

fn check_slot(name: &str, slot: &SuppliedSlot) -> Result<(), String> {
    if !is_slot_name(name) {
        return Err(format!(
            "slot name {name:?} is not 1 to {MAX_SLOT_NAME_BYTES} of A-Z, a-z, 0-9, '.', '_' and \
             '-', not starting with '.'"
        ));
    }
    if slot.max_bytes == Some(0) {
        return Err(format!("slot {name}: max_bytes is 0"));
    }
    if slot.accept.len() > MAX_SLOT_ACCEPT {
        return Err(format!(
            "slot {name}: accept lists more than {MAX_SLOT_ACCEPT} suffixes"
        ));
    }
    if let Some(suffix) = slot
        .accept
        .iter()
        .find(|suffix| suffix.is_empty() || suffix.len() > MAX_SLOT_ACCEPT_BYTES)
    {
        return Err(format!(
            "slot {name}: accept suffix {suffix:?} is empty or longer than \
             {MAX_SLOT_ACCEPT_BYTES} bytes"
        ));
    }
    Ok(())
}

fn check_file(url: &str, sha256: &str) -> Result<(), String> {
    reference(url)?;
    if sha256.len() != 64
        || !sha256
            .bytes()
            .all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f'))
    {
        return Err(format!(
            "{url}: sha256 is not 64 lowercase hexadecimal digits"
        ));
    }
    Ok(())
}

/// `MAJOR.MINOR.PATCH` with an optional `-prerelease` and `+build`, as semver 2.0 spells them.
fn semver(version: &str) -> bool {
    let (rest, build) = version
        .split_once('+')
        .map_or((version, None), |(a, b)| (a, Some(b)));
    let (core, pre) = rest
        .split_once('-')
        .map_or((rest, None), |(a, b)| (a, Some(b)));
    let identifiers = |text: &str| {
        text.split('.').all(|part| {
            !part.is_empty()
                && part
                    .bytes()
                    .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
        })
    };
    let numbers = core.split('.').collect::<Vec<_>>();
    numbers.len() == 3
        && numbers.iter().all(|part| {
            !part.is_empty()
                && part.bytes().all(|byte| byte.is_ascii_digit())
                && (part.len() == 1 || !part.starts_with('0'))
        })
        && pre.is_none_or(identifiers)
        && build.is_none_or(identifiers)
}

/// The JSON Schema of a plugin manifest.
#[cfg(feature = "schema")]
#[must_use]
pub fn plugin_manifest_json_schema() -> serde_json::Value {
    let mut schema = schemars::generate::SchemaSettings::draft2020_12()
        .for_deserialize()
        .into_generator()
        .into_root_schema_for::<PluginManifest>();
    schema.insert(
        "$id".to_owned(),
        serde_json::Value::String(PLUGIN_MANIFEST_SCHEMA_ID.to_owned()),
    );
    schema.insert(
        "title".to_owned(),
        serde_json::Value::String(format!(
            "alpha2 plugin manifest, ABI {}",
            alpha2_plugin_abi::ABI
        )),
    );
    let mut value = schema.to_value();
    alpha2_model::finish_json_schema(&mut value);
    value
}

/// Where the plugin manifest schema is published.
pub const PLUGIN_MANIFEST_SCHEMA_ID: &str = "https://alpha2.sh/schema/plugin-manifest.v1.json";

/// The plugin manifest schema without its prose, as the diagnostics walker embeds it.
#[cfg(feature = "schema")]
#[must_use]
pub fn plugin_manifest_shape_json() -> serde_json::Value {
    let mut value = plugin_manifest_json_schema();
    alpha2_model::strip_json_schema_prose(&mut value);
    value
}