Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove thiserror from bevy_reflect #15766

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/bevy_reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ bevy_ptr = { path = "../bevy_ptr", version = "0.15.0-dev" }
erased-serde = "0.4"
disqualified = "1.0"
downcast-rs = "1.2"
thiserror = "1.0"
derive_more = { version = "1", default-features = false, features = [
"error",
"from",
"display",
] }
serde = "1"
smallvec = { version = "1.11", optional = true }
assert_type_match = "0.1.1"
Expand Down
15 changes: 2 additions & 13 deletions crates/bevy_reflect/src/enums/dynamic_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::{
};

use core::fmt::Formatter;
use derive_more::derive::From;

/// A dynamic representation of an enum variant.
#[derive(Debug, Default)]
#[derive(Debug, Default, From)]
pub enum DynamicVariant {
#[default]
Unit,
Expand All @@ -27,18 +28,6 @@ impl Clone for DynamicVariant {
}
}

impl From<DynamicTuple> for DynamicVariant {
fn from(dyn_tuple: DynamicTuple) -> Self {
Self::Tuple(dyn_tuple)
}
}

impl From<DynamicStruct> for DynamicVariant {
fn from(dyn_struct: DynamicStruct) -> Self {
Self::Struct(dyn_struct)
}
}

impl From<()> for DynamicVariant {
fn from(_: ()) -> Self {
Self::Unit
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/src/enums/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_utils::HashMap;
use core::slice::Iter;

use alloc::sync::Arc;
use thiserror::Error;
use derive_more::derive::{Display, Error};

/// Describes the form of an enum variant.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -40,12 +40,12 @@ pub enum VariantType {
}

/// A [`VariantInfo`]-specific error.
#[derive(Debug, Error)]
#[derive(Debug, Error, Display)]
pub enum VariantInfoError {
/// Caused when a variant was expected to be of a certain [type], but was not.
///
/// [type]: VariantType
#[error("variant type mismatch: expected {expected:?}, received {received:?}")]
#[display("variant type mismatch: expected {expected:?}, received {received:?}")]
TypeMismatch {
expected: VariantType,
received: VariantType,
Expand Down
12 changes: 7 additions & 5 deletions crates/bevy_reflect/src/func/args/error.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use alloc::borrow::Cow;

use thiserror::Error;
use derive_more::derive::{Display, Error};

use crate::func::args::Ownership;

/// An error that occurs when converting an [argument].
///
/// [argument]: crate::func::args::Arg
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error, Display, PartialEq)]
pub enum ArgError {
/// The argument is not the expected type.
#[error("expected `{expected}` but received `{received}` (@ argument index {index})")]
#[display("expected `{expected}` but received `{received}` (@ argument index {index})")]
UnexpectedType {
index: usize,
expected: Cow<'static, str>,
received: Cow<'static, str>,
},
/// The argument has the wrong ownership.
#[error("expected {expected} value but received {received} value (@ argument index {index})")]
#[display(
"expected {expected} value but received {received} value (@ argument index {index})"
)]
InvalidOwnership {
index: usize,
expected: Ownership,
Expand All @@ -26,6 +28,6 @@ pub enum ArgError {
/// Occurs when attempting to access an argument from an empty [`ArgList`].
///
/// [`ArgList`]: crate::func::args::ArgList
#[error("expected an argument but received none")]
#[display("expected an argument but received none")]
EmptyArgList,
}
16 changes: 8 additions & 8 deletions crates/bevy_reflect/src/func/error.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::func::{args::ArgError, Return};
use alloc::borrow::Cow;
use thiserror::Error;
use derive_more::derive::{Display, Error, From};

/// An error that occurs when calling a [`DynamicFunction`] or [`DynamicFunctionMut`].
///
/// [`DynamicFunction`]: crate::func::DynamicFunction
/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error, Display, PartialEq, From)]
pub enum FunctionError {
/// An error occurred while converting an argument.
#[error(transparent)]
ArgError(#[from] ArgError),
ArgError(ArgError),
/// The number of arguments provided does not match the expected number.
#[error("expected {expected} arguments but received {received}")]
#[display("expected {expected} arguments but received {received}")]
ArgCountMismatch { expected: usize, received: usize },
}

Expand All @@ -28,14 +27,15 @@ pub type FunctionResult<'a> = Result<Return<'a>, FunctionError>;
/// An error that occurs when registering a function into a [`FunctionRegistry`].
///
/// [`FunctionRegistry`]: crate::func::FunctionRegistry
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error, Display, PartialEq)]
pub enum FunctionRegistrationError {
/// A function with the given name has already been registered.
///
/// Contains the duplicate function name.
#[error("a function has already been registered with name {0:?}")]
#[display("a function has already been registered with name {_0:?}")]
#[error(ignore)]
DuplicateName(Cow<'static, str>),
/// The function is missing a name by which it can be registered.
#[error("function name is missing")]
#[display("function name is missing")]
MissingName,
}
15 changes: 2 additions & 13 deletions crates/bevy_reflect/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{Reflect, Type, TypePath};
use alloc::borrow::Cow;
use alloc::sync::Arc;
use core::ops::Deref;
use derive_more::derive::From;

/// The generic parameters of a type.
///
Expand Down Expand Up @@ -63,7 +64,7 @@ impl Deref for Generics {
}

/// An enum representing a generic parameter.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, From)]
pub enum GenericInfo {
/// A type parameter.
///
Expand Down Expand Up @@ -100,18 +101,6 @@ impl GenericInfo {
});
}

impl From<TypeParamInfo> for GenericInfo {
fn from(info: TypeParamInfo) -> Self {
Self::Type(info)
}
}

impl From<ConstParamInfo> for GenericInfo {
fn from(info: ConstParamInfo) -> Self {
Self::Const(info)
}
}

/// Type information for a generic type parameter.
///
/// An example of a type parameter would be `T` in `struct Foo<T>`.
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/src/kind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use thiserror::Error;
use derive_more::derive::{Display, Error};

#[cfg(feature = "functions")]
use crate::func::Function;
Expand Down Expand Up @@ -130,8 +130,8 @@ macro_rules! impl_reflect_kind_conversions {
/// Caused when a type was expected to be of a certain [kind], but was not.
///
/// [kind]: ReflectKind
#[derive(Debug, Error)]
#[error("kind mismatch: expected {expected:?}, received {received:?}")]
#[derive(Debug, Error, Display)]
#[display("kind mismatch: expected {expected:?}, received {received:?}")]
pub struct ReflectKindMismatchError {
pub expected: ReflectKind,
pub received: ReflectKind,
Expand Down
23 changes: 7 additions & 16 deletions crates/bevy_reflect/src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ use parse::PathParser;

use crate::{PartialReflect, Reflect};
use core::fmt;
use thiserror::Error;
use derive_more::derive::{Display, From};

type PathResult<'a, T> = Result<T, ReflectPathError<'a>>;

/// An error returned from a failed path string query.
#[derive(Debug, PartialEq, Eq, Error)]
#[derive(Debug, PartialEq, Eq, Display, From)]
pub enum ReflectPathError<'a> {
/// An error caused by trying to access a path that's not able to be accessed,
/// see [`AccessError`] for details.
#[error(transparent)]
InvalidAccess(AccessError<'a>),

/// An error that occurs when a type cannot downcast to a given type.
#[error("Can't downcast result of access to the given type")]
#[display("Can't downcast result of access to the given type")]
InvalidDowncast,

/// An error caused by an invalid path string that couldn't be parsed.
#[error("Encountered an error at offset {offset} while parsing `{path}`: {error}")]
#[display("Encountered an error at offset {offset} while parsing `{path}`: {error}")]
ParseError {
/// Position in `path`.
offset: usize,
Expand All @@ -37,11 +36,8 @@ pub enum ReflectPathError<'a> {
error: ParseError<'a>,
},
}
impl<'a> From<AccessError<'a>> for ReflectPathError<'a> {
fn from(value: AccessError<'a>) -> Self {
Self::InvalidAccess(value)
}
}

impl<'a> core::error::Error for ReflectPathError<'a> {}

/// Something that can be interpreted as a reflection path in [`GetPath`].
pub trait ReflectPath<'a>: Sized {
Expand Down Expand Up @@ -355,7 +351,7 @@ impl From<Access<'static>> for OffsetAccess {
/// ];
/// let my_path = ParsedPath::from(path_elements);
/// ```
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, From)]
pub struct ParsedPath(
/// This is a vector of pre-parsed [`OffsetAccess`]es.
pub Vec<OffsetAccess>,
Expand Down Expand Up @@ -447,11 +443,6 @@ impl<'a> ReflectPath<'a> for &'a ParsedPath {
Ok(root)
}
}
impl From<Vec<OffsetAccess>> for ParsedPath {
fn from(value: Vec<OffsetAccess>) -> Self {
ParsedPath(value)
}
}
impl<const N: usize> From<[OffsetAccess; N]> for ParsedPath {
fn from(value: [OffsetAccess; N]) -> Self {
ParsedPath(value.to_vec())
Expand Down
26 changes: 15 additions & 11 deletions crates/bevy_reflect/src/path/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@ use core::{
str::from_utf8_unchecked,
};

use thiserror::Error;
use derive_more::derive::{Display, Error, From};

use super::{Access, ReflectPathError};

/// An error that occurs when parsing reflect path strings.
#[derive(Debug, PartialEq, Eq, Error)]
#[error(transparent)]
#[derive(Debug, PartialEq, Eq, Error, Display)]
#[error(ignore)]
pub struct ParseError<'a>(Error<'a>);

/// A parse error for a path string.
#[derive(Debug, PartialEq, Eq, Error)]
#[derive(Debug, PartialEq, Eq, Error, Display, From)]
enum Error<'a> {
#[error("expected an identifier, but reached end of path string")]
#[display("expected an identifier, but reached end of path string")]
NoIdent,

#[error("expected an identifier, got '{0}' instead")]
#[display("expected an identifier, got '{_0}' instead")]
#[error(ignore)]
#[from(ignore)]
ExpectedIdent(Token<'a>),

#[error("failed to parse index as integer")]
InvalidIndex(#[from] ParseIntError),
#[display("failed to parse index as integer")]
InvalidIndex(ParseIntError),

#[error("a '[' wasn't closed, reached end of path string before finding a ']'")]
#[display("a '[' wasn't closed, reached end of path string before finding a ']'")]
Unclosed,

#[error("a '[' wasn't closed properly, got '{0}' instead")]
#[display("a '[' wasn't closed properly, got '{_0}' instead")]
#[error(ignore)]
#[from(ignore)]
BadClose(Token<'a>),

#[error("a ']' was found before an opening '['")]
#[display("a ']' was found before an opening '['")]
CloseBeforeOpen,
}

Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_reflect/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ use core::{
fmt::Debug,
};

use thiserror::Error;
use derive_more::derive::{Display, Error};

use crate::utility::NonGenericTypeInfoCell;

/// A enumeration of all error outcomes that might happen when running [`try_apply`](PartialReflect::try_apply).
#[derive(Error, Debug)]
#[derive(Error, Display, Debug)]
pub enum ApplyError {
#[error("attempted to apply `{from_kind}` to `{to_kind}`")]
#[display("attempted to apply `{from_kind}` to `{to_kind}`")]
/// Attempted to apply the wrong [kind](ReflectKind) to a type, e.g. a struct to a enum.
MismatchedKinds {
from_kind: ReflectKind,
to_kind: ReflectKind,
},

#[error("enum variant `{variant_name}` doesn't have a field named `{field_name}`")]
#[display("enum variant `{variant_name}` doesn't have a field named `{field_name}`")]
/// Enum variant that we tried to apply to was missing a field.
MissingEnumField {
variant_name: Box<str>,
field_name: Box<str>,
},

#[error("`{from_type}` is not `{to_type}`")]
#[display("`{from_type}` is not `{to_type}`")]
/// Tried to apply incompatible types.
MismatchedTypes {
from_type: Box<str>,
to_type: Box<str>,
},

#[error("attempted to apply type with {from_size} size to a type with {to_size} size")]
#[display("attempted to apply type with {from_size} size to a type with {to_size} size")]
/// Attempted to apply to types with mismatched sizez, e.g. a [u8; 4] to [u8; 3].
DifferentSize { from_size: usize, to_size: usize },

#[error("variant with name `{variant_name}` does not exist on enum `{enum_name}`")]
#[display("variant with name `{variant_name}` does not exist on enum `{enum_name}`")]
/// The enum we tried to apply to didn't contain a variant with the give name.
UnknownVariant {
enum_name: Box<str>,
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{
fmt::{Debug, Formatter},
hash::Hash,
};
use thiserror::Error;
use derive_more::derive::{Display, Error};

/// A static accessor to compile-time type information.
///
Expand Down Expand Up @@ -163,12 +163,12 @@ impl<T: Typed> DynamicTyped for T {
}

/// A [`TypeInfo`]-specific error.
#[derive(Debug, Error)]
#[derive(Debug, Error, Display)]
pub enum TypeInfoError {
/// Caused when a type was expected to be of a certain [kind], but was not.
///
/// [kind]: ReflectKind
#[error("kind mismatch: expected {expected:?}, received {received:?}")]
#[display("kind mismatch: expected {expected:?}, received {received:?}")]
KindMismatch {
expected: ReflectKind,
received: ReflectKind,
Expand Down
Loading