input.rs
This file
Path
src/exec/input.rs
232 lines · 5.8 KiB · Rust
Last changed by
Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
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–47why, 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 }3839 if index != 0 {40 self.data = &self.data[index + 1..]41 }4243 self44 }4546 /// Removes the extension from the path47 pub fn remove_extension(&'a mut self) -> &'a mut Self {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 48why, one step back48 let mut has_dir = false;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 49–53why, one step back49 let mut dir_index = 0;50 let mut ext_index = 0;5152 for (id, character) in self.data.char_indices() {53 if character == MAIN_SEPARATOR {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 54why, one step back54 has_dir = true;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 55–62why, one step back55 dir_index = id;56 }57 if character == '.' {58 ext_index = id;59 }60 }6162 // Account for hidden files and directoriesFix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 63why, one step back63 if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 64–71why, one step back64 self.data = &self.data[0..ext_index];65 }6667 self68 }6970 /// Removes the basename from the path.71 pub fn dirname(&'a mut self) -> &'a mut Self {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 72why, one step back72 let mut has_dir = false;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 73–75why, one step back73 let mut index = 0;74 for (id, character) in self.data.char_indices() {75 if character == MAIN_SEPARATOR {Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 76why, one step back76 has_dir = true;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 77–80why, one step back77 index = id;78 }79 }80Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 81why, one step back81 self.data = if !has_dir {Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 82why, one step back82 "."Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 83–84why, one step back83 } else if index == 0 {84 &self.data[..1]Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 85–92why, one step back85 } else {86 &self.data[0..index]87 };8889 self90 }9192 pub fn get(&'a self) -> Cow<'a, str> {Fix --execJ.Wbefore adoption6b232a8
lines 93–94why, one step back93 escape(Cow::Borrowed(self.data))94 }Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 95why, one step back95Fix --execJ.Wbefore adoption6b232a8
lines 96–98why, one step back96 #[cfg(test)]97 fn get_private(&'a self) -> Cow<'a, str> {98 Cow::Borrowed(self.data)Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 99–113why, one step back99 }100}101102#[cfg(test)]103mod tests {104 use super::{MAIN_SEPARATOR, Input};105106 fn correct(input: &str) -> String {107 let mut sep = String::new();108 sep.push(MAIN_SEPARATOR);109 input.replace('/', &sep)110 }111112 #[test]113 fn path_remove_ext_simple() {Fix --execJ.Wbefore adoption6b232a8
lines 114–117why, one step back114 assert_eq!(115 &Input::new("foo.txt").remove_extension().get_private(),116 "foo"117 );Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 118–122why, one step back118 }119120 #[test]121 fn path_remove_ext_dir() {122 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 123–125why, one step back123 &Input::new(&correct("dir/foo.txt"))124 .remove_extension()125 .get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 126–131why, one step back126 &correct("dir/foo")127 );128 }129130 #[test]131 fn path_hidden() {Fix --execJ.Wbefore adoption6b232a8
line 132why, one step back132 assert_eq!(&Input::new(".foo").remove_extension().get_private(), ".foo")Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 133–136why, one step back133 }134135 #[test]136 fn path_remove_ext_utf8() {Fix --execJ.Wbefore adoption6b232a8
lines 137–140why, one step back137 assert_eq!(138 &Input::new("💖.txt").remove_extension().get_private(),139 "💖"140 );Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 141–144why, one step back141 }142143 #[test]144 fn path_remove_ext_empty() {Fix --execJ.Wbefore adoption6b232a8
line 145why, one step back145 assert_eq!(&Input::new("").remove_extension().get_private(), "");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 146–149why, one step back146 }147148 #[test]149 fn path_basename_simple() {Fix --execJ.Wbefore adoption6b232a8
line 150why, one step back150 assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 151–153why, one step back151 }152153 #[test]Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 154–164why, one step back154 fn path_basename_no_ext() {155 assert_eq!(156 &Input::new("foo.txt")157 .basename()158 .remove_extension()159 .get_private(),160 "foo"161 );162 }163164 #[test]Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 165–166why, one step back165 fn path_basename_dir() {166 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
line 167why, one step back167 &Input::new(&correct("dir/foo.txt")).basename().get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 168–173why, one step back168 "foo.txt"169 );170 }171172 #[test]173 fn path_basename_empty() {Fix --execJ.Wbefore adoption6b232a8
line 174why, one step back174 assert_eq!(&Input::new("").basename().get_private(), "");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 175–179why, one step back175 }176177 #[test]178 fn path_basename_utf8() {179 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 180–182why, one step back180 &Input::new(&correct("💖/foo.txt"))181 .basename()182 .get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 183–185why, one step back183 "foo.txt"184 );185 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 186–188why, one step back186 &Input::new(&correct("dir/💖.txt"))187 .basename()188 .get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 189–194why, one step back189 "💖.txt"190 );191 }192193 #[test]194 fn path_dirname_simple() {Fix --execJ.Wbefore adoption6b232a8
line 195why, one step back195 assert_eq!(&Input::new("foo.txt").dirname().get_private(), ".");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 196–199why, one step back196 }197198 #[test]199 fn path_dirname_dir() {Fix --execJ.Wbefore adoption6b232a8
lines 200–203why, one step back200 assert_eq!(201 &Input::new(&correct("dir/foo.txt")).dirname().get_private(),202 "dir"203 );Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 204–208why, one step back204 }205206 #[test]207 fn path_dirname_utf8() {208 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
line 209why, one step back209 &Input::new(&correct("💖/foo.txt")).dirname().get_private(),Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 210–212why, one step back210 "💖"211 );212 assert_eq!(Fix --execJ.Wbefore adoption6b232a8
lines 213–214why, one step back213 &Input::new(&correct("dir/💖.txt")).dirname().get_private(),214 "dir"Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 215–218why, one step back215 );216 }217218 #[test]Fix --execJ.Wbefore adoption6b232a8
lines 219–220why, one step back219 fn path_dirname_empty() {220 assert_eq!(&Input::new("").dirname().get_private(), ".");Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 221–230why, one step back221 }222223 #[test]224 fn path_dirname_root() {225 #[cfg(windows)]226 assert_eq!(&Input::new("C:\\").dirname().get_private(), "C:");227 #[cfg(windows)]228 assert_eq!(&Input::new("\\").dirname().get_private(), "\\");229 #[cfg(not(windows))]230 assert_eq!(&Input::new("/").dirname().get_private(), "/");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 231–232why, one step back231 }232}