Move transpose to util module

This commit is contained in:
sharkdp 2018-10-11 22:27:42 +02:00 committed by David Peter
parent 9e7da05459
commit 5af176c94f
3 changed files with 29 additions and 6 deletions

View File

@ -20,6 +20,7 @@ use errors::*;
use inputfile::InputFile;
use line_range::LineRange;
use style::{OutputComponent, OutputComponents, OutputWrap};
use util::transpose;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PagingMode {
@ -74,12 +75,6 @@ fn is_truecolor_terminal() -> bool {
.unwrap_or(false)
}
/// Helper function that might appear in Rust stable at some point
/// (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose)
fn transpose<T>(opt: Option<Result<T>>) -> Result<Option<T>> {
opt.map_or(Ok(None), |res| res.map(Some))
}
pub struct App {
pub matches: ArgMatches<'static>,
interactive_output: bool,

View File

@ -36,6 +36,7 @@ mod preprocessor;
mod printer;
mod style;
mod terminal;
mod util;
use std::collections::HashSet;
use std::io;

27
src/util.rs Normal file
View File

@ -0,0 +1,27 @@
/// 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));
}
}