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 { type_path: Cow<'static, str> },
15    /// The type cannot be cloned via [`PartialReflect::reflect_clone`].
16    ///
17    /// This type should be returned when a type is intentionally opting out of reflection cloning.
18    ///
19    /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
20    #[error("`{type_path}` cannot be made cloneable for `PartialReflect::reflect_clone`")]
21    NotCloneable { type_path: Cow<'static, str> },
22    /// The field cannot be cloned via [`PartialReflect::reflect_clone`].
23    ///
24    /// When [deriving `Reflect`], this usually means that a field marked with `#[reflect(ignore)]`
25    /// is missing a `#[reflect(clone)]` attribute.
26    ///
27    /// This may be intentional if the field is not meant/able to be cloned.
28    ///
29    /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
30    /// [deriving `Reflect`]: derive@crate::Reflect
31    #[error(
32        "field `{}` cannot be made cloneable for `PartialReflect::reflect_clone` (are you missing a `#[reflect(clone)]` attribute?)",
33        full_path(.field, .variant.as_deref(), .container_type_path)
34    )]
35    FieldNotCloneable {
36        field: FieldId,
37        variant: Option<Cow<'static, str>>,
38        container_type_path: Cow<'static, str>,
39    },
40    /// Could not downcast to the expected type.
41    ///
42    /// Realistically this should only occur when a type has incorrectly implemented [`Reflect`].
43    ///
44    /// [`Reflect`]: crate::Reflect
45    #[error("expected downcast to `{expected}`, but received `{received}`")]
46    FailedDowncast {
47        expected: Cow<'static, str>,
48        received: Cow<'static, str>,
49    },
50}
51
52fn full_path(
53    field: &FieldId,
54    variant: Option<&str>,
55    container_type_path: &str,
56) -> alloc::string::String {
57    match variant {
58        Some(variant) => format!("{}::{}::{}", container_type_path, variant, field),
59        None => format!("{}::{}", container_type_path, field),
60    }
61}