use endbasic_core::*;
use std::borrow::Cow;
use std::io;
use std::rc::Rc;
pub(super) struct RaisefFunction {
metadata: Rc<CallableMetadata>,
}
impl RaisefFunction {
pub(super) fn new() -> Rc<Self> {
Rc::from(Self {
metadata: CallableMetadataBuilder::new("RAISEF")
.with_return_type(ExprType::Boolean)
.with_syntax(&[
(
&[SingularArgSyntax::RequiredValue(
RequiredValueSyntax {
name: Cow::Borrowed("arg"),
vtype: ExprType::Text,
},
ArgSepSyntax::End,
)],
None,
),
(
&[
SingularArgSyntax::RequiredValue(
RequiredValueSyntax {
name: Cow::Borrowed("arg"),
vtype: ExprType::Text,
},
ArgSepSyntax::Exactly(ArgSep::Long),
),
SingularArgSyntax::RequiredValue(
RequiredValueSyntax {
name: Cow::Borrowed("n"),
vtype: ExprType::Integer,
},
ArgSepSyntax::End,
),
],
None,
),
])
.test_build(),
})
}
}
impl Callable for RaisefFunction {
fn metadata(&self) -> Rc<CallableMetadata> {
self.metadata.clone()
}
fn exec(&self, scope: Scope<'_>) -> CallResult<()> {
let arg = scope.get_string(0);
match arg {
"argument" => Err(CallError::Argument("Bad argument".to_owned())),
"eval" => Err(CallError::Eval("Some eval error".to_owned())),
"internal" => Err(CallError::Eval("Some internal error".to_owned())),
"io" => Err(CallError::from(io::Error::other("Some I/O error"))),
"syntax" => Err(CallError::Syntax(scope.get_pos(0), "Some syntax error".to_owned())),
"syntax0" => Err(CallError::Syntax(scope.get_pos(0), "Some syntax error".to_owned())),
"syntax1" => Err(CallError::Syntax(scope.get_pos(1), "Some syntax error".to_owned())),
_ => Err(CallError::Argument("Invalid arguments".to_owned())),
}
}
}