fd/src/exec/mod.rs

179 lines
5.7 KiB
Rust
Raw Normal View History

2017-10-21 10:16:03 +02:00
// Copyright (c) 2017 fd developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
2017-10-14 18:04:11 +02:00
// TODO: Possible optimization could avoid pushing characters on a buffer.
2017-10-14 22:42:47 +02:00
mod ticket;
mod token;
2017-10-14 18:04:11 +02:00
mod job;
2017-10-20 21:54:43 +02:00
mod input;
2017-10-14 18:04:11 +02:00
2017-11-03 01:39:03 +01:00
use std::borrow::Cow;
2017-10-14 18:04:11 +02:00
use std::path::Path;
2017-10-14 23:59:36 +02:00
use std::sync::{Arc, Mutex};
2017-10-14 18:04:11 +02:00
2017-11-03 01:39:03 +01:00
use regex::Regex;
use self::input::{basename, dirname, remove_extension};
2017-10-14 22:42:47 +02:00
use self::ticket::CommandTicket;
use self::token::Token;
2017-10-14 18:04:11 +02:00
pub use self::job::job;
2017-11-03 01:39:03 +01:00
/// Contains a collection of `TokenizedArgument`s that are utilized to generate command strings.
2017-10-14 18:04:11 +02:00
///
2017-11-03 01:39:03 +01:00
/// The arguments are a representation of the supplied command template, and are meant to be coupled
/// with an input in order to generate a command. The `generate()` method will be used to generate
/// a command and obtain a ticket for executing that command.
2017-10-14 18:04:11 +02:00
#[derive(Debug, Clone, PartialEq)]
pub struct TokenizedCommand {
2017-11-03 01:39:03 +01:00
args: Vec<TokenizedArgument>,
2017-10-14 18:04:11 +02:00
}
2017-11-03 01:39:03 +01:00
/// Represents a single command argument.
///
/// The argument is either a collection of `Token`s including at least one placeholder variant,
/// or a fixed text.
#[derive(Clone, Debug, PartialEq)]
enum TokenizedArgument {
Tokens(Vec<Token>),
Text(String),
}
impl TokenizedArgument {
pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {
use self::Token::*;
match *self {
TokenizedArgument::Tokens(ref tokens) => {
let mut s = String::new();
for token in tokens {
match *token {
Basename => s += basename(path),
BasenameNoExt => s += remove_extension(basename(path)),
NoExt => s += remove_extension(path),
Parent => s += dirname(path),
Placeholder => s += path,
Text(ref string) => s += string,
2017-10-14 18:04:11 +02:00
}
2017-10-14 20:04:04 +02:00
}
2017-11-03 01:39:03 +01:00
Cow::Owned(s)
}
TokenizedArgument::Text(ref text) => Cow::Borrowed(text),
}
}
}
impl TokenizedCommand {
pub fn new<I, S>(input: I) -> TokenizedCommand
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
lazy_static! {
static ref PLACEHOLDER: Regex = Regex::new(r"\{(/?\.?|//)\}").unwrap();
}
let mut args = Vec::new();
let mut has_placeholder = false;
for arg in input {
let arg = arg.as_ref();
let mut tokens = Vec::new();
let mut start = 0;
for placeholder in PLACEHOLDER.find_iter(arg) {
// Leading text before the placeholder.
if placeholder.start() > start {
tokens.push(Token::Text(arg[start..placeholder.start()].to_owned()));
2017-10-14 20:04:04 +02:00
}
2017-11-03 01:39:03 +01:00
start = placeholder.end();
match placeholder.as_str() {
"{}" => tokens.push(Token::Placeholder),
"{.}" => tokens.push(Token::NoExt),
"{/}" => tokens.push(Token::Basename),
"{//}" => tokens.push(Token::Parent),
"{/.}" => tokens.push(Token::BasenameNoExt),
_ => panic!("Unhandled placeholder"),
2017-10-14 20:04:04 +02:00
}
2017-11-03 01:39:03 +01:00
has_placeholder = true;
}
// Without a placeholder, the argument is just fixed text.
if tokens.is_empty() {
args.push(TokenizedArgument::Text(arg.to_owned()));
continue;
}
if start < arg.len() {
// Trailing text after last placeholder.
tokens.push(Token::Text(arg[start..].to_owned()));
2017-10-14 18:04:11 +02:00
}
2017-11-03 01:39:03 +01:00
args.push(TokenizedArgument::Tokens(tokens));
2017-10-14 20:04:04 +02:00
}
2017-10-14 18:04:11 +02:00
// If a placeholder token was not supplied, append one at the end of the command.
2017-11-03 01:39:03 +01:00
if !has_placeholder {
args.push(TokenizedArgument::Tokens(vec![Token::Placeholder]));
2017-10-14 18:04:11 +02:00
}
2017-11-03 01:39:03 +01:00
TokenizedCommand { args: args }
2017-10-14 18:04:11 +02:00
}
/// Generates a ticket that is required to execute the generated command.
///
2017-11-03 01:39:03 +01:00
/// Using the internal `args` field, and a supplied `input` variable, arguments will be
/// collected in a Vec. Once all arguments have been processed, the Vec will be wrapped
/// within a `CommandTicket`, which will be responsible for executing the command.
pub fn generate(&self, input: &Path, out_perm: Arc<Mutex<()>>) -> CommandTicket {
let input = input
.strip_prefix(".")
.unwrap_or(input)
.to_string_lossy()
.into_owned();
let mut args = Vec::with_capacity(self.args.len());
for arg in &self.args {
args.push(arg.generate(&input));
2017-10-14 18:04:11 +02:00
}
2017-11-03 01:39:03 +01:00
CommandTicket::new(args, out_perm)
2017-10-14 18:04:11 +02:00
}
}
#[cfg(test)]
mod tests {
2017-11-03 01:39:03 +01:00
use super::{TokenizedCommand, TokenizedArgument, Token};
2017-10-14 18:04:11 +02:00
#[test]
fn tokens() {
let expected = TokenizedCommand {
2017-11-03 01:39:03 +01:00
args: vec![
TokenizedArgument::Text("echo".into()),
TokenizedArgument::Text("${SHELL}:".into()),
TokenizedArgument::Tokens(vec![Token::Placeholder]),
],
2017-10-14 18:04:11 +02:00
};
2017-11-03 01:39:03 +01:00
assert_eq!(TokenizedCommand::new(&[&"echo", &"${SHELL}:"]), expected);
2017-10-14 20:04:04 +02:00
assert_eq!(
2017-11-03 01:39:03 +01:00
TokenizedCommand::new(&["echo", "{.}"]),
TokenizedCommand {
args: vec![
TokenizedArgument::Text("echo".into()),
TokenizedArgument::Tokens(vec![Token::NoExt]),
],
}
2017-10-14 20:04:04 +02:00
);
2017-10-14 18:04:11 +02:00
}
2017-10-14 20:04:04 +02:00
}