use thiserror::Error;
use crate::values::StarlarkValue;
use crate::values::Value;
#[derive(Debug, Error)]
#[allow(missing_docs)] pub enum ValueError {
#[error("Operation `{op}` not supported on type `{typ}`")]
OperationNotSupported { op: String, typ: String },
#[error("Operation `{op}` not supported for types `{left}` and `{right}`")]
OperationNotSupportedBinary {
op: String,
left: String,
right: String,
},
#[error("Cannot divide by zero")]
DivisionByZero,
#[error("Integer overflow")]
IntegerOverflow,
#[error("Negative shift count")]
NegativeShiftCount,
#[error("Type of parameters mismatch, expected `{0}`, actual `{1}`")]
IncorrectParameterTypeWithExpected(String, String),
#[error("Type of parameter `{0}` doesn't match, expected `{1}`, actual `{2}`")]
IncorrectParameterTypeNamedWithExpected(String, String, String),
#[error("Type of parameters mismatch")]
IncorrectParameterType,
#[error("Type of parameter `{0}` doesn't match")]
IncorrectParameterTypeNamed(String),
#[error("Missing this parameter")]
MissingThis,
#[error("Missing required parameter `{0}`")]
MissingRequired(String),
#[error("Index `{0}` is out of bound")]
IndexOutOfBound(i32),
#[error("Key `{0}` was not found")]
KeyNotFound(String),
#[error("Immutable")]
CannotMutateImmutableValue,
#[error("This operation mutate an iterable for an iterator while iterating.")]
MutationDuringIteration,
#[error("Object of type `{0}` has no attribute `{1}`")]
NoAttr(String, String),
#[error("Object of type `{0}` has no attribute `{1}`, did you mean `{2}`?")]
NoAttrDidYouMean(String, String, String),
}
impl From<ValueError> for crate::Error {
fn from(e: ValueError) -> Self {
crate::Error::new(crate::ErrorKind::Value(anyhow::Error::new(e)))
}
}
#[derive(Debug, Error)]
pub(crate) enum ControlError {
#[error("Value of type `{0}` is not hashable")]
NotHashableValue(String),
#[error("Too many recursion levels")]
TooManyRecursionLevel,
}
impl ValueError {
#[cold]
pub(crate) fn unsupported_owned<T>(
left: &str,
op: &str,
right: Option<&str>,
) -> crate::Result<T> {
match right {
None => Err(ValueError::OperationNotSupported {
op: op.to_owned(),
typ: left.to_owned(),
}
.into()),
Some(right) => Err(ValueError::OperationNotSupportedBinary {
op: op.to_owned(),
left: left.to_owned(),
right: right.to_owned(),
}
.into()),
}
}
#[cold]
pub fn unsupported<'v, T, V: StarlarkValue<'v> + ?Sized>(
_left: &V,
op: &str,
) -> crate::Result<T> {
Self::unsupported_owned(V::TYPE, op, None)
}
#[cold]
pub(crate) fn unsupported_type<T>(left: Value, op: &str) -> crate::Result<T> {
Self::unsupported_owned(left.get_type(), op, None)
}
#[cold]
pub fn unsupported_with<'v, T, V: StarlarkValue<'v> + ?Sized>(
_left: &V,
op: &str,
right: Value,
) -> crate::Result<T> {
Self::unsupported_owned(V::TYPE, op, Some(right.get_type()))
}
}