use crate::{
error::{EvalResult, InterpreterError},
value::Value,
};
pub fn has_function(name: &str) -> bool {
matches!(name, "auto")
}
pub fn call(func: &str, args: &[Value]) -> EvalResult {
match func {
"auto" => {
Ok(Value::Int(
args.first()
.and_then(|v| match v {
Value::Int(i) => Some(*i),
_ => None,
})
.unwrap_or(1),
))
}
_ => Err(InterpreterError::AttributeError(format!(
"module 'enum' has no attribute '{func}'"
))
.into()),
}
}
pub fn constant(name: &str) -> Option<Value> {
match name {
"Enum" | "IntEnum" | "StrEnum" | "Flag" | "IntFlag" => {
Some(Value::Type(format!("enum.{name}")))
}
_ => None,
}
}
pub struct EnumModule;
#[async_trait::async_trait]
impl crate::eval::modules::Module for EnumModule {
fn name(&self) -> &'static str {
"enum"
}
fn constant(&self, name: &str) -> Option<Value> {
constant(name)
}
fn has_function(&self, name: &str) -> bool {
has_function(name)
}
async fn call(
&self,
_state: &mut crate::state::InterpreterState,
func: &str,
args: &[Value],
_kwargs: &indexmap::IndexMap<String, Value>,
_tools: &crate::tools::Tools,
) -> EvalResult {
call(func, args)
}
}