mirror of
https://github.com/sharkdp/bat.git
synced 2024-12-22 14:30:42 +01:00
Move transpose to util module
This commit is contained in:
parent
9e7da05459
commit
5af176c94f
@ -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,
|
||||
|
@ -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
27
src/util.rs
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user