use endbasic_core::*;
use std::borrow::Cow;
use std::rc::Rc;
pub(super) struct ConcatFunction {
metadata: Rc<CallableMetadata>,
}
impl ConcatFunction {
pub(super) fn new() -> Rc<Self> {
Rc::from(Self {
metadata: CallableMetadataBuilder::new("CONCAT")
.with_return_type(ExprType::Text)
.with_syntax(&[(
&[],
Some(&RepeatedSyntax {
name: Cow::Borrowed("arg"),
type_syn: RepeatedTypeSyntax::AnyValue,
sep: ArgSepSyntax::Exactly(ArgSep::Long),
require_one: false,
allow_missing: true,
}),
)])
.test_build(),
})
}
}
impl Callable for ConcatFunction {
fn metadata(&self) -> Rc<CallableMetadata> {
self.metadata.clone()
}
fn exec(&self, scope: Scope<'_>) -> CallResult<()> {
let mut result = String::new();
let mut reg = 0;
loop {
let sep = match scope.get_type(reg) {
VarArgTag::Immediate(sep, etype) => {
reg += 1;
match etype {
ExprType::Text => result.push_str(scope.get_string(reg)),
_ => {
return Err(CallError::Argument(
"Only accepts string values".to_owned(),
));
}
}
sep
}
_ => return Err(CallError::Argument("Only accepts string values".to_owned())),
};
reg += 1;
if sep == ArgSep::End {
break;
}
}
scope.return_string(result)
}
}