bevy_mod_scripting_bindings 0.19.0

Core traits and structures required for smoothly interfacing with other languages in a generic way
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
//! Error types for the bindings
use crate::{
    FunctionCallContext, Namespace, ReflectBaseType, ReflectReference, access_map::ReflectAccessId,
    script_value::ScriptValue,
};
use bevy_ecs::entity::Entity;
use bevy_mod_scripting_asset::Language;
use bevy_mod_scripting_derive::DebugWithTypeInfo;
use bevy_mod_scripting_display::{
    DebugWithTypeInfo, DisplayWithTypeInfo, GetTypeInfo, OrFakeId, PrintReflectAsDebug,
    WithTypeInfo,
};
use bevy_reflect::{ApplyError, PartialReflect, Reflect};
use std::{any::TypeId, borrow::Cow, error::Error, fmt::Display, panic::Location, sync::Arc};

/// A wrapper around a reflect value to implement various traits useful for error reporting.
#[derive(Clone)]
pub struct ReflectWrapper(Arc<dyn PartialReflect>);

impl bevy_mod_scripting_display::DebugWithTypeInfo for ReflectWrapper {
    fn to_string_with_type_info(
        &self,
        f: &mut std::fmt::Formatter,
        type_info_provider: Option<&dyn GetTypeInfo>,
    ) -> std::fmt::Result {
        PrintReflectAsDebug::new_with_opt_info(&*self.0, type_info_provider)
            .to_string_with_type_info(f, type_info_provider)
    }
}

impl DisplayWithTypeInfo for ReflectWrapper {
    fn display_with_type_info(
        &self,
        f: &mut std::fmt::Formatter,
        type_info_provider: Option<&dyn GetTypeInfo>,
    ) -> std::fmt::Result {
        // TODO: different display?
        PrintReflectAsDebug::new_with_opt_info(&*self.0, type_info_provider)
            .to_string_with_type_info(f, type_info_provider)
    }
}

/// An error that occurred in an external library.
#[derive(Clone)]
pub struct ExternalError(pub Arc<dyn Error + Send + Sync>);

impl bevy_mod_scripting_display::DebugWithTypeInfo for ExternalError {
    fn to_string_with_type_info(
        &self,
        f: &mut std::fmt::Formatter,
        _type_info_provider: Option<&dyn GetTypeInfo>,
    ) -> std::fmt::Result {
        write!(f, "External error: {}", self.0)
    }
}

impl DisplayWithTypeInfo for ExternalError {
    fn display_with_type_info(
        &self,
        f: &mut std::fmt::Formatter,
        _type_info_provider: Option<&dyn GetTypeInfo>,
    ) -> std::fmt::Result {
        write!(f, "External error: {}", self.0)
    }
}

/// An error occurring when converting between rust and a script context.
#[derive(Clone, Reflect, DebugWithTypeInfo)]
#[debug_with_type_info(bms_display_path = "bevy_mod_scripting_display")]
#[reflect(opaque)]
pub enum InteropError {
    /// The given feature is not implemented
    NotImplemented,
    /// A script value was of the wrong type
    ValueMismatch {
        /// The value with the wrong type
        value: Box<ScriptValue>,
        /// The expected type
        expected: Box<TypeId>,
    },
    /// The length of a list like structure was wrong
    LengthMismatch {
        /// The expected length
        expected: usize,
        /// The actual length
        got: usize,
    },
    /// Something failed when converting from a reflect
    FailedFromReflect {
        /// The type id of the reflect
        type_id: Box<Option<TypeId>>,
        /// The reason for the failure
        reason: Box<String>,
    },
    /// A type mismatch occurred
    TypeMismatch {
        /// The expected type
        expected: Box<TypeId>,
        /// The actual type
        got: Box<Option<TypeId>>,
    },
    /// A type mismatch occurred, but with a string representation of the expected type
    StringTypeMismatch {
        /// The expected type
        expected: Box<String>,
        /// The actual type
        got: Box<Option<TypeId>>,
    },
    /// Could not claim access to a value
    CannotClaimAccess {
        /// The id of the access
        base: Box<ReflectAccessId>,
        /// The location of the access
        location: Box<Option<Location<'static>>>,
        /// The context of the access
        context: Box<Cow<'static, str>>,
    },
    /// An invariant was broken
    Invariant(Box<String>),
    /// An unregistered component or resource type was used
    UnregisteredComponentOrResourceType {
        /// The name of the type
        type_name: Box<Cow<'static, str>>,
    },
    /// An unsupported operation was performed
    UnsupportedOperation {
        /// The base type of the operation
        base: Box<Option<TypeId>>,
        /// The value of the operation
        value: Box<Option<ReflectWrapper>>,
        /// The operation
        operation: Box<String>,
    },
    /// Some type data was missing
    MissingTypeData {
        /// The type id of the missing data
        type_id: Box<TypeId>,
        /// The missing type data
        type_data: Box<String>,
    },
    /// An entity was missing
    MissingEntity(Entity),

    /// An error occurred in a function
    FunctionInteropError {
        /// The name of the function
        function_name: Box<String>,
        /// The namespace of the function
        on: Box<Namespace>,
        /// The error that occurred
        error: Box<InteropError>,
        /// The context at the time of the call
        context: Box<Option<FunctionCallContext>>,
    },
    /// An error occurred when converting a function argument
    FunctionArgConversionError {
        /// The argument that failed to convert
        argument: Box<String>,
        /// The error that occurred
        error: Box<InteropError>,
    },
    /// The number of arguments in a function call was wrong
    ArgumentCountMismatch {
        /// The expected number of arguments
        expected: usize,
        /// The actual number of arguments
        got: usize,
    },
    /// A garbage collected allocation was used
    GarbageCollectedAllocation {
        /// The reference to the allocation
        reference: Box<ReflectReference>,
    },
    /// An unregistered reflect base was used
    UnregisteredReflectBase {
        /// The base that was unregistered
        base: Box<ReflectBaseType>,
    },
    /// An error occurred when using a reflection path
    ReflectionPathError {
        /// The error that occurred
        error: Box<String>,
        /// The reflected value
        reflected: Option<Box<ReflectReference>>,
    },
    /// Could not downcast a reference
    CouldNotDowncastReference {
        /// The type to downcast to
        to: Box<TypeId>,
        /// The reference to downcast
        reference: Box<ReflectReference>,
    },
    /// A schedule was missing
    MissingSchedule {
        /// The name of the schedule
        schedule_name: &'static str,
    },
    /// An invalid index was used
    InvalidIndex {
        /// The index that was invalid
        index: Box<ScriptValue>,
        /// The reason the index was invalid
        reason: Box<String>,
    },
    /// The world was missing
    MissingWorld,
    /// An external error occurred
    External(ExternalError),
    /// an error enriched with some contextual information
    WithContext(
        /// The context to add
        Box<Cow<'static, str>>,
        /// The error to add context to
        Box<InteropError>,
    ),
}

impl InteropError {
    /// Strips outer context layers from the error, returning all contexts and the base error
    pub fn unwrap_context(self) -> (Vec<Cow<'static, str>>, InteropError) {
        let mut contexts = Vec::new();
        let mut current = self;
        while let InteropError::WithContext(context, err) = current {
            contexts.push(*context);
            current = *err;
        }
        (contexts, current)
    }

    /// Adds context to an existing error
    pub fn with_context(self, context: impl Into<Cow<'static, str>>) -> Self {
        Self::WithContext(Box::new(context.into()), Box::new(self))
    }

    /// Creates a new external error.
    pub fn external(error: impl std::error::Error + Send + Sync + 'static) -> Self {
        Self::External(ExternalError(Arc::new(error)))
    }

    /// Creates a new external error from a boxed error.
    pub fn external_boxed(error: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
        Self::External(ExternalError(Arc::from(error)))
    }

    /// Creates a new external error from a static string.
    pub fn str(reason: &'static str) -> Self {
        Self::External(ExternalError(Arc::from(Box::<
            dyn std::error::Error + Send + Sync + 'static,
        >::from(reason))))
    }

    /// Creates a new external error from a string.
    pub fn string(reason: String) -> Self {
        Self::External(ExternalError(Arc::from(Box::<
            dyn std::error::Error + Send + Sync + 'static,
        >::from(reason))))
    }

    /// Creates a new value mismatch error.
    pub fn value_mismatch(expected: TypeId, value: ScriptValue) -> Self {
        Self::ValueMismatch {
            value: Box::new(value),
            expected: Box::new(expected),
        }
    }

    /// Creates a new length mismatch error.
    pub fn length_mismatch(expected: usize, got: usize) -> Self {
        Self::LengthMismatch { expected, got }
    }

    /// Creates a new failed from reflect error.
    pub fn failed_from_reflect(type_id: Option<TypeId>, reason: impl Display) -> Self {
        Self::FailedFromReflect {
            type_id: Box::new(type_id),
            reason: Box::new(reason.to_string()),
        }
    }

    /// Creates a new type mismatch error.
    pub fn type_mismatch(expected: TypeId, got: Option<TypeId>) -> Self {
        Self::TypeMismatch {
            expected: Box::new(expected),
            got: Box::new(got),
        }
    }

    /// Creates a new string type mismatch error.
    pub fn string_type_mismatch(expected: String, got: Option<TypeId>) -> Self {
        Self::StringTypeMismatch {
            expected: Box::new(expected),
            got: Box::new(got),
        }
    }

    /// Creates a new cannot claim access error.
    pub fn cannot_claim_access(
        base: ReflectAccessId,
        location: Option<Location<'static>>,
        context: impl Into<Cow<'static, str>>,
    ) -> Self {
        Self::CannotClaimAccess {
            base: Box::new(base),
            location: Box::new(location),
            context: Box::new(context.into()),
        }
    }

    /// Creates a new invariant error.
    pub fn invariant(reason: impl Into<String>) -> Self {
        Self::Invariant(Box::new(reason.into()))
    }

    /// Creates a new unregistered component or resource type error.
    pub fn unregistered_component_or_resource_type(
        type_name: impl Into<Cow<'static, str>>,
    ) -> Self {
        Self::UnregisteredComponentOrResourceType {
            type_name: Box::new(type_name.into()),
        }
    }

    /// Creates a new unsupported operation error.
    pub fn unsupported_operation(
        base: Option<TypeId>,
        value: Option<Box<dyn PartialReflect>>,
        op: impl Display,
    ) -> Self {
        Self::UnsupportedOperation {
            base: Box::new(base),
            value: Box::new(value.map(|v| ReflectWrapper(Arc::from(v)))),
            operation: Box::new(op.to_string()),
        }
    }

    /// Creates a new missing type data error.
    pub fn missing_type_data(type_id: TypeId, type_data: String) -> Self {
        Self::MissingTypeData {
            type_id: Box::new(type_id),
            type_data: Box::new(type_data),
        }
    }

    /// Creates a new missing entity error.
    pub fn missing_entity(entity: Entity) -> Self {
        Self::MissingEntity(entity)
    }

    /// Creates a new reflect apply error.
    pub fn reflect_apply_error(e: ApplyError) -> Self {
        Self::External(ExternalError(Arc::new(e)))
    }

    /// Creates a new function interop error.
    pub fn function_interop_error(
        function_name: impl Display,
        on: Namespace,
        error: InteropError,
        context: Option<FunctionCallContext>,
    ) -> Self {
        Self::FunctionInteropError {
            function_name: Box::new(function_name.to_string()),
            on: Box::new(on),
            error: Box::new(error),
            context: Box::new(context),
        }
    }

    /// Thrown when an error happens during argument conversion in a function call
    pub fn function_arg_conversion_error(argument: String, error: InteropError) -> Self {
        Self::FunctionArgConversionError {
            argument: Box::new(argument),
            error: Box::new(error),
        }
    }

    /// Thrown when the number of arguments in a function call does not match.
    pub fn argument_count_mismatch(expected: usize, got: usize) -> Self {
        Self::ArgumentCountMismatch { expected, got }
    }

    /// Creates a new garbage collected allocation error.
    pub fn garbage_collected_allocation(reference: ReflectReference) -> Self {
        Self::GarbageCollectedAllocation {
            reference: Box::new(reference),
        }
    }

    /// Creates a new unregistered base error.
    pub fn unregistered_base(base: ReflectBaseType) -> Self {
        Self::UnregisteredReflectBase {
            base: Box::new(base),
        }
    }

    /// Creates a new reflection path error.
    pub fn reflection_path_error(error: String, reflected: Option<ReflectReference>) -> Self {
        Self::ReflectionPathError {
            error: Box::new(error),
            reflected: reflected.map(Box::new),
        }
    }

    /// Creates a new could not downcast error.
    pub fn could_not_downcast(reference: ReflectReference, to: TypeId) -> Self {
        Self::CouldNotDowncastReference {
            to: Box::new(to),
            reference: Box::new(reference),
        }
    }

    /// Creates a new missing schedule error.
    pub fn missing_schedule(schedule_name: &'static str) -> Self {
        Self::MissingSchedule { schedule_name }
    }

    /// Creates a new invalid index error.
    pub fn invalid_index(index: ScriptValue, reason: impl Into<String>) -> Self {
        Self::InvalidIndex {
            index: Box::new(index),
            reason: Box::new(reason.into()),
        }
    }

    /// Creates a new missing world error.
    pub fn missing_world() -> Self {
        Self::MissingWorld
    }

    /// Creates a new missing function error.
    pub fn missing_function(
        function_name: impl Display,
        on: Namespace,
        context: Option<FunctionCallContext>,
    ) -> Self {
        Self::FunctionInteropError {
            function_name: Box::new(function_name.to_string()),
            on: Box::new(on),
            error: Box::new(InteropError::str("Function not found")),
            context: Box::new(context),
        }
    }
}

impl std::fmt::Display for InteropError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        std::fmt::Display::fmt(&WithTypeInfo::new(self), f)
    }
}

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

impl DisplayWithTypeInfo for InteropError {
    fn display_with_type_info(
        &self,
        f: &mut std::fmt::Formatter,
        type_info_provider: Option<&dyn GetTypeInfo>,
    ) -> std::fmt::Result {
        match self {
            InteropError::NotImplemented => {
                write!(f, "This feature is not implemented")
            }
            InteropError::ValueMismatch { value, expected } => {
                write!(
                    f,
                    "Value type mismatch: expected {}, got {}",
                    WithTypeInfo::new_with_opt_info(expected, type_info_provider),
                    WithTypeInfo::new_with_opt_info(value, type_info_provider)
                )
            }
            InteropError::LengthMismatch { expected, got } => {
                write!(f, "Length mismatch: expected {expected}, got {got}")
            }
            InteropError::FailedFromReflect { type_id, reason } => {
                write!(
                    f,
                    "Failed to convert from reflect {}: {}",
                    WithTypeInfo::new_with_opt_info(
                        &type_id.as_ref().or_fake_id(),
                        type_info_provider
                    ),
                    reason
                )
            }
            InteropError::TypeMismatch { expected, got } => {
                write!(
                    f,
                    "Type mismatch: expected {}, got {}",
                    WithTypeInfo::new_with_opt_info(expected, type_info_provider),
                    WithTypeInfo::new_with_opt_info(&got.as_ref().or_fake_id(), type_info_provider)
                )
            }
            InteropError::StringTypeMismatch { expected, got } => {
                write!(
                    f,
                    "Type mismatch: expected {}, got {}",
                    expected,
                    WithTypeInfo::new_with_opt_info(&got.as_ref().or_fake_id(), type_info_provider)
                )
            }
            InteropError::CannotClaimAccess {
                base,
                location,
                context,
            } => {
                if let Some(location) = location.as_ref() {
                    write!(
                        f,
                        "Cannot claim access to {} at {}:{}:{}: {}",
                        WithTypeInfo::new_with_opt_info(base, type_info_provider),
                        location.file(),
                        location.line(),
                        location.column(),
                        context
                    )
                } else {
                    write!(
                        f,
                        "Cannot claim access to {}: {}",
                        WithTypeInfo::new_with_opt_info(base, type_info_provider),
                        context
                    )
                }
            }
            InteropError::Invariant(i) => {
                write!(f, "Invariant broken: {i}")
            }
            InteropError::UnregisteredComponentOrResourceType { type_name } => {
                write!(f, "Unregistered component or resource type: {type_name}")
            }
            InteropError::UnsupportedOperation {
                base,
                value,
                operation,
            } => {
                write!(
                    f,
                    "Unsupported operation on {}{}: {}",
                    WithTypeInfo::new_with_opt_info(
                        &base.as_ref().or_fake_id(),
                        type_info_provider
                    ),
                    if let Some(value) = value.as_ref() {
                        format!(
                            " with value {}",
                            WithTypeInfo::new_with_opt_info(value, type_info_provider)
                        )
                    } else {
                        "".to_string()
                    },
                    operation
                )
            }
            InteropError::MissingTypeData { type_id, type_data } => {
                write!(
                    f,
                    "Missing type data {} for type: {}",
                    type_data,
                    WithTypeInfo::new_with_opt_info(type_id, type_info_provider)
                )
            }
            InteropError::MissingEntity(entity) => {
                if *entity == Entity::PLACEHOLDER || entity.index().index() == 0 {
                    write!(
                        f,
                        "Invalid entity: {entity}. Are you trying to use an entity in a callback in which it's unavailable?"
                    )
                } else {
                    write!(f, "Missing or invalid entity: {entity}")
                }
            }
            InteropError::FunctionInteropError {
                function_name,
                on,
                error,
                context,
            } => {
                write!(
                    f,
                    "Error {}\n in function {} on {}:\n {}",
                    context
                        .clone()
                        .unwrap_or(FunctionCallContext::new(Language::Unknown)),
                    function_name,
                    WithTypeInfo::new_with_opt_info(on, type_info_provider),
                    error
                )
            }
            InteropError::FunctionArgConversionError { argument, error } => {
                write!(
                    f,
                    "Error converting argument {}: {}",
                    argument,
                    WithTypeInfo::new_with_opt_info(error, type_info_provider)
                )
            }
            InteropError::ArgumentCountMismatch { expected, got } => {
                write!(f, "Argument count mismatch: expected {expected}, got {got}")
            }
            InteropError::GarbageCollectedAllocation { reference } => {
                write!(
                    f,
                    "Garbage collected allocation used: {}",
                    WithTypeInfo::new_with_opt_info(reference, type_info_provider)
                )
            }
            InteropError::UnregisteredReflectBase { base } => {
                write!(
                    f,
                    "Unregistered reflect base: {}",
                    WithTypeInfo::new_with_opt_info(base, type_info_provider)
                )
            }
            InteropError::ReflectionPathError { error, reflected } => {
                write!(
                    f,
                    "Reflection path error: {}{}",
                    error,
                    if let Some(reflected) = reflected.as_ref() {
                        format!(
                            "\nOn value: {}",
                            WithTypeInfo::new_with_opt_info(reflected, type_info_provider)
                        )
                    } else {
                        "".to_string()
                    }
                )
            }
            InteropError::CouldNotDowncastReference { to, reference } => {
                write!(
                    f,
                    "Could not downcast reference {} to type {}",
                    WithTypeInfo::new_with_opt_info(reference, type_info_provider),
                    WithTypeInfo::new_with_opt_info(to, type_info_provider)
                )
            }
            InteropError::MissingSchedule { schedule_name } => {
                write!(f, "Missing schedule: {schedule_name}")
            }
            InteropError::InvalidIndex { index, reason } => {
                write!(
                    f,
                    "Invalid index {}: {}",
                    WithTypeInfo::new_with_opt_info(index, type_info_provider),
                    reason
                )
            }
            InteropError::MissingWorld => {
                write!(f, "Missing world")
            }
            InteropError::External(external_error) => {
                write!(
                    f,
                    "External error: {}",
                    WithTypeInfo::new_with_opt_info(external_error, type_info_provider)
                )
            }
            InteropError::WithContext(cow, interop_error) => {
                write!(
                    f,
                    "{}: {}",
                    cow,
                    WithTypeInfo::new_with_opt_info(interop_error, type_info_provider)
                )
            }
        }
    }
}

#[cfg(test)]
mod test {
    use bevy_reflect::TypeRegistry;

    use super::*;
    #[test]
    fn test_script_value_prints_using_type_data() {
        // check script values print fine
        let mut registry = TypeRegistry::empty();
        registry.register::<ScriptValue>();
        pretty_assertions::assert_str_eq!(
            format!(
                "{:?}",
                PrintReflectAsDebug::new_with_opt_info(&ScriptValue::Integer(1), Some(&registry))
            ),
            "1",
        );
    }
}