bevy_convars/
error.rs

1use std::fmt::Display;
2
3use bevy_ecs::world::error::ResourceFetchError;
4use bevy_reflect::ApplyError;
5#[cfg(feature = "parse_cvars")]
6use toml_edit::TomlError;
7
8/// Errors that can occur when manipulating CVars.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum CVarError {
12    /// Error indicating a CVar was never registered or is invalid.
13    UnknownCVar,
14    /// Error indicating the given CVar type is invalid.
15    BadCVarType,
16    /// Error indicating the CVar type is missing a [ComponentId](bevy_ecs::component::ComponentId) and is likely not registered correctly.
17    MissingCid,
18    /// Error indicating the underlying type of the CVar cannot be deserialized, and as such cannot be reflected over.
19    CannotDeserialize,
20    /// Error indicating the CVar failed to deserialize.
21    FailedDeserialize(String),
22    /// Error indicating applying a value to the CVar failed, containing the inner error.
23    FailedApply {
24        /// The inner error.
25        inner: ApplyError,
26    },
27    /// Error indicating that the world could not fulfill the requested operation due to an access conflict with an ongoing operation.
28    AccessConflict,
29    #[cfg(feature = "parse_cvars")]
30    /// An error when parsing a TOML document.
31    TomlError(TomlError),
32
33    /// An error when saving encounters malformed TOML structure.
34    MalformedConfigDuringWrite(&'static str),
35
36    /// An error when serializing TOML data.
37    #[cfg(feature = "parse_cvars")]
38    TomlSerError(toml_edit::ser::Error),
39}
40
41impl std::error::Error for CVarError {}
42
43impl Display for CVarError {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            CVarError::UnknownCVar => write!(f, "Unknown CVar."),
47            CVarError::BadCVarType => write!(
48                f,
49                "CVar is not internally a Tuple Struct of the expected layout, did you try to register it manually?"
50            ),
51            CVarError::MissingCid => write!(f, "Missing ComponentID, was the resource registered?"),
52            CVarError::CannotDeserialize => {
53                write!(f, "Underlying CVar type cannot be deserialized.")
54            }
55            CVarError::FailedDeserialize(inner) => write!(f, "Failed to deserialize: {inner}"),
56            CVarError::FailedApply { inner } => {
57                write!(f, "Failed to apply value to CVar. ({inner:?})")
58            }
59            CVarError::AccessConflict => write!(
60                f,
61                "The requested operation conflicts with another ongoing operation on the world and cannot be performed."
62            ),
63            #[cfg(feature = "parse_cvars")]
64            CVarError::TomlError(toml_error) => write!(f, "TOML parsing error: {toml_error}"),
65            CVarError::MalformedConfigDuringWrite(info) => {
66                write!(f, "Malformed config during write: {info}")
67            }
68            #[cfg(feature = "parse_cvars")]
69            CVarError::TomlSerError(error) => write!(f, "TOML serializing error: {error}"),
70        }
71    }
72}
73
74impl From<ApplyError> for CVarError {
75    fn from(value: ApplyError) -> Self {
76        Self::FailedApply { inner: value }
77    }
78}
79
80#[cfg(feature = "parse_cvars")]
81impl From<TomlError> for CVarError {
82    fn from(value: TomlError) -> Self {
83        Self::TomlError(value)
84    }
85}
86
87#[cfg(feature = "parse_cvars")]
88impl From<toml_edit::ser::Error> for CVarError {
89    fn from(value: toml_edit::ser::Error) -> Self {
90        Self::TomlSerError(value)
91    }
92}
93
94impl From<ResourceFetchError> for CVarError {
95    fn from(value: ResourceFetchError) -> Self {
96        match value {
97            ResourceFetchError::NotRegistered => CVarError::UnknownCVar,
98            ResourceFetchError::DoesNotExist(_) => CVarError::UnknownCVar,
99            ResourceFetchError::NoResourceAccess(_) => CVarError::AccessConflict,
100        }
101    }
102}