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
use std::path::PathBuf;
use std::fs::{remove_file, OpenOptions};
use std::io::Write;
use crate::dirs;
use crate::plug_manager;
#[derive(Clone)]
pub struct NvimConfig {
plug_config: Option<plug_manager::PlugManagerConfigSource>,
}
impl NvimConfig {
const CONFIG_PATH: &'static str = "settings.vim";
pub fn new(plug_config: Option<plug_manager::PlugManagerConfigSource>) -> Self {
NvimConfig { plug_config }
}
pub fn generate_config(&self) -> Option<PathBuf> {
if self.plug_config.is_some() {
match self.write_file() {
Err(err) => {
error!("{}", err);
None
}
Ok(file) => Some(file),
}
} else {
NvimConfig::config_path().map(remove_file);
None
}
}
pub fn config_path() -> Option<PathBuf> {
if let Ok(mut path) = dirs::get_app_config_dir() {
path.push(NvimConfig::CONFIG_PATH);
if path.is_file() {
return Some(path);
}
}
None
}
fn write_file(&self) -> Result<PathBuf, String> {
let mut config_dir = dirs::get_app_config_dir_create()?;
config_dir.push(NvimConfig::CONFIG_PATH);
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&config_dir)
.map_err(|e| format!("{}", e))?;
let content = &self.plug_config.as_ref().unwrap().source;
if !content.is_empty() {
debug!("{}", content);
file.write_all(content.as_bytes()).map_err(
|e| format!("{}", e),
)?;
}
file.sync_all().map_err(|e| format!("{}", e))?;
Ok(config_dir)
}
}