use std::rc::Rc;
use crate::error::{DogeError, DogeResult};
use crate::objects::method_arity_error;
use crate::value::Value;
mod dict;
mod list;
#[cfg(test)]
mod tests;
use dict::dict_method;
use list::list_method;
pub fn builtin_method(recv: &Value, name: &str, args: Vec<Value>) -> DogeResult {
match recv {
Value::List(_) => list_method(recv, name, args),
Value::Dict(_) => dict_method(recv, name, args),
Value::Object(_) => Err(crate::objects::no_such_method(recv, name)),
Value::Int(_)
| Value::Float(_)
| Value::Str(_)
| Value::Bool(_)
| Value::None
| Value::Function(_)
| Value::Class(_)
| Value::BoundMethod(_)
| Value::Error(_)
| Value::Socket(_)
| Value::Pup(_)
| Value::Bowl(_) => Err(crate::objects::no_methods_error(recv)),
}
}
pub fn has_builtin_method(recv: &Value, name: &str) -> bool {
match recv {
Value::List(_) => list::LIST_METHODS.contains(&name),
Value::Dict(_) => dict::DICT_METHODS.contains(&name),
_ => false,
}
}
pub(super) fn check_arity(
class: &str,
method: &str,
expected: usize,
got: usize,
) -> DogeResult<()> {
if got == expected {
Ok(())
} else {
Err(method_arity_error(
class,
method,
expected,
Some(expected),
got,
))
}
}
pub(super) fn expect_int(value: Value, what: &str) -> DogeResult<i64> {
match value {
Value::Int(n) => Ok(n),
other => Err(DogeError::type_error(format!(
"{what}, got {}",
other.describe()
))),
}
}
pub(super) fn expect_str(value: Value, what: &str) -> DogeResult<Rc<str>> {
match value {
Value::Str(s) => Ok(s),
other => Err(DogeError::type_error(format!(
"{what}, got {}",
other.describe()
))),
}
}