refactor: use custom Result

This commit is contained in:
graelo 2021-10-24 11:57:25 +02:00
parent 077d16311e
commit 7d1bb69c6f
8 changed files with 34 additions and 33 deletions

View file

@ -3,13 +3,14 @@
//! The main use cases are running Tmux commands & parsing Tmux panes
//! information.
use regex::Regex;
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use regex::Regex;
use crate::config::extended::CaptureRegion;
use crate::error::ParseError;
use crate::{error::ParseError, Result};
#[derive(Debug, PartialEq)]
pub struct Pane {
@ -44,7 +45,7 @@ impl FromStr for Pane {
///
/// For definitions, look at `Pane` type,
/// and at the tmux man page for definitions.
fn from_str(src: &str) -> Result<Self, Self::Err> {
fn from_str(src: &str) -> std::result::Result<Self, Self::Err> {
let items: Vec<&str> = src.split(':').collect();
assert_eq!(items.len(), 5, "tmux should have returned 5 items per line");
@ -98,7 +99,7 @@ impl Pane {
/// scrolled up by 3 lines. It is necessarily in copy mode. Its start line
/// index is `-3`. The index of the last line is `(40-1) - 3 = 36`.
///
pub fn capture(&self, region: &CaptureRegion) -> Result<String, ParseError> {
pub fn capture(&self, region: &CaptureRegion) -> Result<String> {
let mut args_str = format!("capture-pane -t {pane_id} -J -p", pane_id = self.id);
let region_str = match region {
@ -133,7 +134,7 @@ impl FromStr for PaneId {
/// Parse into PaneId. The `&str` must be start with '%'
/// followed by a `u16`.
fn from_str(src: &str) -> Result<Self, Self::Err> {
fn from_str(src: &str) -> std::result::Result<Self, Self::Err> {
if !src.starts_with('%') {
return Err(ParseError::ExpectedPaneIdMarker);
}
@ -156,7 +157,7 @@ impl fmt::Display for PaneId {
}
/// Returns a list of `Pane` from the current tmux session.
pub fn available_panes() -> Result<Vec<Pane>, ParseError> {
pub fn available_panes() -> Result<Vec<Pane>> {
let args = vec![
"list-panes",
"-F",
@ -165,9 +166,9 @@ pub fn available_panes() -> Result<Vec<Pane>, ParseError> {
let output = duct::cmd("tmux", &args).read()?;
// Each call to `Pane::parse` returns a `Result<Pane, _>`. All results
// are collected into a Result<Vec<Pane>, _>, thanks to `collect()`.
let result: Result<Vec<Pane>, ParseError> = output
// Each call to `Pane::parse` returns a `Result<Pane>`. All results
// are collected into a Result<Vec<Pane>>, thanks to `collect()`.
let result: Result<Vec<Pane>> = output
.trim_end() // trim last '\n' as it would create an empty line
.split('\n')
.map(|line| Pane::from_str(line))
@ -182,7 +183,7 @@ pub fn available_panes() -> Result<Vec<Pane>, ParseError> {
///
/// # Example
/// ```get_options("@copyrat-")```
pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError> {
pub fn get_options(prefix: &str) -> Result<HashMap<String, String>> {
let output = duct::cmd!("tmux", "show-options", "-g").read()?;
let lines: Vec<&str> = output.split('\n').collect();
@ -205,7 +206,7 @@ pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError>
}
/// Ask tmux to swap the current Pane with the target_pane (uses Tmux format).
pub fn swap_pane_with(target_pane: &str) -> Result<(), ParseError> {
pub fn swap_pane_with(target_pane: &str) -> Result<()> {
// -Z: keep the window zoomed if it was zoomed.
duct::cmd!("tmux", "swap-pane", "-Z", "-s", target_pane).run()?;
@ -214,16 +215,13 @@ pub fn swap_pane_with(target_pane: &str) -> Result<(), ParseError> {
#[cfg(test)]
mod tests {
use super::Pane;
use super::PaneId;
use crate::error;
use super::*;
use std::str::FromStr;
#[test]
fn test_parse_pass() {
let output = vec!["%52:false:62:3:false", "%53:false:23::true"];
let panes: Result<Vec<Pane>, error::ParseError> =
output.iter().map(|&line| Pane::from_str(line)).collect();
let panes: Result<Vec<Pane>> = output.iter().map(|&line| Pane::from_str(line)).collect();
let panes = panes.expect("Could not parse tmux panes");
let expected = vec![