Browse Source

feat: add preprocessing function

main
fdai7451 2 years ago
parent
commit
80cd94232a
  1. 27
      Cargo.lock
  2. 1
      Cargo.toml
  3. 5
      src/execute.rs
  4. 1
      src/main.rs
  5. 21
      src/preprocess.rs

27
Cargo.lock

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -243,6 +252,7 @@ dependencies = [
"dirs", "dirs",
"gethostname", "gethostname",
"once_cell", "once_cell",
"regex",
"rustyline", "rustyline",
"shlex", "shlex",
] ]
@ -295,6 +305,23 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "regex"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.36.6" version = "0.36.6"

1
Cargo.toml

@ -9,5 +9,6 @@ ctrlc = "3.2"
dirs = "4.0" dirs = "4.0"
gethostname = "0.4" gethostname = "0.4"
once_cell = "1.17" once_cell = "1.17"
regex = "1.7"
rustyline = "10.1.0" rustyline = "10.1.0"
shlex = "1.1" shlex = "1.1"

5
src/execute.rs

@ -6,12 +6,15 @@ use std::{io, thread};
use crate::builtins::{execute_builtin, is_builtin, BuiltinConfig}; use crate::builtins::{execute_builtin, is_builtin, BuiltinConfig};
use crate::error::ShellError; use crate::error::ShellError;
use crate::parse::parse_line; use crate::parse::parse_line;
use crate::preprocess::preprocess;
pub fn interpret( pub fn interpret(
line: String,
mut line: String,
config: &mut BuiltinConfig, config: &mut BuiltinConfig,
ctrlc_recv: Receiver<()>, ctrlc_recv: Receiver<()>,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
line = preprocess(line)?;
if line.is_empty() { if line.is_empty() {
return Err(ShellError::EmptyLine); return Err(ShellError::EmptyLine);
} }

1
src/main.rs

@ -2,6 +2,7 @@ mod builtins;
mod error; mod error;
mod execute; mod execute;
mod parse; mod parse;
mod preprocess;
mod prompt; mod prompt;
use crate::builtins::BuiltinConfig; use crate::builtins::BuiltinConfig;

21
src/preprocess.rs

@ -0,0 +1,21 @@
use crate::error::ShellError;
use once_cell::unsync::Lazy;
use regex::Regex;
use std::env;
const ENV_SET: Lazy<Regex> = Lazy::new(|| Regex::new(r#"(?P<key>\w+)=(?P<value>\w*)"#).unwrap());
pub fn preprocess(line: String) -> Result<String, ShellError> {
if let Some(capture) = ENV_SET.captures(&line) {
let Some(key) = capture.name("key") else {
return Err(ShellError::MalformedArgs("cannot find key to set env variable".to_string()))
};
let value = capture
.name("value")
.map_or("".to_string(), |v| v.as_str().to_string());
env::set_var(key.as_str(), value)
}
Ok(line)
}
Loading…
Cancel
Save