Remove custom 'transpose' function

This commit is contained in:
sharkdp 2019-10-20 22:01:20 +02:00 committed by David Peter
parent b9ce3c248c
commit 3334f74b72
4 changed files with 16 additions and 45 deletions

View File

@ -23,7 +23,6 @@ use bat::{
line_range::{LineRange, LineRanges},
style::{OutputComponent, OutputComponents, OutputWrap},
syntax_mapping::SyntaxMapping,
util::transpose,
Config, PagingMode,
};
@ -198,12 +197,11 @@ impl App {
.or_else(|| env::var("BAT_THEME").ok())
.unwrap_or_else(|| String::from(BAT_THEME_DEFAULT)),
line_ranges: LineRanges::from(
transpose(
self.matches
.values_of("line-range")
.map(|vs| vs.map(LineRange::from).collect()),
)?
.unwrap_or_else(|| vec![]),
self.matches
.values_of("line-range")
.map(|vs| vs.map(LineRange::from).collect())
.transpose()?
.unwrap_or_else(|| vec![]),
),
output_components,
syntax_mapping,
@ -247,13 +245,15 @@ impl App {
} else if matches.is_present("plain") {
[OutputComponent::Plain].iter().cloned().collect()
} else {
let env_style_components: Option<Vec<OutputComponent>> =
transpose(env::var("BAT_STYLE").ok().map(|style_str| {
let env_style_components: Option<Vec<OutputComponent>> = env::var("BAT_STYLE")
.ok()
.map(|style_str| {
style_str
.split(',')
.map(|x| OutputComponent::from_str(&x))
.collect::<Result<Vec<OutputComponent>>>()
}))?;
})
.transpose()?;
matches
.value_of("style")

View File

@ -5,7 +5,7 @@ use std::path::PathBuf;
use shell_words;
use bat::{dirs::PROJECT_DIRS, util::transpose};
use bat::dirs::PROJECT_DIRS;
pub fn config_file() -> PathBuf {
env::var("BAT_CONFIG_PATH")
@ -16,12 +16,11 @@ pub fn config_file() -> PathBuf {
}
pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
Ok(transpose(
fs::read_to_string(config_file())
.ok()
.map(|content| get_args_from_str(&content)),
)?
.unwrap_or_else(|| vec![]))
Ok(fs::read_to_string(config_file())
.ok()
.map(|content| get_args_from_str(&content))
.transpose()?
.unwrap_or_else(|| vec![]))
}
pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {

View File

@ -31,7 +31,6 @@ pub mod printer;
pub mod style;
pub mod syntax_mapping;
pub mod terminal;
pub mod util;
pub mod errors {
error_chain! {

View File

@ -1,27 +0,0 @@
/// Helper function that might appear in Rust stable at some point
/// (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose)
pub fn transpose<T, E>(opt: Option<Result<T, E>>) -> Result<Option<T>, E> {
opt.map_or(Ok(None), |res| res.map(Some))
}
#[cfg(test)]
mod tests {
use super::transpose;
#[derive(Debug, PartialEq)]
struct TestError;
type TestResult<T> = Result<T, TestError>;
#[test]
fn basic() {
let a: Option<TestResult<i32>> = Some(Ok(2));
assert_eq!(Ok(Some(2)), transpose(a));
let b: Option<TestResult<i32>> = Some(Err(TestError));
assert_eq!(Err(TestError), transpose(b));
let c: Option<TestResult<i32>> = None;
assert_eq!(Ok(None), transpose(c));
}
}