refactor: rename ParseError -> Error

This commit is contained in:
graelo 2021-10-24 18:28:58 +02:00
parent 7d1bb69c6f
commit 5d9ea6f7f8
8 changed files with 21 additions and 19 deletions

View file

@ -1,7 +1,7 @@
use clap::{ArgEnum, Parser}; use clap::{ArgEnum, Parser};
use crate::{ use crate::{
error::ParseError, error::Error,
textbuf::{alphabet, regexes}, textbuf::{alphabet, regexes},
ui, Result, ui, Result,
}; };
@ -80,7 +80,7 @@ pub enum HintStyleArg {
/// Try to parse a `&str` into a tuple of `char`s. /// Try to parse a `&str` into a tuple of `char`s.
fn parse_chars(src: &str) -> Result<(char, char)> { fn parse_chars(src: &str) -> Result<(char, char)> {
if src.chars().count() != 2 { if src.chars().count() != 2 {
return Err(ParseError::ExpectedSurroundingPair); return Err(Error::ExpectedSurroundingPair);
} }
let chars: Vec<char> = src.chars().collect(); let chars: Vec<char> = src.chars().collect();

View file

@ -4,7 +4,7 @@ use std::fmt;
use clap::{ArgEnum, Parser}; use clap::{ArgEnum, Parser};
use super::basic; use super::basic;
use crate::{error::ParseError, textbuf::alphabet, tmux, ui, Result}; use crate::{error::Error, textbuf::alphabet, tmux, ui, Result};
/// Extended configuration for handling Tmux-specific configuration (options /// Extended configuration for handling Tmux-specific configuration (options
/// and outputs). This is only used by `tmux-copyrat` and parsed from command /// and outputs). This is only used by `tmux-copyrat` and parsed from command
@ -66,7 +66,7 @@ impl ConfigExt {
"@copyrat-capture-region" => { "@copyrat-capture-region" => {
let case_insensitive = true; let case_insensitive = true;
config_ext.capture_region = CaptureRegion::from_str(value, case_insensitive) config_ext.capture_region = CaptureRegion::from_str(value, case_insensitive)
.map_err(ParseError::ExpectedEnumVariant)? .map_err(Error::ExpectedEnumVariant)?
} }
"@copyrat-alphabet" => { "@copyrat-alphabet" => {
inner.alphabet = alphabet::parse_alphabet(value)?; inner.alphabet = alphabet::parse_alphabet(value)?;
@ -92,13 +92,13 @@ impl ConfigExt {
"@copyrat-hint-alignment" => { "@copyrat-hint-alignment" => {
let case_insensitive = true; let case_insensitive = true;
inner.hint_alignment = ui::HintAlignment::from_str(value, case_insensitive) inner.hint_alignment = ui::HintAlignment::from_str(value, case_insensitive)
.map_err(ParseError::ExpectedEnumVariant)? .map_err(Error::ExpectedEnumVariant)?
} }
"@copyrat-hint-style" => { "@copyrat-hint-style" => {
let case_insensitive = true; let case_insensitive = true;
inner.hint_style = Some( inner.hint_style = Some(
basic::HintStyleArg::from_str(value, case_insensitive) basic::HintStyleArg::from_str(value, case_insensitive)
.map_err(ParseError::ExpectedEnumVariant)?, .map_err(Error::ExpectedEnumVariant)?,
) )
} }

View file

@ -1,7 +1,7 @@
// use std::fmt; // use std::fmt;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum ParseError { pub enum Error {
#[error("Expected 2 chars")] #[error("Expected 2 chars")]
ExpectedSurroundingPair, ExpectedSurroundingPair,

View file

@ -4,7 +4,7 @@ pub mod textbuf;
pub mod tmux; pub mod tmux;
pub mod ui; pub mod ui;
pub type Result<T> = std::result::Result<T, error::ParseError>; pub type Result<T> = std::result::Result<T, error::Error>;
/// Run copyrat on an input string `buffer`, configured by `Opt`. /// Run copyrat on an input string `buffer`, configured by `Opt`.
/// ///

View file

@ -1,4 +1,4 @@
use crate::{error::ParseError, Result}; use crate::{error::Error, Result};
/// Catalog of available alphabets. /// Catalog of available alphabets.
/// ///
@ -49,7 +49,7 @@ pub fn parse_alphabet(src: &str) -> Result<Alphabet> {
let letters = letters.replace(&['n', 'N', 'y', 'Y'][..], ""); let letters = letters.replace(&['n', 'N', 'y', 'Y'][..], "");
Ok(Alphabet(letters)) Ok(Alphabet(letters))
} }
None => Err(ParseError::UnknownAlphabet), None => Err(Error::UnknownAlphabet),
} }
} }

View file

@ -2,7 +2,7 @@
//! //!
//! All patterns must have one capture group. The first group is used. //! All patterns must have one capture group. The first group is used.
use crate::{error::ParseError, Result}; use crate::{error::Error, Result};
pub(super) const EXCLUDE_PATTERNS: [(&str, &str); 1] = pub(super) const EXCLUDE_PATTERNS: [(&str, &str); 1] =
[("ansi_colors", r"[[:cntrl:]]\[([0-9]{1,2};)?([0-9]{1,2})?m")]; [("ansi_colors", r"[[:cntrl:]]\[([0-9]{1,2};)?([0-9]{1,2})?m")];
@ -54,6 +54,6 @@ pub struct NamedPattern(pub String, pub String);
pub(crate) fn parse_pattern_name(src: &str) -> Result<NamedPattern> { pub(crate) fn parse_pattern_name(src: &str) -> Result<NamedPattern> {
match PATTERNS.iter().find(|&(name, _pattern)| name == &src) { match PATTERNS.iter().find(|&(name, _pattern)| name == &src) {
Some((name, pattern)) => Ok(NamedPattern(name.to_string(), pattern.to_string())), Some((name, pattern)) => Ok(NamedPattern(name.to_string(), pattern.to_string())),
None => Err(ParseError::UnknownPatternName), None => Err(Error::UnknownPatternName),
} }
} }

View file

@ -10,8 +10,10 @@ use std::str::FromStr;
use regex::Regex; use regex::Regex;
use crate::config::extended::CaptureRegion; use crate::config::extended::CaptureRegion;
use crate::{error::ParseError, Result}; use crate::{error::Error, Result};
/// Represents a simplified Tmux Pane, only holding the properties needed in
/// this crate.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Pane { pub struct Pane {
/// Pane identifier, e.g. `%37`. /// Pane identifier, e.g. `%37`.
@ -31,7 +33,7 @@ pub struct Pane {
} }
impl FromStr for Pane { impl FromStr for Pane {
type Err = ParseError; type Err = Error;
/// Parse a string containing tmux panes status into a new `Pane`. /// Parse a string containing tmux panes status into a new `Pane`.
/// ///
@ -130,13 +132,13 @@ impl Pane {
pub struct PaneId(String); pub struct PaneId(String);
impl FromStr for PaneId { impl FromStr for PaneId {
type Err = ParseError; type Err = Error;
/// Parse into PaneId. The `&str` must be start with '%' /// Parse into PaneId. The `&str` must be start with '%'
/// followed by a `u16`. /// followed by a `u16`.
fn from_str(src: &str) -> std::result::Result<Self, Self::Err> { fn from_str(src: &str) -> std::result::Result<Self, Self::Err> {
if !src.starts_with('%') { if !src.starts_with('%') {
return Err(ParseError::ExpectedPaneIdMarker); return Err(Error::ExpectedPaneIdMarker);
} }
let id = src[1..].parse::<u16>()?; let id = src[1..].parse::<u16>()?;
let id = format!("%{}", id); let id = format!("%{}", id);
@ -205,7 +207,7 @@ pub fn get_options(prefix: &str) -> Result<HashMap<String, String>> {
Ok(args) Ok(args)
} }
/// Ask tmux to swap the current Pane with the target_pane (uses Tmux format). /// Asks tmux to swap the current Pane with the target_pane (uses Tmux format).
pub fn swap_pane_with(target_pane: &str) -> Result<()> { pub fn swap_pane_with(target_pane: &str) -> Result<()> {
// -Z: keep the window zoomed if it was zoomed. // -Z: keep the window zoomed if it was zoomed.
duct::cmd!("tmux", "swap-pane", "-Z", "-s", target_pane).run()?; duct::cmd!("tmux", "swap-pane", "-Z", "-s", target_pane).run()?;

View file

@ -1,7 +1,7 @@
use clap::Parser; use clap::Parser;
use termion::color; use termion::color;
use crate::{error::ParseError, Result}; use crate::{error::Error, Result};
pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>> { pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>> {
match src { match src {
@ -22,7 +22,7 @@ pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>> {
"bright-cyan" | "brightcyan" => Ok(Box::new(color::LightCyan)), "bright-cyan" | "brightcyan" => Ok(Box::new(color::LightCyan)),
"bright-white" | "brightwhite" => Ok(Box::new(color::LightWhite)), "bright-white" | "brightwhite" => Ok(Box::new(color::LightWhite)),
"none" => Ok(Box::new(color::Reset)), "none" => Ok(Box::new(color::Reset)),
_ => Err(ParseError::UnknownColor), _ => Err(Error::UnknownColor),
} }
} }