diff options
Diffstat (limited to '')
-rw-r--r-- | src/model/config.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/model/config.rs b/src/model/config.rs new file mode 100644 index 0000000..844609d --- /dev/null +++ b/src/model/config.rs @@ -0,0 +1,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(()) + } +} |