2017-10-19 14:04:58 +00:00
|
|
|
use toml;
|
|
|
|
|
|
|
|
use settings::SettingsLoader;
|
2017-10-20 15:06:05 +00:00
|
|
|
use super::vim_plug;
|
2017-10-19 14:04:58 +00:00
|
|
|
|
2017-10-20 15:06:05 +00:00
|
|
|
pub struct Store {
|
|
|
|
settings: Settings,
|
|
|
|
}
|
2017-10-18 14:49:56 +00:00
|
|
|
|
|
|
|
impl Store {
|
2017-10-24 15:03:34 +00:00
|
|
|
pub fn is_config_exists() -> bool {
|
|
|
|
Settings::is_file_exists()
|
|
|
|
}
|
|
|
|
|
2017-10-29 10:55:36 +00:00
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
self.settings.enabled
|
|
|
|
}
|
|
|
|
|
2017-10-20 15:06:05 +00:00
|
|
|
pub fn load() -> Self {
|
|
|
|
Store { settings: Settings::load() }
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:25:23 +00:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
Store { settings: Settings::empty() }
|
|
|
|
}
|
|
|
|
|
2017-10-20 15:06:05 +00:00
|
|
|
pub fn load_from_plug(vim_plug: &vim_plug::Manager) -> Self {
|
|
|
|
let settings = match vim_plug.get_plugs() {
|
|
|
|
Err(msg) => {
|
|
|
|
error!("{}", msg);
|
|
|
|
Settings::empty()
|
|
|
|
}
|
|
|
|
Ok(plugs) => {
|
|
|
|
let plugs = plugs
|
|
|
|
.iter()
|
|
|
|
.map(|vpi| PlugInfo::new(vpi.name.to_owned(), vpi.uri.to_owned()))
|
|
|
|
.collect();
|
|
|
|
Settings::new(plugs)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Store { settings }
|
2017-10-19 14:04:58 +00:00
|
|
|
}
|
2017-10-21 17:20:55 +00:00
|
|
|
|
|
|
|
pub fn get_plugs(&self) -> &[PlugInfo] {
|
|
|
|
&self.settings.plugs
|
|
|
|
}
|
2017-10-29 10:55:36 +00:00
|
|
|
|
|
|
|
pub fn set_enabled(&mut self, enabled: bool) {
|
|
|
|
self.settings.enabled = enabled;
|
|
|
|
}
|
|
|
|
|
2017-10-29 19:16:55 +00:00
|
|
|
pub fn clear_removed(&mut self) {
|
|
|
|
self.settings.plugs.retain(|p| !p.removed);
|
|
|
|
}
|
|
|
|
|
2017-10-29 10:55:36 +00:00
|
|
|
pub fn save(&self) {
|
|
|
|
self.settings.save();
|
|
|
|
}
|
2017-10-29 19:16:55 +00:00
|
|
|
|
|
|
|
pub fn remove_plug(&mut self, idx: usize) {
|
|
|
|
self.settings.plugs[idx].removed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn restore_plug(&mut self, idx: usize) {
|
|
|
|
self.settings.plugs[idx].removed = false;
|
|
|
|
}
|
2017-10-30 15:25:23 +00:00
|
|
|
|
2017-11-01 14:40:54 +00:00
|
|
|
pub fn add_plug(&mut self, plug: PlugInfo) -> bool {
|
|
|
|
let path = plug.get_plug_path();
|
|
|
|
if self.settings.plugs.iter().any(|p| p.get_plug_path() == path || p.name == plug.name) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-30 15:25:23 +00:00
|
|
|
self.settings.plugs.push(plug);
|
2017-11-01 14:40:54 +00:00
|
|
|
true
|
2017-10-30 15:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plugs_count(&self) -> usize {
|
|
|
|
self.settings.plugs.len()
|
|
|
|
}
|
2017-10-19 14:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Settings {
|
2017-10-27 15:06:18 +00:00
|
|
|
enabled: bool,
|
2017-10-29 10:55:36 +00:00
|
|
|
plugs: Vec<PlugInfo>,
|
2017-10-19 14:04:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 15:06:05 +00:00
|
|
|
impl Settings {
|
|
|
|
fn new(plugs: Vec<PlugInfo>) -> Self {
|
2017-10-27 15:06:18 +00:00
|
|
|
Settings { plugs, enabled: false }
|
2017-10-20 15:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 14:04:58 +00:00
|
|
|
impl SettingsLoader for Settings {
|
|
|
|
const SETTINGS_FILE: &'static str = "plugs.toml";
|
|
|
|
|
|
|
|
fn empty() -> Self {
|
2017-10-27 15:06:18 +00:00
|
|
|
Settings { plugs: vec![], enabled: false }
|
2017-10-19 14:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, String> {
|
|
|
|
toml::from_str(&s).map_err(|e| format!("{}", e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct PlugInfo {
|
2017-10-21 17:20:55 +00:00
|
|
|
pub name: String,
|
|
|
|
pub url: String,
|
2017-10-26 14:56:53 +00:00
|
|
|
pub removed: bool,
|
2017-10-19 14:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PlugInfo {
|
|
|
|
pub fn new(name: String, url: String) -> Self {
|
2017-10-26 14:56:53 +00:00
|
|
|
PlugInfo {
|
|
|
|
name,
|
|
|
|
url,
|
|
|
|
removed: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_plug_path(&self) -> String {
|
|
|
|
if self.url.contains("github.com") {
|
|
|
|
let mut path_comps: Vec<&str> = self.url
|
|
|
|
.trim_right_matches(".git")
|
|
|
|
.rsplit('/')
|
|
|
|
.take(2)
|
|
|
|
.collect();
|
|
|
|
path_comps.reverse();
|
|
|
|
path_comps.join("/")
|
|
|
|
} else {
|
|
|
|
self.url.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_plug_path() {
|
|
|
|
let plug = PlugInfo::new(
|
|
|
|
"rust.vim".to_owned(),
|
|
|
|
"https://git::@github.com/rust-lang/rust.vim.git".to_owned(),
|
|
|
|
);
|
|
|
|
assert_eq!("rust-lang/rust.vim".to_owned(), plug.get_plug_path());
|
2017-10-18 14:49:56 +00:00
|
|
|
}
|
|
|
|
}
|