summaryrefslogtreecommitdiff
path: root/src/model/config.rs
blob: 844609d8d6a59284c1c5348ea0491ee86b62bdf9 (plain)
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
//! Definition of an iris [config][Config].

use crate::{get_writer, path, Plugin, Result};

use log::trace;
use std::{fs, io::Write, path::Path};

/// Iris config
#[derive(Debug)]
pub struct Config {
	/// List of iris plugin specifications
	pub plugins: Vec<Plugin>,
}

impl Config {
	/// Read an iris config from the file system
	pub fn read_from_file<P>(path: P) -> Result<Self>
	where
		P: AsRef<Path>,
	{
		trace!("reading: {}", path.as_ref().display());
		let contents = fs::read_to_string(&path)?;
		let iris = Self::parse(&contents)?;
		Ok(iris)
	}

	/// Read an iris config from the plugin file path
	pub fn read_from_plugin_file() -> Result<Self> {
		trace!("read from plugin file");
		Self::read_from_file(path::plugin_file())
	}

	/// Read an iris config from the lock file path
	pub fn read_from_lock_file() -> Result<Self> {
		trace!("read from lock file");
		Self::read_from_file(path::lock_file())
	}

	/// Write the iris config to the file system.
	pub fn write_to_file<P>(&self, path: P) -> Result<()>
	where
		P: AsRef<Path>,
	{
		trace!("writing: {}", path.as_ref().display());
		let contents = self.to_string()?;
		let mut write = get_writer(&path, '#')?;
		write.write_all(contents.as_bytes())?;
		Ok(())
	}

	/// Write the iris config to the plugins file.
	pub fn write_to_plugin_file(&self) -> Result<()> {
		self.write_to_file(path::plugin_file())
	}

	/// Write the iris config to the lock file.
	pub fn write_to_lock_file(&self) -> Result<()> {
		self.write_to_file(path::lock_file())
	}

	/// Write the vim autoload file formed from each plugin.
	pub fn write_autoload_file(&self) -> Result<()> {
		let path = path::autoload_file();
		trace!("writing: {}", path.display());

		let mut f = get_writer(&path, '"')?;

		// BEGIN iris#load
		writeln!(f, "function! iris#load()")?;
		// add each plugin to load path
		for plugin in &self.plugins {
			// runtime path
			{
				let path = plugin.runtime_path().display();
				writeln!(f, "set runtimepath+={path}")?;
			}
			// lua package path
			if let Some(path) = plugin.lua_package_path() {
				let path = path.display();
				writeln!(f, "lua package.path = package.path .. \";{path}\"")?;
			}
			// vim source file
			if let Some(path) = plugin.vim_source_file() {
				let path = path.display();
				writeln!(f, "source {path}")?;
			}
			// lua source file
			if let Some(path) = plugin.lua_source_file() {
				let path = path.display();
				writeln!(f, "lua dofile('{path}')")?;
			}
		}
		for plugin in &self.plugins {
			// vim source after file
			if let Some(path) = plugin.vim_source_after_file() {
				let path = path.display();
				writeln!(f, "source {path}")?;
			}
			// lua source after file
			if let Some(path) = plugin.lua_source_after_file() {
				let path = path.display();
				writeln!(f, "lua dofile('{path}')")?;
			}
		}
		writeln!(f, "endfunction")?;
		// END iris#load

		Ok(())
	}
}