From 5e89acacb448e95bfd79b9dd9c3d05b7237fe9ae Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Thu, 10 Oct 2024 01:23:01 +1100 Subject: [PATCH] Remove `thiserror` from `bevy_input` (#15770) # Objective - Contributes to #15460 ## Solution - Removed `thiserror` from `bevy_input` --- crates/bevy_input/Cargo.toml | 6 ++- crates/bevy_input/src/gamepad.rs | 87 +++++++++----------------------- 2 files changed, 28 insertions(+), 65 deletions(-) diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index 9abf8db2999a3..3ce1e076d6f5b 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -36,7 +36,11 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", features = [ # other serde = { version = "1", features = ["derive"], optional = true } -thiserror = "1.0" +derive_more = { version = "1", default-features = false, features = [ + "error", + "from", + "display", +] } smol_str = "0.2" [lints] diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 1e508a1e8f67d..1f7c240c5b4ac 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -17,7 +17,7 @@ use bevy_utils::{ tracing::{info, warn}, Duration, HashMap, }; -use thiserror::Error; +use derive_more::derive::{Display, Error, From}; /// A gamepad event. /// @@ -26,7 +26,7 @@ use thiserror::Error; /// the in-frame relative ordering of events is important. /// /// This event is produced by `bevy_input` -#[derive(Event, Debug, Clone, PartialEq)] +#[derive(Event, Debug, Clone, PartialEq, From)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( @@ -42,24 +42,6 @@ pub enum GamepadEvent { Axis(GamepadAxisChangedEvent), } -impl From for GamepadEvent { - fn from(value: GamepadConnectionEvent) -> Self { - Self::Connection(value) - } -} - -impl From for GamepadEvent { - fn from(value: GamepadButtonChangedEvent) -> Self { - Self::Button(value) - } -} - -impl From for GamepadEvent { - fn from(value: GamepadAxisChangedEvent) -> Self { - Self::Axis(value) - } -} - /// A raw gamepad event. /// /// This event type is used over the [`GamepadConnectionEvent`], @@ -67,7 +49,7 @@ impl From for GamepadEvent { /// the in-frame relative ordering of events is important. /// /// This event type is used by `bevy_input` to feed its components. -#[derive(Event, Debug, Clone, PartialEq, Reflect)] +#[derive(Event, Debug, Clone, PartialEq, Reflect, From)] #[reflect(Debug, PartialEq)] #[cfg_attr( feature = "serialize", @@ -83,24 +65,6 @@ pub enum RawGamepadEvent { Axis(RawGamepadAxisChangedEvent), } -impl From for RawGamepadEvent { - fn from(value: GamepadConnectionEvent) -> Self { - Self::Connection(value) - } -} - -impl From for RawGamepadEvent { - fn from(value: RawGamepadButtonChangedEvent) -> Self { - Self::Button(value) - } -} - -impl From for RawGamepadEvent { - fn from(value: RawGamepadAxisChangedEvent) -> Self { - Self::Axis(value) - } -} - /// [`GamepadButton`] changed event unfiltered by [`GamepadSettings`] #[derive(Event, Debug, Copy, Clone, PartialEq, Reflect)] #[reflect(Debug, PartialEq)] @@ -281,22 +245,26 @@ impl GamepadAxisChangedEvent { } /// Errors that occur when setting axis settings for gamepad input. -#[derive(Error, Debug, PartialEq)] +#[derive(Error, Display, Debug, PartialEq)] pub enum AxisSettingsError { /// The given parameter `livezone_lowerbound` was not in range -1.0..=0.0. - #[error("invalid livezone_lowerbound {0}, expected value [-1.0..=0.0]")] + #[display("invalid livezone_lowerbound {_0}, expected value [-1.0..=0.0]")] + #[error(ignore)] LiveZoneLowerBoundOutOfRange(f32), /// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. - #[error("invalid deadzone_lowerbound {0}, expected value [-1.0..=0.0]")] + #[display("invalid deadzone_lowerbound {_0}, expected value [-1.0..=0.0]")] + #[error(ignore)] DeadZoneLowerBoundOutOfRange(f32), /// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. - #[error("invalid deadzone_upperbound {0}, expected value [0.0..=1.0]")] + #[display("invalid deadzone_upperbound {_0}, expected value [0.0..=1.0]")] + #[error(ignore)] DeadZoneUpperBoundOutOfRange(f32), /// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. - #[error("invalid livezone_upperbound {0}, expected value [0.0..=1.0]")] + #[display("invalid livezone_upperbound {_0}, expected value [0.0..=1.0]")] + #[error(ignore)] LiveZoneUpperBoundOutOfRange(f32), /// Parameter `livezone_lowerbound` was not less than or equal to parameter `deadzone_lowerbound`. - #[error("invalid parameter values livezone_lowerbound {} deadzone_lowerbound {}, expected livezone_lowerbound <= deadzone_lowerbound", .livezone_lowerbound, .deadzone_lowerbound)] + #[display("invalid parameter values livezone_lowerbound {} deadzone_lowerbound {}, expected livezone_lowerbound <= deadzone_lowerbound", livezone_lowerbound, deadzone_lowerbound)] LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { /// The value of the `livezone_lowerbound` parameter. livezone_lowerbound: f32, @@ -304,7 +272,7 @@ pub enum AxisSettingsError { deadzone_lowerbound: f32, }, /// Parameter `deadzone_upperbound` was not less than or equal to parameter `livezone_upperbound`. - #[error("invalid parameter values livezone_upperbound {} deadzone_upperbound {}, expected deadzone_upperbound <= livezone_upperbound", .livezone_upperbound, .deadzone_upperbound)] + #[display("invalid parameter values livezone_upperbound {} deadzone_upperbound {}, expected deadzone_upperbound <= livezone_upperbound", livezone_upperbound, deadzone_upperbound)] DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { /// The value of the `livezone_upperbound` parameter. livezone_upperbound: f32, @@ -312,21 +280,24 @@ pub enum AxisSettingsError { deadzone_upperbound: f32, }, /// The given parameter was not in range 0.0..=2.0. - #[error("invalid threshold {0}, expected 0.0 <= threshold <= 2.0")] + #[display("invalid threshold {_0}, expected 0.0 <= threshold <= 2.0")] + #[error(ignore)] Threshold(f32), } /// Errors that occur when setting button settings for gamepad input. -#[derive(Error, Debug, PartialEq)] +#[derive(Error, Display, Debug, PartialEq)] pub enum ButtonSettingsError { /// The given parameter was not in range 0.0..=1.0. - #[error("invalid release_threshold {0}, expected value [0.0..=1.0]")] + #[display("invalid release_threshold {_0}, expected value [0.0..=1.0]")] + #[error(ignore)] ReleaseThresholdOutOfRange(f32), /// The given parameter was not in range 0.0..=1.0. - #[error("invalid press_threshold {0}, expected [0.0..=1.0]")] + #[display("invalid press_threshold {_0}, expected [0.0..=1.0]")] + #[error(ignore)] PressThresholdOutOfRange(f32), /// Parameter `release_threshold` was not less than or equal to `press_threshold`. - #[error("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", .release_threshold, .press_threshold)] + #[display("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", release_threshold, press_threshold)] ReleaseThresholdGreaterThanPressThreshold { /// The value of the `press_threshold` parameter. press_threshold: f32, @@ -716,7 +687,7 @@ impl GamepadAxis { /// Encapsulation over [`GamepadAxis`] and [`GamepadButton`] // This is done so Gamepad can share a single Axis and simplifies the API by having only one get/get_unclamped method -#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, From)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))] pub enum GamepadInput { /// A [`GamepadAxis`] @@ -725,18 +696,6 @@ pub enum GamepadInput { Button(GamepadButton), } -impl From for GamepadInput { - fn from(value: GamepadAxis) -> Self { - GamepadInput::Axis(value) - } -} - -impl From for GamepadInput { - fn from(value: GamepadButton) -> Self { - GamepadInput::Button(value) - } -} - /// Gamepad settings component. /// /// ## Usage