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