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

View File

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