1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
use toml; use crate::settings::SettingsLoader; use super::vim_plug; #[derive(Default)] pub struct Store { settings: Settings, } impl Store { pub fn is_config_exists() -> bool { Settings::is_file_exists() } pub fn is_enabled(&self) -> bool { self.settings.enabled } pub fn load() -> Self { Store { settings: Settings::load() } } pub fn load_from_plug(vim_plug: &vim_plug::Manager) -> Self { let settings = match vim_plug.get_plugs() { Err(msg) => { error!("{}", msg); Default::default() } Ok(plugs) => { let plugs = plugs .iter() .map(|vpi| PlugInfo::new(vpi.name.to_owned(), vpi.uri.to_owned())) .collect(); Settings::new(plugs) } }; Store { settings } } pub fn get_plugs(&self) -> &[PlugInfo] { &self.settings.plugs } pub fn set_enabled(&mut self, enabled: bool) { self.settings.enabled = enabled; } pub fn clear_removed(&mut self) { self.settings.plugs.retain(|p| !p.removed); } pub fn save(&self) { self.settings.save(); } 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; } 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; } self.settings.plugs.push(plug); true } pub fn plugs_count(&self) -> usize { self.settings.plugs.len() } pub fn move_item(&mut self, idx: usize, offset: i32) { let plug = self.settings.plugs.remove(idx); self.settings.plugs.insert( (idx as i32 + offset) as usize, plug, ); } } #[derive(Serialize, Deserialize)] struct Settings { enabled: bool, plugs: Vec<PlugInfo>, } impl Settings { fn new(plugs: Vec<PlugInfo>) -> Self { Settings { plugs, enabled: false, } } } 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)) } } #[derive(Serialize, Deserialize)] pub struct PlugInfo { pub name: String, pub url: String, pub removed: bool, } impl PlugInfo { pub fn new(name: String, url: String) -> Self { 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_end_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()); } }