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#[derive(Debug)]
10#[non_exhaustive]
11pub enum CVarError {
12 UnknownCVar,
14 BadCVarType,
16 MissingCid,
18 CannotDeserialize,
20 FailedDeserialize(String),
22 FailedApply {
24 inner: ApplyError,
26 },
27 AccessConflict,
29 #[cfg(feature = "parse_cvars")]
30 TomlError(TomlError),
32
33 MalformedConfigDuringWrite(&'static str),
35
36 #[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}