Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on bad colors #519

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/config/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ use crate::env_var;
use crate::filesystem::default_config_pathbuf;
use crate::finder::FinderChoice;
use crate::fs;
use crate::terminal::style;
use crate::terminal::style::Color as TerminalColor;
use anyhow::Result;
use serde::{de, Deserialize};
use std::convert::TryFrom;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;

#[derive(Deserialize)]
pub struct Color(#[serde(deserialize_with = "color_deserialize")] style::Color);
pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor);

impl Color {
pub fn from_str(color: &str) -> Self {
Self(style::Color::from_str(color).unwrap_or(style::Color::White))
}

pub fn get(&self) -> style::Color {
pub fn get(&self) -> TerminalColor {
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
D: de::Deserializer<'de>,
{
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)]
Expand All @@ -38,6 +36,7 @@ pub struct ColorWidth {
pub width_percentage: u16,
pub min_width: u16,
}

#[derive(Deserialize)]
#[serde(default)]
pub struct Style {
Expand Down Expand Up @@ -113,7 +112,7 @@ impl YamlConfig {
impl Default for ColorWidth {
fn default() -> Self {
Self {
color: Color::from_str("white"),
color: Color(TerminalColor::Blue),
width_percentage: 26,
min_width: 20,
}
Expand All @@ -124,12 +123,12 @@ impl Default for Style {
fn default() -> Self {
Self {
tag: ColorWidth {
color: Color::from_str("cyan"),
color: Color(TerminalColor::Cyan),
width_percentage: 26,
min_width: 20,
},
comment: ColorWidth {
color: Color::from_str("blue"),
color: Color(TerminalColor::Blue),
width_percentage: 42,
min_width: 45,
},
Expand Down