diff options
author | Thomas Otto <th1000s@posteo.net> | 2021-10-02 14:51:04 +0200 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-10-18 10:41:12 -0400 |
commit | 3a03d4b5eede9a6c342a61941dd78c38c3559824 (patch) | |
tree | 9d9fb9d2a087b16f673decf22bf441e881b73a89 /src/format.rs | |
parent | cd50d301844991da65eaf613f753296217fb4cda (diff) |
Convert Align and Placeholder into enums
Also move number and style logic out of `format_and_paint_line_numbers()`
and into a separate `linenumbers_and_styles()` function.
Diffstat (limited to 'src/format.rs')
-rw-r--r-- | src/format.rs | 66 |
1 files changed, 56 insertions, 10 deletions
diff --git a/src/format.rs b/src/format.rs index 0b77e699..57a39790 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,13 +1,57 @@ +use std::convert::{TryFrom, TryInto}; + use regex::Regex; use smol_str::SmolStr; use unicode_segmentation::UnicodeSegmentation; +#[derive(Debug, PartialEq)] +pub enum Placeholder<'a> { + NumberMinus, + NumberPlus, + Str(&'a str), +} + +impl<'a> TryFrom<Option<&'a str>> for Placeholder<'a> { + type Error = (); + fn try_from(from: Option<&'a str>) -> Result<Self, Self::Error> { + match from { + Some(placeholder) if placeholder == "nm" => Ok(Placeholder::NumberMinus), + Some(placeholder) if placeholder == "np" => Ok(Placeholder::NumberPlus), + Some(placeholder) => Ok(Placeholder::Str(placeholder)), + _ => Err(()), + } + } +} + +#[derive(Debug, PartialEq)] +pub enum Align { + Left, + Center, + Right, +} + +impl TryFrom<Option<&str>> for Align { + type Error = (); + fn try_from(from: Option<&str>) -> Result<Self, Self::Error> { + match from { + Some(alignment) if alignment == "<" => Ok(Align::Left), + Some(alignment) if alignment == ">" => Ok(Align::Right), + Some(alignment) if alignment == "^" => Ok(Align::Center), + Some(alignment) => { + debug_assert!(false, "Unknown Alignment: {}", alignment); + Err(()) + } + None => Err(()), + } + } +} + #[derive(Debug, Default, PartialEq)] pub struct FormatStringPlaceholderData<'a> { pub prefix: SmolStr, pub prefix_len: usize, - pub placeholder: Option<&'a str>, - pub alignment_spec: Option<&'a str>, + pub placeholder: Option<Placeholder<'a>>, + pub alignment_spec: Option<Align>, pub width: Option<usize>, pub suffix: SmolStr, pub suffix_len: usize, @@ -20,7 +64,9 @@ impl<'a> FormatStringPlaceholderData<'a> { ( self.prefix_len + std::cmp::max( - self.placeholder.map_or(0, |_| hunk_max_line_number_width), + self.placeholder + .as_ref() + .map_or(0, |_| hunk_max_line_number_width), self.width.unwrap_or(0), ), self.suffix_len, @@ -66,8 +112,8 @@ pub fn parse_line_number_format<'a>( format_data.push(FormatStringPlaceholderData { prefix, prefix_len, - placeholder: captures.get(1).map(|m| m.as_str()), - alignment_spec: captures.get(3).map(|m| m.as_str()), + placeholder: captures.get(1).map(|m| m.as_str()).try_into().ok(), + alignment_spec: captures.get(3).map(|m| m.as_str()).try_into().ok(), width: captures.get(4).map(|m| { m.as_str() .parse() @@ -90,14 +136,14 @@ pub fn parse_line_number_format<'a>( suffix_len: format_string.graphemes(true).count(), }) } + format_data } -pub fn pad(s: &str, width: usize, alignment: &str) -> String { +pub fn pad(s: &str, width: usize, alignment: &Align) -> String { match alignment { - "<" => format!("{0:<1$}", s, width), - "^" => format!("{0:^1$}", s, width), - ">" => format!("{0:>1$}", s, width), - _ => unreachable!(), + Align::Left => format!("{0:<1$}", s, width), + Align::Center => format!("{0:^1$}", s, width), + Align::Right => format!("{0:>1$}", s, width), } } |