rusting_engine 1.4.0

Vulkan 3D game engine with GPU-accelerated physics for massive physics-heavy scenes
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
//! Reflection: descriptions of component, resource, and asset types that
//! scene files, the Inspector, `rusting schema`, and field paths share.
//!
//! [`reflect!`](crate::reflect!) writes the [`Reflect`] impl of a type from a
//! list of its fields with editor hints. It also emits a check that
//! destructures the type with exactly those fields, and matches an enum on
//! exactly those variants, so adding, renaming, or removing a field without
//! updating the description does not compile:
//!
//! ```
//! use rusting_engine::reflect::{Reflect, TypeInfo};
//!
//! #[derive(Default, serde::Serialize, serde::Deserialize)]
//! struct Health {
//!     current: f32,
//!     max: f32,
//!     #[serde(skip)]
//!     flashing: bool,
//! }
//!
//! rusting_engine::reflect! {
//!     struct Health {
//!         current: f32 { min: 0.0, doc: "hit points left" },
//!         max: f32 { min: 1.0 },
//!         #[skip] flashing: bool,
//!     }
//! }
//!
//! let TypeInfo::Struct(info) = Health::type_info() else { unreachable!() };
//! assert_eq!(info.fields.len(), 2);
//! ```
//!
//! Serialization stays serde. Reflection adds what serde does not know:
//! field lists and hints, rejection of saved fields the type no longer has,
//! versioned [`FieldMigration`]s, and the scene form of references (an
//! [`Entity`] saves as its target's `SceneId`, a [`Handle`] as
//! `{"$asset": path}`).
//!
//! Field and variant names must match serde's: `#[serde(rename)]` and
//! `rename_all` are not supported. Mark `#[serde(skip)]` fields `#[skip]`.
//! Hints are `unit: "m/s"`, `min: 0.0`, `max: 1.0` (floats), `doc: "..."`,
//! and `color: true` for `[f32; 3]` or `[f32; 4]` colors.

mod builtin;

use std::collections::{BTreeMap, HashMap};
use std::fmt::{Debug, Display, Formatter};
use std::path::{Path, PathBuf};

use bevy_ecs::entity::Entity;
use bevy_ecs::prelude::{Resource, World};
use serde_json::{json, Value};
use uuid::Uuid;

use crate::assets::{AssetError, AssetServer, Handle, MeshAsset, TextureAsset};
use crate::runtime::SceneId;

/// A type with a reflected description. Implement it with
/// [`reflect!`](crate::reflect!).
pub trait Reflect: 'static {
    fn type_info() -> TypeInfo;
}

/// The shape of a reflected type, in the form serde writes it to JSON.
#[derive(Clone, Debug, PartialEq)]
pub enum TypeInfo {
    Bool,
    Int {
        name: &'static str,
        min: i128,
        max: i128,
    },
    Float,
    String,
    /// A file path, saved as a string.
    Path,
    /// Fixed-length array.
    Array(Box<TypeInfo>, usize),
    List(Box<TypeInfo>),
    /// String-keyed map, saved as a JSON object.
    Map(Box<TypeInfo>),
    /// `null` or the inner value.
    Option(Box<TypeInfo>),
    Struct(StructInfo),
    Enum(EnumInfo),
    /// Saved as the target's `SceneId`.
    Entity,
    /// Saved as `{"$asset": path}`.
    Handle(AssetKind),
}

#[derive(Clone, Debug, PartialEq)]
pub struct StructInfo {
    pub name: &'static str,
    pub fields: Vec<FieldInfo>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct FieldInfo {
    pub name: &'static str,
    pub ty: TypeInfo,
    pub hints: Hints,
}

/// Editor and documentation hints of one field.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Hints {
    pub unit: &'static str,
    pub min: Option<f64>,
    pub max: Option<f64>,
    pub doc: &'static str,
    /// Edit as a color: `[f32; 3]` RGB or `[f32; 4]` RGBA.
    pub color: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub struct EnumInfo {
    pub name: &'static str,
    pub variants: Vec<VariantInfo>,
}

impl EnumInfo {
    #[must_use]
    pub fn variant(&self, name: &str) -> Option<&VariantInfo> {
        self.variants.iter().find(|variant| variant.name == name)
    }

    /// True when every variant is a unit variant, saved as its name.
    #[must_use]
    pub fn is_unit_only(&self) -> bool {
        self.variants
            .iter()
            .all(|variant| variant.fields == VariantFields::Unit)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct VariantInfo {
    pub name: &'static str,
    pub fields: VariantFields,
}

/// Unit variants save as `"Name"`, the others as `{"Name": payload}`.
#[derive(Clone, Debug, PartialEq)]
pub enum VariantFields {
    Unit,
    /// One item saves as the item itself, more as an array.
    Tuple(Vec<TypeInfo>),
    Struct(Vec<FieldInfo>),
}

/// An asset type that handles can point at, with how to find a handle's
/// path and load a path.
#[derive(Clone, Copy)]
pub struct AssetKind {
    pub name: &'static str,
    path: fn(&AssetServer, u64) -> Option<PathBuf>,
    load: fn(&mut AssetServer, &Path) -> Result<u64, AssetError>,
    paths: fn(&AssetServer) -> Vec<PathBuf>,
}

impl AssetKind {
    /// Loads the asset at `path`, or finds it when it is already loaded.
    pub fn load(
        &self,
        assets: &mut AssetServer,
        path: &Path,
    ) -> Result<u64, AssetError> {
        (self.load)(assets, path)
    }

    /// Paths of every loaded asset of this kind, sorted.
    #[must_use]
    pub fn loaded_paths(&self, assets: &AssetServer) -> Vec<PathBuf> {
        let mut paths = (self.paths)(assets);
        paths.sort();
        paths
    }
}

impl Debug for AssetKind {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.name)
    }
}

impl PartialEq for AssetKind {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

/// Asset types a reflected [`Handle`] can point at.
pub trait ReflectAsset: Sized + 'static {
    const KIND: AssetKind;
}

fn texture_path(assets: &AssetServer, key: u64) -> Option<PathBuf> {
    assets
        .textures
        .path(Handle::from_key(key))
        .map(Path::to_path_buf)
}

fn load_texture(
    assets: &mut AssetServer,
    path: &Path,
) -> Result<u64, AssetError> {
    assets.load_texture(path).map(Handle::key)
}

fn mesh_path(assets: &AssetServer, key: u64) -> Option<PathBuf> {
    assets
        .meshes
        .path(Handle::from_key(key))
        .map(Path::to_path_buf)
}

fn load_mesh(assets: &mut AssetServer, path: &Path) -> Result<u64, AssetError> {
    assets.load_mesh(path).map(Handle::key)
}

fn texture_paths(assets: &AssetServer) -> Vec<PathBuf> {
    assets
        .textures
        .paths()
        .map(|(_, path)| path.to_path_buf())
        .collect()
}

fn mesh_paths(assets: &AssetServer) -> Vec<PathBuf> {
    assets
        .meshes
        .paths()
        .map(|(_, path)| path.to_path_buf())
        .collect()
}

impl ReflectAsset for TextureAsset {
    const KIND: AssetKind = AssetKind {
        name: "texture",
        path: texture_path,
        load: load_texture,
        paths: texture_paths,
    };
}

impl ReflectAsset for MeshAsset {
    const KIND: AssetKind = AssetKind {
        name: "mesh",
        path: mesh_path,
        load: load_mesh,
        paths: mesh_paths,
    };
}

impl<T: ReflectAsset> Reflect for Handle<T> {
    fn type_info() -> TypeInfo {
        TypeInfo::Handle(T::KIND)
    }
}

impl Reflect for Entity {
    fn type_info() -> TypeInfo {
        TypeInfo::Entity
    }
}

macro_rules! reflect_leaf {
    ($($ty:ty => $info:expr),* $(,)?) => {
        $(impl Reflect for $ty {
            fn type_info() -> TypeInfo {
                $info
            }
        })*
    };
}

macro_rules! reflect_int {
    ($($ty:ty),*) => {
        $(impl Reflect for $ty {
            fn type_info() -> TypeInfo {
                TypeInfo::Int {
                    name: stringify!($ty),
                    min: <$ty>::MIN as i128,
                    max: <$ty>::MAX as i128,
                }
            }
        })*
    };
}

reflect_leaf! {
    bool => TypeInfo::Bool,
    f32 => TypeInfo::Float,
    f64 => TypeInfo::Float,
    String => TypeInfo::String,
    PathBuf => TypeInfo::Path,
}
reflect_int!(i8, i16, i32, i64, u8, u16, u32, u64, isize, usize);

impl<T: Reflect, const N: usize> Reflect for [T; N] {
    fn type_info() -> TypeInfo {
        TypeInfo::Array(Box::new(T::type_info()), N)
    }
}

impl<T: Reflect> Reflect for Vec<T> {
    fn type_info() -> TypeInfo {
        TypeInfo::List(Box::new(T::type_info()))
    }
}

impl<T: Reflect> Reflect for Option<T> {
    fn type_info() -> TypeInfo {
        TypeInfo::Option(Box::new(T::type_info()))
    }
}

impl<T: Reflect> Reflect for BTreeMap<String, T> {
    fn type_info() -> TypeInfo {
        TypeInfo::Map(Box::new(T::type_info()))
    }
}

impl<T: Reflect, S: 'static> Reflect for HashMap<String, T, S> {
    fn type_info() -> TypeInfo {
        TypeInfo::Map(Box::new(T::type_info()))
    }
}

/// Converts a hint value written in [`reflect!`](crate::reflect!) into its
/// [`Hints`] field.
pub trait IntoHint<T> {
    fn into_hint(self) -> T;
}

impl IntoHint<&'static str> for &'static str {
    fn into_hint(self) -> &'static str {
        self
    }
}

impl IntoHint<Option<f64>> for f64 {
    fn into_hint(self) -> Option<f64> {
        Some(self)
    }
}

impl IntoHint<bool> for bool {
    fn into_hint(self) -> bool {
        self
    }
}

/// Writes [`Reflect`] for a struct, a one-field tuple struct, or an enum.
/// See the [module docs](crate::reflect).
#[macro_export]
macro_rules! reflect {
    (struct $name:ident { $($body:tt)* }) => {
        impl $crate::reflect::Reflect for $name {
            fn type_info() -> $crate::reflect::TypeInfo {
                $crate::reflect::TypeInfo::Struct($crate::reflect::StructInfo {
                    name: ::std::stringify!($name),
                    fields: $crate::reflect!(@fields $($body)*),
                })
            }
        }
        const _: () = {
            #[allow(dead_code)]
            fn check(value: &$name) {
                $crate::reflect!(@check_fields [$name] value; $($body)*);
            }
        };
    };
    (struct $name:ident ( $inner:ty )) => {
        impl $crate::reflect::Reflect for $name {
            fn type_info() -> $crate::reflect::TypeInfo {
                <$inner as $crate::reflect::Reflect>::type_info()
            }
        }
        const _: () = {
            #[allow(dead_code)]
            fn check() {
                let _: fn($inner) -> $name = $name;
            }
        };
    };
    (enum $name:ident {
        $( $variant:ident $( ( $($tuple:ty),* $(,)? ) )? $( { $($body:tt)* } )? ),* $(,)?
    }) => {
        impl $crate::reflect::Reflect for $name {
            fn type_info() -> $crate::reflect::TypeInfo {
                $crate::reflect::TypeInfo::Enum($crate::reflect::EnumInfo {
                    name: ::std::stringify!($name),
                    variants: ::std::vec![$(
                        $crate::reflect::VariantInfo {
                            name: ::std::stringify!($variant),
                            fields: $crate::reflect!(
                                @variant [$( $($tuple),* )?] [$( $($body)* )?]
                            ),
                        }
                    ),*],
                })
            }
        }
        const _: () = {
            #[allow(dead_code)]
            fn check(value: &$name) {
                match value {
                    $( $name::$variant { .. } => {} )*
                }
                $( $crate::reflect!(
                    @check_variant $name, $variant, value,
                    [$( $($tuple),* )?] [$( $($body)* )?]
                ); )*
            }
        };
    };

    (@fields $(
        $(#[$attr:ident])? $field:ident : $ty:ty
        $({ $($hint:ident : $value:expr),* $(,)? })?
    ),* $(,)?) => {{
        #[allow(unused_mut)]
        let mut fields = ::std::vec::Vec::new();
        $( $crate::reflect!(
            @field fields, [$($attr)?], $field, $ty, { $($($hint : $value),*)? }
        ); )*
        fields
    }};
    (@field $fields:ident, [skip], $field:ident, $ty:ty,
        { $($hint:ident : $value:expr),* }) => {};
    (@field $fields:ident, [], $field:ident, $ty:ty,
        { $($hint:ident : $value:expr),* }) => {
        $fields.push($crate::reflect::FieldInfo {
            name: ::std::stringify!($field),
            ty: <$ty as $crate::reflect::Reflect>::type_info(),
            hints: {
                #[allow(unused_mut)]
                let mut hints = $crate::reflect::Hints::default();
                $( hints.$hint = $crate::reflect::IntoHint::into_hint($value); )*
                hints
            },
        });
    };

    (@variant [] []) => { $crate::reflect::VariantFields::Unit };
    (@variant [$($tuple:ty),+] []) => {
        $crate::reflect::VariantFields::Tuple(::std::vec![
            $( <$tuple as $crate::reflect::Reflect>::type_info() ),+
        ])
    };
    (@variant [] [$($body:tt)+]) => {
        $crate::reflect::VariantFields::Struct($crate::reflect!(@fields $($body)+))
    };

    (@check_variant $name:ident, $variant:ident, $value:ident, [] []) => {};
    (@check_variant $name:ident, $variant:ident, $value:ident,
        [$($tuple:ty),+] []) => {
        let _: fn($($tuple),+) -> $name = $name::$variant;
    };
    (@check_variant $name:ident, $variant:ident, $value:ident,
        [] [$($body:tt)+]) => {
        $crate::reflect!(@check_fields [$name::$variant] $value; $($body)+);
    };

    (@check_fields [$($path:tt)+] $value:ident; $(
        $(#[$attr:ident])? $field:ident : $ty:ty
        $({ $($hint:ident : $hint_value:expr),* $(,)? })?
    ),* $(,)?) => {
        #[allow(irrefutable_let_patterns)]
        if let $($path)+ { $($field),* } = $value {
            $( let _: &$ty = $field; )*
        }
    };
}

/// Key of a saved component's version when it has migrations.
pub const VERSION_KEY: &str = "$version";
/// Key of a saved asset reference: `{"$asset": path}`.
pub const ASSET_KEY: &str = "$asset";

/// One step in the history of a reflected component. Each registered step
/// raises the component's version by one; values saved at an older version
/// run the steps after it when they load.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FieldMigration {
    /// A top-level field was renamed.
    Rename { from: String, to: String },
    /// A top-level field was removed; saved values of it are dropped.
    Remove(String),
}

impl FieldMigration {
    #[must_use]
    pub fn rename(from: impl Into<String>, to: impl Into<String>) -> Self {
        Self::Rename {
            from: from.into(),
            to: to.into(),
        }
    }

    #[must_use]
    pub fn remove(field: impl Into<String>) -> Self {
        Self::Remove(field.into())
    }
}

/// What is wrong at a [`ReflectError`]'s path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReflectProblem {
    /// A saved field the type does not have.
    UnknownField,
    /// A saved enum variant the type does not have.
    UnknownVariant(String),
    /// Saved by a build with more migrations than this one.
    NewerVersion,
    /// A handle to an asset that was not loaded from a file.
    UnsavedAsset,
    /// The asset a saved reference names failed to load.
    Asset(String),
    /// A value of the wrong kind; names the expected one.
    WrongKind(&'static str),
    /// The path names nothing in the type.
    NoSuchPath,
    /// A migration on a component that is not a struct.
    NotAStruct,
}

/// A reflected component value that cannot be read or written, with the
/// saved and current version so a tool can tell which migration is missing.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReflectError {
    pub component: String,
    /// Version the value was saved at; the current one for runtime edits.
    pub saved_version: u32,
    pub current_version: u32,
    /// JSON pointer into the component's scene form.
    pub path: String,
    pub problem: ReflectProblem,
}

impl Display for ReflectError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        let path = if self.path.is_empty() {
            "/"
        } else {
            &self.path
        };
        write!(
            formatter,
            "component `{}` at `{path}` (saved version {}, current {}): ",
            self.component, self.saved_version, self.current_version
        )?;
        match &self.problem {
            ReflectProblem::UnknownField => formatter.write_str(
                "the type has no such field; register a rename or remove \
                 migration instead of dropping the saved value",
            ),
            ReflectProblem::UnknownVariant(name) => {
                write!(formatter, "the type has no variant `{name}`")
            }
            ReflectProblem::NewerVersion => formatter
                .write_str("saved by a newer build with more migrations"),
            ReflectProblem::UnsavedAsset => formatter.write_str(
                "references an asset that was not loaded from a file",
            ),
            ReflectProblem::Asset(message) => {
                write!(formatter, "asset failed to load: {message}")
            }
            ReflectProblem::WrongKind(expected) => {
                write!(formatter, "expected {expected}")
            }
            ReflectProblem::NoSuchPath => {
                formatter.write_str("the type has nothing at this path")
            }
            ReflectProblem::NotAStruct => formatter
                .write_str("migrations need a component saved as a struct"),
        }
    }
}

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

/// A problem and the JSON pointer it was found at.
pub type Located = (String, ReflectProblem);

impl TypeInfo {
    /// True when values hold an [`Entity`] or a [`Handle`] somewhere, which
    /// saves in a different form than serde writes.
    #[must_use]
    pub fn has_references(&self) -> bool {
        match self {
            Self::Entity | Self::Handle(_) => true,
            Self::Array(item, _)
            | Self::List(item)
            | Self::Map(item)
            | Self::Option(item) => item.has_references(),
            Self::Struct(info) => {
                info.fields.iter().any(|field| field.ty.has_references())
            }
            Self::Enum(info) => {
                info.variants.iter().any(|variant| match &variant.fields {
                    VariantFields::Unit => false,
                    VariantFields::Tuple(items) => {
                        items.iter().any(Self::has_references)
                    }
                    VariantFields::Struct(fields) => {
                        fields.iter().any(|field| field.ty.has_references())
                    }
                })
            }
            _ => false,
        }
    }

    /// A plain value of this type, for new list items and variant switches:
    /// false, zero, empty, `null`, or the first variant.
    #[must_use]
    pub fn zero_value(&self) -> Value {
        match self {
            Self::Bool => Value::Bool(false),
            Self::Int { .. } => json!(0),
            Self::Float => json!(0.0),
            Self::String | Self::Path => json!(""),
            Self::Array(item, len) => {
                Value::Array(vec![item.zero_value(); *len])
            }
            Self::List(_) => json!([]),
            Self::Map(_) => json!({}),
            Self::Option(_) | Self::Entity | Self::Handle(_) => Value::Null,
            Self::Struct(info) => fields_zero(&info.fields),
            Self::Enum(info) => {
                info.variants.first().map_or(Value::Null, variant_zero)
            }
        }
    }

    /// What a value of this type looks like, for error messages.
    #[must_use]
    pub fn kind_name(&self) -> &'static str {
        match self {
            Self::Bool => "a boolean",
            Self::Int { .. } => "an integer",
            Self::Float => "a number",
            Self::String => "a string",
            Self::Path => "a path string",
            Self::Array(..) => "an array of fixed length",
            Self::List(_) => "an array",
            Self::Map(_) => "an object",
            Self::Option(_) => "null or a value",
            Self::Struct(_) => "an object with the type's fields",
            Self::Enum(_) => "a variant name or {\"Variant\": payload}",
            Self::Entity => "an object ID or null",
            Self::Handle(_) => "{\"$asset\": path}",
        }
    }

    /// Shallow check that `value` has this type's scene form.
    #[must_use]
    pub fn accepts(&self, value: &Value) -> bool {
        match self {
            Self::Bool => value.is_boolean(),
            Self::Int { .. } => value.is_i64() || value.is_u64(),
            Self::Float => value.is_number(),
            Self::String | Self::Path => value.is_string(),
            Self::Array(_, len) => {
                value.as_array().is_some_and(|items| items.len() == *len)
            }
            Self::List(_) => value.is_array(),
            Self::Map(_) | Self::Struct(_) => value.is_object(),
            Self::Option(inner) => value.is_null() || inner.accepts(value),
            Self::Enum(_) => value.is_string() || value.is_object(),
            Self::Entity => {
                value.is_null()
                    || value
                        .as_str()
                        .is_some_and(|text| text.parse::<Uuid>().is_ok())
            }
            Self::Handle(_) => {
                value.get(ASSET_KEY).is_some_and(Value::is_string)
            }
        }
    }

    /// The type at a JSON pointer into this type's scene form. Enum
    /// payloads are addressed by variant name, map values by any key, and
    /// an `Option` by its inner type's paths.
    #[must_use]
    pub fn at(&self, pointer: &str) -> Option<&TypeInfo> {
        if pointer.is_empty() {
            return Some(self);
        }
        if let Self::Option(inner) = self {
            return inner.at(pointer);
        }
        let (segment, rest) = split_first(pointer)?;
        match self {
            Self::Struct(info) => field_type(&info.fields, &segment)?.at(rest),
            Self::Map(item) => item.at(rest),
            Self::List(item) => {
                segment.parse::<usize>().ok()?;
                item.at(rest)
            }
            Self::Array(item, len) => {
                (segment.parse::<usize>().ok()? < *len).then_some(())?;
                item.at(rest)
            }
            Self::Enum(info) => match &info.variant(&segment)?.fields {
                VariantFields::Unit => None,
                VariantFields::Tuple(items) if items.len() == 1 => {
                    items[0].at(rest)
                }
                VariantFields::Tuple(items) => {
                    let (index, rest) = split_first(rest)?;
                    items.get(index.parse::<usize>().ok()?)?.at(rest)
                }
                VariantFields::Struct(fields) => {
                    let (name, rest) = split_first(rest)?;
                    field_type(fields, &name)?.at(rest)
                }
            },
            _ => None,
        }
    }
}

fn split_first(pointer: &str) -> Option<(String, &str)> {
    let rest = pointer.strip_prefix('/')?;
    let (segment, rest) = match rest.find('/') {
        Some(end) => (&rest[..end], &rest[end..]),
        None => (rest, ""),
    };
    Some((segment.replace("~1", "/").replace("~0", "~"), rest))
}

fn field_type<'a>(fields: &'a [FieldInfo], name: &str) -> Option<&'a TypeInfo> {
    fields
        .iter()
        .find(|field| field.name == name)
        .map(|field| &field.ty)
}

fn fields_zero(fields: &[FieldInfo]) -> Value {
    Value::Object(
        fields
            .iter()
            .map(|field| (field.name.to_owned(), field.ty.zero_value()))
            .collect(),
    )
}

/// The saved form of `variant` with zero payload.
#[must_use]
pub fn variant_zero(variant: &VariantInfo) -> Value {
    let payload = match &variant.fields {
        VariantFields::Unit => return json!(variant.name),
        VariantFields::Tuple(items) if items.len() == 1 => {
            items[0].zero_value()
        }
        VariantFields::Tuple(items) => {
            Value::Array(items.iter().map(TypeInfo::zero_value).collect())
        }
        VariantFields::Struct(fields) => fields_zero(fields),
    };
    json!({ variant.name: payload })
}

fn escape(segment: &str) -> String {
    segment.replace('~', "~0").replace('/', "~1")
}

/// Walks `value` as `info`: rejects object keys and variants the type does
/// not know, and calls `visit` on every non-null entity and handle.
pub fn walk(
    info: &TypeInfo,
    value: &mut Value,
    visit: &mut dyn FnMut(&TypeInfo, &mut Value) -> Result<(), ReflectProblem>,
) -> Result<(), Located> {
    walk_at(info, value, &mut String::new(), visit)
}

fn walk_at(
    info: &TypeInfo,
    value: &mut Value,
    pointer: &mut String,
    visit: &mut dyn FnMut(&TypeInfo, &mut Value) -> Result<(), ReflectProblem>,
) -> Result<(), Located> {
    let located = |pointer: &String, problem| (pointer.clone(), problem);
    match (info, value) {
        // Entities are visited even when null: a dangling reference saves
        // as null and loads as `Entity::PLACEHOLDER`.
        (TypeInfo::Entity, value) => {
            visit(info, value).map_err(|problem| located(pointer, problem))
        }
        (TypeInfo::Handle(_), value) if !value.is_null() => {
            visit(info, value).map_err(|problem| located(pointer, problem))
        }
        (TypeInfo::Option(inner), value) if !value.is_null() => {
            walk_at(inner, value, pointer, visit)?;
            if **inner == TypeInfo::Entity && *value == placeholder() {
                *value = Value::Null;
            }
            Ok(())
        }
        (TypeInfo::Struct(info), Value::Object(map)) => {
            walk_fields(&info.fields, map, pointer, visit)
        }
        (
            TypeInfo::Array(item, _) | TypeInfo::List(item),
            Value::Array(items),
        ) => {
            for (index, value) in items.iter_mut().enumerate() {
                let length = pointer.len();
                pointer.push_str(&format!("/{index}"));
                walk_at(item, value, pointer, visit)?;
                pointer.truncate(length);
            }
            Ok(())
        }
        (TypeInfo::Map(item), Value::Object(map)) => {
            for (key, value) in map.iter_mut() {
                let length = pointer.len();
                pointer.push('/');
                pointer.push_str(&escape(key));
                walk_at(item, value, pointer, visit)?;
                pointer.truncate(length);
            }
            Ok(())
        }
        (TypeInfo::Enum(info), Value::String(name)) => {
            if info.variant(name).is_some() {
                Ok(())
            } else {
                Err(located(
                    pointer,
                    ReflectProblem::UnknownVariant(name.clone()),
                ))
            }
        }
        (TypeInfo::Enum(info), Value::Object(map)) if map.len() == 1 => {
            let (name, payload) = map.iter_mut().next().expect("one entry");
            let Some(variant) = info.variant(name) else {
                return Err(located(
                    pointer,
                    ReflectProblem::UnknownVariant(name.clone()),
                ));
            };
            let length = pointer.len();
            pointer.push('/');
            pointer.push_str(&escape(name));
            match (&variant.fields, payload) {
                (VariantFields::Struct(fields), Value::Object(map)) => {
                    walk_fields(fields, map, pointer, visit)?;
                }
                (VariantFields::Tuple(items), payload) if items.len() == 1 => {
                    walk_at(&items[0], payload, pointer, visit)?;
                }
                (VariantFields::Tuple(items), Value::Array(values)) => {
                    for (index, (item, value)) in
                        items.iter().zip(values.iter_mut()).enumerate()
                    {
                        let length = pointer.len();
                        pointer.push_str(&format!("/{index}"));
                        walk_at(item, value, pointer, visit)?;
                        pointer.truncate(length);
                    }
                }
                _ => {}
            }
            pointer.truncate(length);
            Ok(())
        }
        // Serde reports values of the wrong kind with its own message.
        _ => Ok(()),
    }
}

fn walk_fields(
    fields: &[FieldInfo],
    map: &mut serde_json::Map<String, Value>,
    pointer: &mut String,
    visit: &mut dyn FnMut(&TypeInfo, &mut Value) -> Result<(), ReflectProblem>,
) -> Result<(), Located> {
    for (key, value) in map.iter_mut() {
        let length = pointer.len();
        pointer.push('/');
        pointer.push_str(&escape(key));
        let Some(field) = fields.iter().find(|field| field.name == key) else {
            return Err((pointer.clone(), ReflectProblem::UnknownField));
        };
        walk_at(&field.ty, value, pointer, visit)?;
        pointer.truncate(length);
    }
    Ok(())
}

/// Removes the saved version from `value`: 0 when it has none.
pub fn take_version(value: &mut Value) -> Result<u32, Located> {
    match value
        .as_object_mut()
        .and_then(|map| map.remove(VERSION_KEY))
    {
        Some(version) => version
            .as_u64()
            .and_then(|version| u32::try_from(version).ok())
            .ok_or_else(|| {
                (
                    format!("/{}", escape(VERSION_KEY)),
                    ReflectProblem::WrongKind("a version number"),
                )
            }),
        None => Ok(0),
    }
}

/// Runs the migrations after `saved` on a value saved at that version.
pub fn migrate(
    value: &mut Value,
    saved: u32,
    migrations: &[FieldMigration],
) -> Result<(), Located> {
    let pending = migrations
        .get(saved as usize..)
        .ok_or((String::new(), ReflectProblem::NewerVersion))?;
    if let Value::Object(map) = value {
        for migration in pending {
            match migration {
                FieldMigration::Rename { from, to } => {
                    if let Some(moved) = map.remove(from) {
                        map.insert(to.clone(), moved);
                    }
                }
                FieldMigration::Remove(field) => {
                    map.remove(field);
                }
            }
        }
    }
    Ok(())
}

fn placeholder() -> Value {
    Entity::PLACEHOLDER.to_bits().into()
}

/// Turns entities and handles in serde's output into their scene form. A
/// reference to an entity that is gone or has no `SceneId` saves as null.
pub fn to_scene_form(
    info: &TypeInfo,
    value: &mut Value,
    world: &World,
) -> Result<(), Located> {
    walk(info, value, &mut |info, value| match info {
        TypeInfo::Entity => {
            *value = value
                .as_u64()
                .and_then(Entity::try_from_bits)
                .and_then(|entity| world.get::<SceneId>(entity))
                .map_or(Value::Null, |id| Value::String(id.0.to_string()));
            Ok(())
        }
        TypeInfo::Handle(kind) => {
            let key = value
                .as_u64()
                .ok_or(ReflectProblem::WrongKind("a handle key"))?;
            let assets = world
                .get_resource::<AssetServer>()
                .ok_or(ReflectProblem::UnsavedAsset)?;
            let path =
                (kind.path)(assets, key).ok_or(ReflectProblem::UnsavedAsset)?;
            *value = json!({ ASSET_KEY: path });
            Ok(())
        }
        _ => Ok(()),
    })
}

/// Turns scene-form entities and handles back into what serde reads,
/// loading referenced assets. `ids` maps scene IDs to entities; without it,
/// the world's `SceneId`s are searched. Null and IDs no entity has load as
/// `Entity::PLACEHOLDER`, or `None` inside an `Option`, so deleting an
/// object leaves references to it dangling instead of failing the scene.
pub fn from_scene_form(
    info: &TypeInfo,
    value: &mut Value,
    world: &mut World,
    ids: Option<&HashMap<Uuid, Entity>>,
) -> Result<(), Located> {
    // ponytail: builds the whole ID map for one edit; keep a map resource
    // if entity references in large scenes are edited often.
    let mut searched: Option<HashMap<Uuid, Entity>> = None;
    walk(info, value, &mut |info, value| match info {
        TypeInfo::Entity => {
            if value.is_null() {
                *value = placeholder();
                return Ok(());
            }
            let id = value
                .as_str()
                .and_then(|text| text.parse::<Uuid>().ok())
                .ok_or(ReflectProblem::WrongKind("an object ID"))?;
            let ids = match ids {
                Some(ids) => ids,
                None => searched.get_or_insert_with(|| {
                    let mut query = world.query::<(Entity, &SceneId)>();
                    query
                        .iter(world)
                        .map(|(entity, id)| (id.0, entity))
                        .collect()
                }),
            };
            let entity = ids.get(&id).copied().unwrap_or(Entity::PLACEHOLDER);
            *value = entity.to_bits().into();
            Ok(())
        }
        TypeInfo::Handle(kind) => {
            let path = value
                .get(ASSET_KEY)
                .and_then(Value::as_str)
                .ok_or(ReflectProblem::WrongKind("{\"$asset\": path}"))?;
            let mut assets = world
                .get_resource_mut::<AssetServer>()
                .ok_or(ReflectProblem::Asset("no AssetServer".into()))?;
            let key = kind
                .load(&mut assets, Path::new(path))
                .map_err(|error| ReflectProblem::Asset(error.to_string()))?;
            *value = key.into();
            Ok(())
        }
        _ => Ok(()),
    })
}

/// Applies `convert` to every `{"$asset": path}` in a saved component.
pub fn map_asset_paths<E>(
    value: &mut Value,
    convert: &mut impl FnMut(&Path) -> Result<PathBuf, E>,
) -> Result<(), E> {
    match value {
        Value::Object(map) => {
            if map.len() == 1 {
                if let Some(Value::String(path)) = map.get_mut(ASSET_KEY) {
                    let converted = convert(Path::new(path.as_str()))?;
                    *path = converted.to_string_lossy().into_owned();
                    return Ok(());
                }
            }
            map.values_mut()
                .try_for_each(|value| map_asset_paths(value, convert))
        }
        Value::Array(items) => items
            .iter_mut()
            .try_for_each(|value| map_asset_paths(value, convert)),
        _ => Ok(()),
    }
}

/// The engine's reflected resource and asset types, listed by
/// `rusting schema`. Components are in `SceneComponentRegistry`, which also
/// stores them.
pub struct TypeRegistry {
    resources: BTreeMap<String, TypeInfo>,
    assets: BTreeMap<String, TypeInfo>,
}

impl Default for TypeRegistry {
    fn default() -> Self {
        let mut registry = Self {
            resources: BTreeMap::new(),
            assets: BTreeMap::new(),
        };
        builtin::register(&mut registry);
        registry
    }
}

impl TypeRegistry {
    pub fn register_resource<T: Resource + Reflect>(
        &mut self,
        name: impl Into<String>,
    ) {
        self.resources.insert(name.into(), T::type_info());
    }

    pub fn register_asset<T: Reflect>(&mut self, name: impl Into<String>) {
        self.assets.insert(name.into(), T::type_info());
    }

    pub fn resources(&self) -> impl Iterator<Item = (&str, &TypeInfo)> {
        self.resources
            .iter()
            .map(|(name, info)| (name.as_str(), info))
    }

    pub fn assets(&self) -> impl Iterator<Item = (&str, &TypeInfo)> {
        self.assets.iter().map(|(name, info)| (name.as_str(), info))
    }
}

#[cfg(test)]
mod tests;