diff options
author | Canop <cano.petrole@gmail.com> | 2020-12-11 13:12:05 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-12-11 13:12:05 +0100 |
commit | aab4f13284e2b61e485aeb898b84e2332c968227 (patch) | |
tree | ec5b936355017bf05f70ce9cf6fe75f5e2e239e6 /src/display | |
parent | f6efb7456752ac0fcd603d5c2afbb9a0118f630c (diff) |
use serde's derive to deserialize conf
Diffstat (limited to 'src/display')
-rw-r--r-- | src/display/col.rs | 101 | ||||
-rw-r--r-- | src/display/mod.rs | 2 |
2 files changed, 62 insertions, 41 deletions
diff --git a/src/display/col.rs b/src/display/col.rs index ebe80da..04edb57 100644 --- a/src/display/col.rs +++ b/src/display/col.rs @@ -1,8 +1,10 @@ use { - crate::{ - errors::ConfError, + crate::errors::ConfError, + serde::Deserialize, + std::{ + convert::TryFrom, + str::FromStr, }, - std::str::FromStr, }; // number of columns in enum @@ -36,6 +38,29 @@ pub enum Col { Name, } +pub type Cols = [Col;COLS_COUNT]; + +#[derive(Debug, Clone, Deserialize)] +#[serde(untagged)] +pub enum ColsConf { + /// the old representation, with one character per column + Compact(String), + /// the newer representation, with column names in clear + Array(Vec<String>), +} + +/// Default column order +pub static DEFAULT_COLS: Cols = [ + Col::Mark, + Col::Git, + Col::Size, + Col::Date, + Col::Permission, + Col::Count, + Col::Branch, + Col::Name, +]; + impl FromStr for Col { type Err = ConfError; fn from_str(s: &str) -> Result<Self, Self::Err> { @@ -65,45 +90,41 @@ impl Col { } None } - // warning: don't change the type of argument of this function or - // it won't compile on some platforms - /// return a Cols which tries to take the s setting into account - /// but is guaranteed to have every Col exactly once. - pub fn parse_cols(arr: &Vec<String>) -> Result<Cols, ConfError> { - let mut cols = DEFAULT_COLS; - for (idx, s) in arr.iter().enumerate() { - if idx >= COLS_COUNT { - return Err(ConfError::InvalidCols { - details: format!("too long: {:?}", arr), - }); - } - // we swap the cols, to ensure both keeps being present - let col = Col::from_str(s)?; - let dest_idx = col.index_in(&cols).unwrap(); // can't be none by construct - cols[dest_idx] = cols[idx]; - cols[idx] = col; +} + +impl TryFrom<&ColsConf> for Cols { + type Error = ConfError; + fn try_from(cc: &ColsConf) -> Result<Self, Self::Error> { + match cc { + ColsConf::Compact(s) => parse_cols_single_str(s), + ColsConf::Array(arr) => parse_cols(arr), } - debug!("cols from conf = {:?}", cols); - Ok(cols) - } - /// return a Cols which tries to take the s setting into account - /// but is guaranteed to have every Col exactly once. - pub fn parse_cols_single_str(s: &str) -> Result<Cols, ConfError> { - Self::parse_cols(&s.chars().map(String::from).collect()) } } -pub type Cols = [Col;COLS_COUNT]; - -/// Default column order -pub static DEFAULT_COLS: Cols = [ - Col::Mark, - Col::Git, - Col::Size, - Col::Date, - Col::Permission, - Col::Count, - Col::Branch, - Col::Name, -]; +/// return a Cols which tries to take the s setting into account +/// but is guaranteed to have every Col exactly once. +#[allow(clippy::ptr_arg)] // &[String] won't compile on all platforms +pub fn parse_cols(arr: &Vec<String>) -> Result<Cols, ConfError> { + let mut cols = DEFAULT_COLS; + for (idx, s) in arr.iter().enumerate() { + if idx >= COLS_COUNT { + return Err(ConfError::InvalidCols { + details: format!("too long: {:?}", arr), + }); + } + // we swap the cols, to ensure both keeps being present + let col = Col::from_str(s)?; + let dest_idx = col.index_in(&cols).unwrap(); // can't be none by construct + cols[dest_idx] = cols[idx]; + cols[idx] = col; + } + debug!("cols from conf = {:?}", cols); + Ok(cols) +} +/// return a Cols which tries to take the s setting into account +/// but is guaranteed to have every Col exactly once. +pub fn parse_cols_single_str(s: &str) -> Result<Cols, ConfError> { + parse_cols(&s.chars().map(String::from).collect()) +} diff --git a/src/display/mod.rs b/src/display/mod.rs index 5db1f1e..b40e92c 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -37,7 +37,7 @@ mod permissions; pub use { areas::Areas, - col::{Col, Cols, DEFAULT_COLS}, + col::*, crop_writer::CropWriter, displayable_tree::DisplayableTree, filling::*, |