on master help sign in
Remove custom 'transpose' function
commit3334f74b729bde5696e161798c99781329215e23 authorsharkdp <davidpeter@web.de> dateSun 20 Oct 2019, 22:01 (7 years ago) parentb9ce3c2
4 files changed+16−45
src/bin/bat/app.rs+10−10 src/bin/bat/config.rs+6−7 src/lib.rs−1 src/util.rs−27
src/bin/bat/app.rs+10−10
@@ -23,7 +23,6 @@ use bat::{
2323 line_range::{LineRange, LineRanges},
2424 style::{OutputComponent, OutputComponents, OutputWrap},
2525 syntax_mapping::SyntaxMapping,
26− util::transpose,
2726 Config, PagingMode,
2827};
2928
@@ -198,12 +197,11 @@ impl App {
198197 .or_else(|| env::var("BAT_THEME").ok())
199198 .unwrap_or_else(|| String::from(BAT_THEME_DEFAULT)),
200199 line_ranges: LineRanges::from(
201− transpose(
202− self.matches
203− .values_of("line-range")
204− .map(|vs| vs.map(LineRange::from).collect()),
205− )?
206− .unwrap_or_else(|| vec![]),
200+ self.matches
201+ .values_of("line-range")
202+ .map(|vs| vs.map(LineRange::from).collect())
203+ .transpose()?
204+ .unwrap_or_else(|| vec![]),
207205 ),
208206 output_components,
209207 syntax_mapping,
@@ -247,13 +245,15 @@ impl App {
247245 } else if matches.is_present("plain") {
248246 [OutputComponent::Plain].iter().cloned().collect()
249247 } else {
250− let env_style_components: Option<Vec<OutputComponent>> =
251− transpose(env::var("BAT_STYLE").ok().map(|style_str| {
248+ let env_style_components: Option<Vec<OutputComponent>> = env::var("BAT_STYLE")
249+ .ok()
250+ .map(|style_str| {
252251 style_str
253252 .split(',')
254253 .map(|x| OutputComponent::from_str(&x))
255254 .collect::<Result<Vec<OutputComponent>>>()
256− }))?;
255+ })
256+ .transpose()?;
257257
258258 matches
259259 .value_of("style")
src/bin/bat/config.rs+6−7
@@ -5,7 +5,7 @@ use std::path::PathBuf;
55
66use shell_words;
77
8−use bat::{dirs::PROJECT_DIRS, util::transpose};
8+use bat::dirs::PROJECT_DIRS;
99
1010pub fn config_file() -> PathBuf {
1111 env::var("BAT_CONFIG_PATH")
@@ -16,12 +16,11 @@ pub fn config_file() -> PathBuf {
1616}
1717
1818pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
19− Ok(transpose(
20− fs::read_to_string(config_file())
21− .ok()
22− .map(|content| get_args_from_str(&content)),
23− )?
24− .unwrap_or_else(|| vec![]))
19+ Ok(fs::read_to_string(config_file())
20+ .ok()
21+ .map(|content| get_args_from_str(&content))
22+ .transpose()?
23+ .unwrap_or_else(|| vec![]))
2524}
2625
2726pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {
src/lib.rs−1
@@ -31,7 +31,6 @@ pub mod printer;
3131pub mod style;
3232pub mod syntax_mapping;
3333pub mod terminal;
34−pub mod util;
3534
3635pub mod errors {
3736 error_chain! {
src/util.rsdeleted−27
@@ -1,27 +0,0 @@
1−/// Helper function that might appear in Rust stable at some point
2−/// (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose)
3−pub fn transpose<T, E>(opt: Option<Result<T, E>>) -> Result<Option<T>, E> {
4− opt.map_or(Ok(None), |res| res.map(Some))
5−}
6−
7−#[cfg(test)]
8−mod tests {
9− use super::transpose;
10−
11− #[derive(Debug, PartialEq)]
12− struct TestError;
13−
14− type TestResult<T> = Result<T, TestError>;
15−
16− #[test]
17− fn basic() {
18− let a: Option<TestResult<i32>> = Some(Ok(2));
19− assert_eq!(Ok(Some(2)), transpose(a));
20−
21− let b: Option<TestResult<i32>> = Some(Err(TestError));
22− assert_eq!(Err(TestError), transpose(b));
23−
24− let c: Option<TestResult<i32>> = None;
25− assert_eq!(Ok(None), transpose(c));
26− }
27−}