bevy_reflect/func/error.rs
1use crate::func::signature::ArgumentSignature;
2use crate::func::{
3 args::{ArgCount, ArgError},
4 Return,
5};
6use alloc::borrow::Cow;
7use bevy_platform::collections::HashSet;
8use thiserror::Error;
9
10/// An error that occurs when calling a [`DynamicFunction`] or [`DynamicFunctionMut`].
11///
12/// [`DynamicFunction`]: crate::func::DynamicFunction
13/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut
14#[derive(Debug, Error, PartialEq)]
15pub enum FunctionError {
16 /// An error occurred while converting an argument.
17 #[error(transparent)]
18 ArgError(#[from] ArgError),
19 /// The number of arguments provided does not match the expected number.
20 #[error("received {received} arguments but expected one of {expected:?}")]
21 ArgCountMismatch {
22 /// Expected argument count. [`ArgCount`] for overloaded functions will contain multiple possible counts.
23 expected: ArgCount,
24 /// Number of arguments received.
25 received: usize,
26 },
27 /// No overload was found for the given set of arguments.
28 #[error("no overload found for arguments with signature `{received:?}`, expected one of `{expected:?}`")]
29 NoOverload {
30 /// The set of available argument signatures.
31 expected: HashSet<ArgumentSignature>,
32 /// The received argument signature.
33 received: ArgumentSignature,
34 },
35}
36
37/// The result of calling a [`DynamicFunction`] or [`DynamicFunctionMut`].
38///
39/// Returns `Ok(value)` if the function was called successfully,
40/// where `value` is the [`Return`] value of the function.
41///
42/// [`DynamicFunction`]: crate::func::DynamicFunction
43/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut
44pub type FunctionResult<'a> = Result<Return<'a>, FunctionError>;
45
46/// An error that occurs when attempting to add a function overload.
47#[derive(Debug, Error, PartialEq)]
48pub enum FunctionOverloadError {
49 /// A [`SignatureInfo`] was expected, but none was found.
50 ///
51 /// [`SignatureInfo`]: crate::func::info::SignatureInfo
52 #[error("expected at least one `SignatureInfo` but found none")]
53 MissingSignature,
54 /// An error that occurs when attempting to add a function overload with a duplicate signature.
55 #[error("could not add function overload: duplicate found for signature `{0:?}`")]
56 DuplicateSignature(ArgumentSignature),
57 /// An attempt was made to add an overload with more than [`ArgCount::MAX_COUNT`] arguments.
58 ///
59 /// [`ArgCount::MAX_COUNT`]: crate::func::args::ArgCount
60 #[error(
61 "argument signature `{:?}` has too many arguments (max {})",
62 0,
63 ArgCount::MAX_COUNT
64 )]
65 TooManyArguments(ArgumentSignature),
66}
67
68/// An error that occurs when registering a function into a [`FunctionRegistry`].
69///
70/// [`FunctionRegistry`]: crate::func::FunctionRegistry
71#[derive(Debug, Error, PartialEq)]
72pub enum FunctionRegistrationError {
73 /// A function with the given name has already been registered.
74 ///
75 /// Contains the duplicate function name.
76 #[error("a function has already been registered with name {0:?}")]
77 DuplicateName(Cow<'static, str>),
78 /// The function is missing a name by which it can be registered.
79 #[error("function name is missing")]
80 MissingName,
81}