input.rs
This file
Path
src/exec/input.rs
234 lines · 5.9 KiB · Rust
Last changed by
Add TODOs and FIXMEs about WindowsJ.Wbefore adoption614f576
History of this fileTake it
Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2
lines 1–8why, one step back1// Copyright (c) 2017 fd developers2// 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.8Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 9–11why, one step back9use std::path::MAIN_SEPARATOR;10use std::borrow::Cow;11Fix --execJ.Wbefore adoption6b232a8
line 12why, one step back12use shell_escape::escape;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 13–38why, one step back1314/// A builder for efficiently generating input strings.15///16/// After choosing your required specs, the `get()` method will escape special characters found17/// in the input. Allocations will only occur if special characters are found that need to be18/// escaped.19pub struct Input<'a> {20 data: &'a str,21}2223impl<'a> Input<'a> {24 /// Creates a new `Input` structure, which provides access to command-building25 /// primitives, such as `basename()` and `dirname()`.26 pub fn new(data: &'a str) -> Input<'a> {27 Input { data }28 }2930 /// Removes the parent component of the path31 pub fn basename(&'a mut self) -> &'a mut Self {32 let mut index = 0;33 for (id, character) in self.data.char_indices() {34 if character == MAIN_SEPARATOR {35 index = id;36 }37 }38Add TODOs and FIXMEs about WindowsJ.Wbefore adoption614f576
line 39why, one step back39 // FIXME: On Windows, should return what for C:file.txt D:file.txt and \\server\share ?Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 40–48why, one step back40 if index != 0 {41 self.data = &self.data[index + 1..]42 }4344 self45 }4647 /// Removes the extension from the path48 pub fn remove_extension(&'a mut self) -> &'a mut Self {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 49why, one step back49 let mut has_dir = false;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 50–54why, one step back50 let mut dir_index = 0;51 let mut ext_index = 0;5253 for (id, character) in self.data.char_indices() {54 if character == MAIN_SEPARATOR {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 55why, one step back55 has_dir = true;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 56–63why, one step back56 dir_index = id;57 }58 if character == '.' {59 ext_index = id;60 }61 }6263 // Account for hidden files and directoriesFix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 64why, one step back64 if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 65–72why, one step back65 self.data = &self.data[0..ext_index];66 }6768 self69 }7071 /// Removes the basename from the path.72 pub fn dirname(&'a mut self) -> &'a mut Self {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 73why, one step back73 let mut has_dir = false;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 74–76why, one step back74 let mut index = 0;75 for (id, character) in self.data.char_indices() {76 if character == MAIN_SEPARATOR {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 77why, one step back77 has_dir = true;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 78–81why, one step back78 index = id;79 }80 }81Add TODOs and FIXMEs about WindowsJ.Wbefore adoption614f576
line 82why, one step back82 // FIXME: On Windows, return what for C:file.txt D:file.txt and \\server\share ?Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 83why, one step back83 self.data = if !has_dir {Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 84why, one step back84 "."Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 85–86why, one step back85 } else if index == 0 {86 &self.data[..1]Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 87–94why, one step back87 } else {88 &self.data[0..index]89 };9091 self92 }9394 pub fn get(&'a self) -> Cow<'a, str> {Fix --execJ.Wbefore adoption6b232a8
lines 95–96why, one step back95 escape(Cow::Borrowed(self.data))96 }Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 97why, one step back97Fix --execJ.Wbefore adoption6b232a8
lines 98–100why, one step back98 #[cfg(test)]99 fn get_private(&'a self) -> Cow<'a, str> {100 Cow::Borrowed(self.data)Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 101–115why, one step back101 }102}103104#[cfg(test)]105mod tests {106 use super::{MAIN_SEPARATOR, Input};107108 fn correct(input: &str) -> String {109 let mut sep = String::new();110 sep.push(MAIN_SEPARATOR);111 input.replace('/', &sep)112 }113114 #[test]115 fn path_remove_ext_simple() {Fix --execJ.Wbefore adoption6b232a8
lines 116–119why, one step back116 assert_eq!(117 &Input::new("foo.txt").remove_extension().get_private(),118 "foo"119 );Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 120–124why, one step back120 }121122 #[test]123 fn path_remove_ext_dir() {124 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 125–127why, one step back125 &Input::new(&correct("dir/foo.txt"))126 .remove_extension()127 .get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 128–133why, one step back128 &correct("dir/foo")129 );130 }131132 #[test]133 fn path_hidden() {Fix --execJ.Wbefore adoption6b232a8
line 134why, one step back134 assert_eq!(&Input::new(".foo").remove_extension().get_private(), ".foo")Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 135–138why, one step back135 }136137 #[test]138 fn path_remove_ext_utf8() {Fix --execJ.Wbefore adoption6b232a8
lines 139–142why, one step back139 assert_eq!(140 &Input::new("💖.txt").remove_extension().get_private(),141 "💖"142 );Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 143–146why, one step back143 }144145 #[test]146 fn path_remove_ext_empty() {Fix --execJ.Wbefore adoption6b232a8
line 147why, one step back147 assert_eq!(&Input::new("").remove_extension().get_private(), "");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 148–151why, one step back148 }149150 #[test]151 fn path_basename_simple() {Fix --execJ.Wbefore adoption6b232a8
line 152why, one step back152 assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 153–155why, one step back153 }154155 #[test]Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 156–166why, one step back156 fn path_basename_no_ext() {157 assert_eq!(158 &Input::new("foo.txt")159 .basename()160 .remove_extension()161 .get_private(),162 "foo"163 );164 }165166 #[test]Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 167–168why, one step back167 fn path_basename_dir() {168 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
line 169why, one step back169 &Input::new(&correct("dir/foo.txt")).basename().get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 170–175why, one step back170 "foo.txt"171 );172 }173174 #[test]175 fn path_basename_empty() {Fix --execJ.Wbefore adoption6b232a8
line 176why, one step back176 assert_eq!(&Input::new("").basename().get_private(), "");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 177–181why, one step back177 }178179 #[test]180 fn path_basename_utf8() {181 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 182–184why, one step back182 &Input::new(&correct("💖/foo.txt"))183 .basename()184 .get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 185–187why, one step back185 "foo.txt"186 );187 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 188–190why, one step back188 &Input::new(&correct("dir/💖.txt"))189 .basename()190 .get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 191–196why, one step back191 "💖.txt"192 );193 }194195 #[test]196 fn path_dirname_simple() {Fix --execJ.Wbefore adoption6b232a8
line 197why, one step back197 assert_eq!(&Input::new("foo.txt").dirname().get_private(), ".");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 198–201why, one step back198 }199200 #[test]201 fn path_dirname_dir() {Fix --execJ.Wbefore adoption6b232a8
lines 202–205why, one step back202 assert_eq!(203 &Input::new(&correct("dir/foo.txt")).dirname().get_private(),204 "dir"205 );Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 206–210why, one step back206 }207208 #[test]209 fn path_dirname_utf8() {210 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
line 211why, one step back211 &Input::new(&correct("💖/foo.txt")).dirname().get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 212–214why, one step back212 "💖"213 );214 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 215–216why, one step back215 &Input::new(&correct("dir/💖.txt")).dirname().get_private(),216 "dir"Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 217–220why, one step back217 );218 }219220 #[test]Fix --execJ.Wbefore adoption6b232a8
lines 221–222why, one step back221 fn path_dirname_empty() {222 assert_eq!(&Input::new("").dirname().get_private(), ".");Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 223–232why, one step back223 }224225 #[test]226 fn path_dirname_root() {227 #[cfg(windows)]228 assert_eq!(&Input::new("C:\\").dirname().get_private(), "C:");229 #[cfg(windows)]230 assert_eq!(&Input::new("\\").dirname().get_private(), "\\");231 #[cfg(not(windows))]232 assert_eq!(&Input::new("/").dirname().get_private(), "/");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 233–234why, one step back233 }234}