1
0
dust/src/abstract_tree/command.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2024-01-25 13:43:21 +00:00
use std::process;
2024-01-25 13:27:24 +00:00
use serde::{Deserialize, Serialize};
2024-01-25 12:10:45 +00:00
2024-01-25 13:43:21 +00:00
use crate::{AbstractTree, Error, Format, Map, Result, Type, Value};
2024-01-25 13:27:24 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
2024-01-25 12:10:45 +00:00
pub struct Command {
2024-01-25 13:43:21 +00:00
command_texts: Vec<String>,
2024-01-25 12:10:45 +00:00
}
impl AbstractTree for Command {
2024-01-25 13:43:21 +00:00
fn from_syntax(node: tree_sitter::Node, source: &str, _context: &crate::Map) -> Result<Self> {
2024-01-25 13:27:24 +00:00
Error::expect_syntax_node(source, "command", node)?;
2024-01-25 13:43:21 +00:00
let mut command_texts = Vec::new();
for index in 1..node.child_count() {
let text_node = node.child(index).unwrap();
2024-01-25 13:57:55 +00:00
let mut text = source[text_node.byte_range()].to_string();
if (text.starts_with('\'') && text.ends_with('\''))
|| (text.starts_with('"') && text.ends_with('"'))
|| (text.starts_with('`') && text.ends_with('`'))
{
text = text[1..text.len() - 1].to_string();
}
2024-01-25 13:43:21 +00:00
command_texts.push(text);
}
Ok(Command { command_texts })
2024-01-25 12:10:45 +00:00
}
2024-01-25 13:43:21 +00:00
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
let output = process::Command::new(self.command_texts.first().unwrap())
.args(&self.command_texts[1..])
.spawn()?
.wait_with_output()?
.stdout;
let string = String::from_utf8(output)?;
Ok(Value::String(string))
2024-01-25 12:10:45 +00:00
}
2024-01-25 13:43:21 +00:00
fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(Type::String)
2024-01-25 12:10:45 +00:00
}
}
impl Format for Command {
2024-01-25 13:43:21 +00:00
fn format(&self, _output: &mut String, _indent_level: u8) {
2024-01-25 12:10:45 +00:00
todo!()
}
}