Skip to content
Myr

mod.rs

This file

Path

src/exec/mod.rs

178 lines · 5.7 KiB · Rust

Last changed by

Remove shell with --execMatthias Reitingerbefore adoption18709b1

History of this file

Take it

Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2

lines 1–8why, one step back
1// Copyright (c) 2017 fd developers
2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.8

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 9–12why, one step back
9// TODO: Possible optimization could avoid pushing characters on a buffer.
10mod ticket;11mod token;12mod job;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 13why, one step back
13mod input;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 14why, one step back
14

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 15why, one step back
15use std::borrow::Cow;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 16–18why, one step back
16use std::path::Path;
17use std::sync::{Arc, Mutex};18

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 19–21why, one step back
19use regex::Regex;
2021use self::input::{basename, dirname, remove_extension};

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 22–25why, one step back
22use self::ticket::CommandTicket;
23use self::token::Token;24pub use self::job::job;25

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 26why, one step back
26/// Contains a collection of `TokenizedArgument`s that are utilized to generate command strings.

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 27why, one step back
27///

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 28–30why, one step back
28/// The arguments are a representation of the supplied command template, and are meant to be coupled
29/// with an input in order to generate a command. The `generate()` method will be used to generate30/// a command and obtain a ticket for executing that command.

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 31–32why, one step back
31#[derive(Debug, Clone, PartialEq)]
32pub struct TokenizedCommand {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 33why, one step back
33 args: Vec<TokenizedArgument>,

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 34–35why, one step back
34}
35

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 36–44why, one step back
36/// Represents a single command argument.
37///38/// The argument is either a collection of `Token`s including at least one placeholder variant,39/// or a fixed text.40#[derive(Clone, Debug, PartialEq)]41enum TokenizedArgument {42 Tokens(Vec<Token>),43 Text(String),44}

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 45why, one step back
45

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 46–60why, one step back
46impl TokenizedArgument {
47 pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {48 use self::Token::*;4950 match *self {51 TokenizedArgument::Tokens(ref tokens) => {52 let mut s = String::new();53 for token in tokens {54 match *token {55 Basename => s += basename(path),56 BasenameNoExt => s += remove_extension(basename(path)),57 NoExt => s += remove_extension(path),58 Parent => s += dirname(path),59 Placeholder => s += path,60 Text(ref string) => s += string,

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 61–62why, one step back
61 }
62 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 63–92why, one step back
63 Cow::Owned(s)
64 }65 TokenizedArgument::Text(ref text) => Cow::Borrowed(text),66 }67 }68}6970impl TokenizedCommand {71 pub fn new<I, S>(input: I) -> TokenizedCommand72 where73 I: IntoIterator<Item = S>,74 S: AsRef<str>,75 {76 lazy_static! {77 static ref PLACEHOLDER: Regex = Regex::new(r"\{(/?\.?|//)\}").unwrap();78 }7980 let mut args = Vec::new();81 let mut has_placeholder = false;8283 for arg in input {84 let arg = arg.as_ref();8586 let mut tokens = Vec::new();87 let mut start = 0;8889 for placeholder in PLACEHOLDER.find_iter(arg) {90 // Leading text before the placeholder.91 if placeholder.start() > start {92 tokens.push(Token::Text(arg[start..placeholder.start()].to_owned()));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 93why, one step back
93 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 94–103why, one step back
94
95 start = placeholder.end();9697 match placeholder.as_str() {98 "{}" => tokens.push(Token::Placeholder),99 "{.}" => tokens.push(Token::NoExt),100 "{/}" => tokens.push(Token::Basename),101 "{//}" => tokens.push(Token::Parent),102 "{/.}" => tokens.push(Token::BasenameNoExt),103 _ => panic!("Unhandled placeholder"),

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 104why, one step back
104 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 105–106why, one step back
105
106 has_placeholder = true;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 107–108why, one step back
107 }
108

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 109–120why, one step back
109 // Without a placeholder, the argument is just fixed text.
110 if tokens.is_empty() {111 args.push(TokenizedArgument::Text(arg.to_owned()));112 continue;113 }114115 if start < arg.len() {116 // Trailing text after last placeholder.117 tokens.push(Token::Text(arg[start..].to_owned()));118 }119120 args.push(TokenizedArgument::Tokens(tokens));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 121–123why, one step back
121 }
122123 // If a placeholder token was not supplied, append one at the end of the command.

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 124–125why, one step back
124 if !has_placeholder {
125 args.push(TokenizedArgument::Tokens(vec![Token::Placeholder]));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 126–127why, one step back
126 }
127

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 128why, one step back
128 TokenizedCommand { args: args }

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 129–132why, one step back
129 }
130131 /// Generates a ticket that is required to execute the generated command.132 ///

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 133–145why, one step back
133 /// Using the internal `args` field, and a supplied `input` variable, arguments will be
134 /// collected in a Vec. Once all arguments have been processed, the Vec will be wrapped135 /// within a `CommandTicket`, which will be responsible for executing the command.136 pub fn generate(&self, input: &Path, out_perm: Arc<Mutex<()>>) -> CommandTicket {137 let input = input138 .strip_prefix(".")139 .unwrap_or(input)140 .to_string_lossy()141 .into_owned();142143 let mut args = Vec::with_capacity(self.args.len());144 for arg in &self.args {145 args.push(arg.generate(&input));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 146–147why, one step back
146 }
147

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 148why, one step back
148 CommandTicket::new(args, out_perm)

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 149–153why, one step back
149 }
150}151152#[cfg(test)]153mod tests {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 154why, one step back
154 use super::{TokenizedCommand, TokenizedArgument, Token};

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 155–158why, one step back
155
156 #[test]157 fn tokens() {158 let expected = TokenizedCommand {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 159–163why, one step back
159 args: vec![
160 TokenizedArgument::Text("echo".into()),161 TokenizedArgument::Text("${SHELL}:".into()),162 TokenizedArgument::Tokens(vec![Token::Placeholder]),163 ],

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 164–165why, one step back
164 };
165

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 166–167why, one step back
166 assert_eq!(TokenizedCommand::new(&[&"echo", &"${SHELL}:"]), expected);
167

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 168why, one step back
168 assert_eq!(

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 169–175why, one step back
169 TokenizedCommand::new(&["echo", "{.}"]),
170 TokenizedCommand {171 args: vec![172 TokenizedArgument::Text("echo".into()),173 TokenizedArgument::Tokens(vec![Token::NoExt]),174 ],175 }

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 176–178why, one step back
176 );
177 }178}