bevy_reflect/error.rs
1use crate::FieldId;
2use alloc::{borrow::Cow, format};
3use thiserror::Error;
4
5/// An error that occurs when cloning a type via [`PartialReflect::reflect_clone`].
6///
7/// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
8#[derive(Clone, Debug, Error, PartialEq, Eq)]
9pub enum ReflectCloneError {
10 /// The type does not have a custom implementation for [`PartialReflect::reflect_clone`].
11 ///
12 /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
13 #[error("`PartialReflect::reflect_clone` not implemented for `{type_path}`")]
14 NotImplemented {
15 /// The fully qualified path of the type that [`PartialReflect::reflect_clone`](crate::PartialReflect::reflect_clone) is not implemented for.
16 type_path: Cow<'static, str>,
17 },
18 /// The type cannot be cloned via [`PartialReflect::reflect_clone`].
19 ///
20 /// This type should be returned when a type is intentionally opting out of reflection cloning.
21 ///
22 /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
23 #[error("`{type_path}` cannot be made cloneable for `PartialReflect::reflect_clone`")]
24 NotCloneable {
25 /// The fully qualified path of the type that cannot be cloned via [`PartialReflect::reflect_clone`](crate::PartialReflect::reflect_clone).
26 type_path: Cow<'static, str>,
27 },
28 /// The field cannot be cloned via [`PartialReflect::reflect_clone`].
29 ///
30 /// When [deriving `Reflect`], this usually means that a field marked with `#[reflect(ignore)]`
31 /// is missing a `#[reflect(clone)]` attribute.
32 ///
33 /// This may be intentional if the field is not meant/able to be cloned.
34 ///
35 /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
36 /// [deriving `Reflect`]: derive@crate::Reflect
37 #[error(
38 "field `{}` cannot be made cloneable for `PartialReflect::reflect_clone` (are you missing a `#[reflect(clone)]` attribute?)",
39 full_path(.field, .variant.as_deref(), .container_type_path)
40 )]
41 FieldNotCloneable {
42 /// Struct field or enum variant field which cannot be cloned.
43 field: FieldId,
44 /// Variant this field is part of if the container is an enum, otherwise [`None`].
45 variant: Option<Cow<'static, str>>,
46 /// Fully qualified path of the type containing this field.
47 container_type_path: Cow<'static, str>,
48 },
49 /// Could not downcast to the expected type.
50 ///
51 /// Realistically this should only occur when a type has incorrectly implemented [`Reflect`].
52 ///
53 /// [`Reflect`]: crate::Reflect
54 #[error("expected downcast to `{expected}`, but received `{received}`")]
55 FailedDowncast {
56 /// The fully qualified path of the type that was expected.
57 expected: Cow<'static, str>,
58 /// The fully qualified path of the type that was received.
59 received: Cow<'static, str>,
60 },
61}
62
63fn full_path(
64 field: &FieldId,
65 variant: Option<&str>,
66 container_type_path: &str,
67) -> alloc::string::String {
68 match variant {
69 Some(variant) => format!("{container_type_path}::{variant}::{field}"),
70 None => format!("{container_type_path}::{field}"),
71 }
72}