♻️ refactor theme read file to de trait

This commit is contained in:
Wei Zhang 2022-07-29 01:35:01 +08:00 committed by Abin Simon
parent 85b9fc2a1e
commit 439b641fbf
2 changed files with 5 additions and 5 deletions

View file

@ -138,7 +138,7 @@ impl Colors {
let theme = match t {
ThemeOption::NoColor => None,
ThemeOption::Default | ThemeOption::NoLscolors => Some(Theme::default().color),
ThemeOption::Custom(ref file) => Some(Theme::from_path(file).unwrap_or_default().color),
ThemeOption::Custom(ref file) => Some(Theme::from_path::<ColorTheme>(file).unwrap_or_default()),
};
let lscolors = match t {
ThemeOption::Default | ThemeOption::Custom(_) => {

View file

@ -1,7 +1,7 @@
pub mod color;
pub mod icon;
use serde::Deserialize;
use serde::{Deserialize, de::DeserializeOwned};
use std::path::Path;
use std::fs;
@ -34,7 +34,7 @@ impl Theme {
/// This read theme from file,
/// use the file path if it is absolute
/// prefix the config_file dir to it if it is not
pub fn from_path(file: &str) -> Option<Self> {
pub fn from_path<D: DeserializeOwned>(file: &str) -> Option<D> {
let real = if let Some(path) = config_file::Config::expand_home(file) {
path
} else {
@ -76,7 +76,7 @@ impl Theme {
}
/// This constructs a Theme struct with a passed [Yaml] str.
fn with_yaml(yaml: &str) -> Result<Self, serde_yaml::Error> {
serde_yaml::from_str::<Self>(yaml)
fn with_yaml<D: DeserializeOwned>(yaml: &str) -> Result<D, serde_yaml::Error> {
serde_yaml::from_str::<D>(yaml)
}
}