Add sh function; Improve type check error output
This commit is contained in:
parent
5beda4695d
commit
3b7e75c41c
@ -47,9 +47,7 @@ impl AbstractTree for Identifier {
|
|||||||
} else {
|
} else {
|
||||||
for built_in_function in BUILT_IN_FUNCTIONS {
|
for built_in_function in BUILT_IN_FUNCTIONS {
|
||||||
if self.0 == built_in_function.name() {
|
if self.0 == built_in_function.name() {
|
||||||
if let Type::Function { return_type, .. } = built_in_function.r#type() {
|
return Ok(built_in_function.r#type());
|
||||||
return Ok(*return_type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
src/built_in_functions/commands.rs
Normal file
33
src/built_in_functions/commands.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
|
||||||
|
|
||||||
|
pub struct Sh;
|
||||||
|
|
||||||
|
impl BuiltInFunction for Sh {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"sh"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
|
||||||
|
Error::expect_argument_amount(self, 1, arguments.len())?;
|
||||||
|
|
||||||
|
let command_text = arguments.first().unwrap().as_string()?;
|
||||||
|
let mut command = Command::new("sh");
|
||||||
|
|
||||||
|
for word in command_text.split(' ') {
|
||||||
|
command.arg(word);
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = command.spawn()?.wait_with_output()?.stdout;
|
||||||
|
|
||||||
|
Ok(Value::String(String::from_utf8(output)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn r#type(&self) -> crate::Type {
|
||||||
|
Type::Function {
|
||||||
|
parameter_types: vec![Type::String],
|
||||||
|
return_type: Box::new(Type::String),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ use crate::{Map, Result, Type, Value};
|
|||||||
|
|
||||||
mod assert;
|
mod assert;
|
||||||
mod collections;
|
mod collections;
|
||||||
|
mod commands;
|
||||||
mod data_formats;
|
mod data_formats;
|
||||||
mod fs;
|
mod fs;
|
||||||
mod network;
|
mod network;
|
||||||
@ -14,10 +15,11 @@ mod r#type;
|
|||||||
///
|
///
|
||||||
/// This is the public interface to access built-in functions by iterating over
|
/// This is the public interface to access built-in functions by iterating over
|
||||||
/// the references it holds.
|
/// the references it holds.
|
||||||
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 15] = [
|
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 16] = [
|
||||||
&assert::Assert,
|
&assert::Assert,
|
||||||
&assert::AssertEqual,
|
&assert::AssertEqual,
|
||||||
&collections::Length,
|
&collections::Length,
|
||||||
|
&commands::Sh,
|
||||||
&data_formats::FromJson,
|
&data_formats::FromJson,
|
||||||
&data_formats::ToJson,
|
&data_formats::ToJson,
|
||||||
&fs::Read,
|
&fs::Read,
|
||||||
|
Loading…
Reference in New Issue
Block a user