Use Default trait were possible

This commit is contained in:
daa84 2018-05-17 16:44:11 +03:00
parent f7ee2ea3d8
commit cb0727c0f0
5 changed files with 18 additions and 29 deletions

View File

@ -16,7 +16,7 @@ impl Manager {
let (plug_manage_state, store) = if Store::is_config_exists() {
(PlugManageState::NvimGtk, Store::load())
} else {
(PlugManageState::Unknown, Store::empty())
(PlugManageState::Unknown, Default::default())
};
Manager {
@ -45,14 +45,14 @@ impl Manager {
self.store = Store::load_from_plug(&self.vim_plug);
self.plug_manage_state = PlugManageState::VimPlug;
} else {
self.store = Store::empty();
self.store = Default::default();
}
}
PlugManageState::NvimGtk => {
if Store::is_config_exists() {
self.store = Store::load();
} else {
self.store = Store::empty();
self.store = Default::default();
}
}
PlugManageState::VimPlug => {
@ -60,7 +60,7 @@ impl Manager {
self.store = Store::load();
self.plug_manage_state = PlugManageState::NvimGtk;
} else {
self.store = Store::empty();
self.store = Default::default();
}
}
}

View File

@ -3,6 +3,7 @@ use toml;
use settings::SettingsLoader;
use super::vim_plug;
#[derive(Default)]
pub struct Store {
settings: Settings,
}
@ -20,15 +21,11 @@ impl Store {
Store { settings: Settings::load() }
}
pub fn empty() -> Self {
Store { settings: Settings::empty() }
}
pub fn load_from_plug(vim_plug: &vim_plug::Manager) -> Self {
let settings = match vim_plug.get_plugs() {
Err(msg) => {
error!("{}", msg);
Settings::empty()
Default::default()
}
Ok(plugs) => {
let plugs = plugs
@ -106,15 +103,17 @@ impl Settings {
}
}
impl SettingsLoader for Settings {
const SETTINGS_FILE: &'static str = "plugs.toml";
fn empty() -> Self {
impl Default for Settings {
fn default() -> Self {
Settings {
plugs: vec![],
enabled: false,
}
}
}
impl SettingsLoader for Settings {
const SETTINGS_FILE: &'static str = "plugs.toml";
fn from_str(s: &str) -> Result<Self, String> {
toml::from_str(&s).map_err(|e| format!("{}", e))

View File

@ -580,7 +580,7 @@ impl Entry {
use settings::SettingsLoader;
use toml;
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
struct ProjectSettings {
projects: Vec<ProjectEntrySettings>,
}
@ -607,10 +607,6 @@ impl ProjectEntrySettings {
impl SettingsLoader for ProjectSettings {
const SETTINGS_FILE: &'static str = "projects.toml";
fn empty() -> ProjectSettings {
ProjectSettings { projects: vec![] }
}
fn from_str(s: &str) -> Result<Self, String> {
toml::from_str(&s).map_err(|e| format!("{}", e))
}

View File

@ -111,11 +111,9 @@ use serde;
use dirs;
pub trait SettingsLoader: Sized + serde::Serialize {
pub trait SettingsLoader: Sized + serde::Serialize + Default {
const SETTINGS_FILE: &'static str;
fn empty() -> Self;
fn from_str(s: &str) -> Result<Self, String>;
fn load() -> Self {
@ -123,7 +121,7 @@ pub trait SettingsLoader: Sized + serde::Serialize {
Ok(settings) => settings,
Err(e) => {
error!("{}", e);
Self::empty()
Default::default()
}
}
}
@ -154,7 +152,7 @@ fn load_from_file<T: SettingsLoader>(path: &Path) -> Result<T, String> {
)?;
T::from_str(&contents)
} else {
Ok(T::empty())
Ok(Default::default())
}
}

View File

@ -445,8 +445,8 @@ struct WindowState {
sidebar_width: i32,
}
impl WindowState {
pub fn new() -> Self {
impl Default for WindowState {
fn default() -> Self {
WindowState {
current_width: DEFAULT_WIDTH,
current_height: DEFAULT_HEIGHT,
@ -460,10 +460,6 @@ impl WindowState {
impl SettingsLoader for WindowState {
const SETTINGS_FILE: &'static str = "window.toml";
fn empty() -> WindowState {
WindowState::new()
}
fn from_str(s: &str) -> Result<Self, String> {
toml::from_str(&s).map_err(|e| format!("{}", e))
}