refactoring builtins
parent
71f148139e
commit
621f64d3f1
@ -1,29 +1,36 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::ExecError,
|
error::ExecError,
|
||||||
runtime::{State, Value},
|
run::{Call, Context, Value},
|
||||||
};
|
};
|
||||||
|
use std::{boxed::Box, collections::HashMap, io::Write};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub enum Builtin {
|
pub enum Builtin {
|
||||||
Echo,
|
Echo,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builtin {
|
impl Call for Builtin {
|
||||||
pub fn lookup(name: &str) -> Option<Self> {
|
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||||
match name {
|
match self {
|
||||||
"echo" => Some(Self::Echo),
|
Builtin::Echo => Echo.call(ctx, args),
|
||||||
_ => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn call(&self, _: &mut State, args: &Vec<Value>) -> Result<Value, ExecError> {
|
pub struct Echo;
|
||||||
match self {
|
|
||||||
Builtin::Echo => {
|
impl Call for Echo {
|
||||||
let args: Result<Vec<String>, ExecError> =
|
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||||
args.into_iter().map(|arg| arg.try_as_string()).collect();
|
let args = args
|
||||||
let args = args?;
|
.into_iter()
|
||||||
println!("{}", args.join(" "));
|
.map(|v| v.try_as_str())
|
||||||
Ok(Value::None)
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
}
|
_ = write!(ctx.stdout, "{}\n", args.join(" "));
|
||||||
}
|
Ok(Value::None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn all() -> HashMap<&'static str, Builtin> {
|
||||||
|
use Builtin::*;
|
||||||
|
HashMap::from([("echo", Echo)])
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue