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 inputfile::InputFile;
use line_range::LineRange; use line_range::LineRange;
use style::{OutputComponent, OutputComponents, OutputWrap}; use style::{OutputComponent, OutputComponents, OutputWrap};
use util::transpose;
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum PagingMode { pub enum PagingMode {
@ -74,12 +75,6 @@ fn is_truecolor_terminal() -> bool {
.unwrap_or(false) .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 struct App {
pub matches: ArgMatches<'static>, pub matches: ArgMatches<'static>,
interactive_output: bool, interactive_output: bool,

View file

@ -36,6 +36,7 @@ mod preprocessor;
mod printer; mod printer;
mod style; mod style;
mod terminal; mod terminal;
mod util;
use std::collections::HashSet; use std::collections::HashSet;
use std::io; 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));
}
}