Fail on bad colors (#519)

This commit is contained in:
Denis Isidoro 2021-04-17 10:31:23 -03:00 committed by GitHub
parent 7fb2b53463
commit 9d9ae304ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,33 +2,31 @@ use crate::env_var;
use crate::filesystem::default_config_pathbuf; use crate::filesystem::default_config_pathbuf;
use crate::finder::FinderChoice; use crate::finder::FinderChoice;
use crate::fs; use crate::fs;
use crate::terminal::style; use crate::terminal::style::Color as TerminalColor;
use anyhow::Result; use anyhow::Result;
use serde::{de, Deserialize}; use serde::{de, Deserialize};
use std::convert::TryFrom;
use std::io::BufReader; use std::io::BufReader;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Color(#[serde(deserialize_with = "color_deserialize")] style::Color); pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor);
impl Color { impl Color {
pub fn from_str(color: &str) -> Self { pub fn get(&self) -> TerminalColor {
Self(style::Color::from_str(color).unwrap_or(style::Color::White))
}
pub fn get(&self) -> style::Color {
self.0 self.0
} }
} }
fn color_deserialize<'de, D>(deserializer: D) -> Result<style::Color, D::Error> fn color_deserialize<'de, D>(deserializer: D) -> Result<TerminalColor, D::Error>
where where
D: de::Deserializer<'de>, D: de::Deserializer<'de>,
{ {
let s: String = Deserialize::deserialize(deserializer)?; let s: String = Deserialize::deserialize(deserializer)?;
style::Color::from_str(&s).map_err(|_| de::Error::custom(format!("Failed to deserialize color: {}", s))) TerminalColor::try_from(s.as_str())
.map_err(|_| de::Error::custom(format!("Failed to deserialize color: {}", s)))
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -38,6 +36,7 @@ pub struct ColorWidth {
pub width_percentage: u16, pub width_percentage: u16,
pub min_width: u16, pub min_width: u16,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)] #[serde(default)]
pub struct Style { pub struct Style {
@ -113,7 +112,7 @@ impl YamlConfig {
impl Default for ColorWidth { impl Default for ColorWidth {
fn default() -> Self { fn default() -> Self {
Self { Self {
color: Color::from_str("white"), color: Color(TerminalColor::Blue),
width_percentage: 26, width_percentage: 26,
min_width: 20, min_width: 20,
} }
@ -124,12 +123,12 @@ impl Default for Style {
fn default() -> Self { fn default() -> Self {
Self { Self {
tag: ColorWidth { tag: ColorWidth {
color: Color::from_str("cyan"), color: Color(TerminalColor::Cyan),
width_percentage: 26, width_percentage: 26,
min_width: 20, min_width: 20,
}, },
comment: ColorWidth { comment: ColorWidth {
color: Color::from_str("blue"), color: Color(TerminalColor::Blue),
width_percentage: 42, width_percentage: 42,
min_width: 45, min_width: 45,
}, },