This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf-rs/src/config/mod.rs

91 lines
2.4 KiB
Rust
Raw Normal View History

2017-07-17 09:52:22 +01:00
use serde_yaml;
2017-07-18 09:20:39 +01:00
use serde_yaml::Value;
2017-07-18 13:39:31 +01:00
use std::path::PathBuf;
2017-07-19 19:54:10 +01:00
use std::collections::HashMap;
2017-11-21 22:12:46 +00:00
use utils::{result_prefix, resolve_path, result_override};
use std::fs::{remove_file, File};
use std::env::current_dir;
use std::io::Read;
2017-07-17 09:52:22 +01:00
2017-07-17 09:59:54 +01:00
pub mod consts;
2017-09-10 17:59:11 +01:00
pub mod csl;
2017-07-17 09:52:22 +01:00
2017-08-14 22:10:05 +01:00
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
2017-07-17 09:52:22 +01:00
pub struct Config {
2017-11-21 22:12:46 +00:00
input: Vec<PathBuf>,
output: HashMap<String, PathBuf>,
2017-07-20 09:24:04 +01:00
pub title: String,
2017-11-21 22:12:46 +00:00
#[serde(default = "default_verbosity")]
2017-09-10 18:51:40 +01:00
pub verbosity: u64,
2017-11-21 22:12:46 +00:00
2017-09-10 19:09:21 +01:00
pub references: Option<References>
2017-09-10 18:51:40 +01:00
}
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct References {
2017-11-21 22:12:46 +00:00
bibliography: PathBuf,
csl: String,
pub inner_csl: Option<String>
2017-07-17 09:52:22 +01:00
}
2017-11-21 22:12:46 +00:00
2017-07-17 09:52:22 +01:00
impl Config {
2017-11-21 22:12:46 +00:00
pub fn absolute_output(&self, output_type: String) -> PathBuf {
return resolve_path(self.output.get(&output_type).unwrap());
2017-07-17 14:53:18 +01:00
}
2017-07-17 09:52:22 +01:00
2017-11-21 22:12:46 +00:00
pub fn has_output(&self, output_type: String) -> bool {
return self.output.contains_key(&output_type);
}
2017-07-17 09:52:22 +01:00
2017-11-21 22:12:46 +00:00
pub fn absolute_inputs(&self) -> Vec<PathBuf> {
let working_dir = current_dir().unwrap();
return self.input.iter().map(|x| working_dir.join(&x)).collect();
}
2017-07-17 09:52:22 +01:00
}
2017-09-10 19:23:12 +01:00
2017-11-21 22:12:46 +00:00
impl References {
pub fn absolute_bibliography(&self) -> PathBuf {
return resolve_path(&self.bibliography);
}
2017-09-10 19:23:12 +01:00
2017-11-21 22:12:46 +00:00
pub fn absolute_csl(&mut self) -> PathBuf {
let tmp_csl_path = csl::unpack_csl(self.csl.as_str().into());
self.inner_csl = Some(tmp_csl_path.as_path().to_str().unwrap().into());
return tmp_csl_path;
2017-09-10 19:23:12 +01:00
}
}
2017-11-21 22:12:46 +00:00
fn default_verbosity() -> u64 {
return 0;
}
fn get_config_path() -> PathBuf {
let mut working_dir = current_dir().unwrap();
working_dir.push(consts::CONFIG_FILE_NAME);
return working_dir;
}
fn read() -> Result<String, String> {
let config_path = get_config_path();
let mut config_file = try!(result_override(
File::open(&config_path),
format!("Unable to find config file at {}", config_path.display())
));
let mut contents = String::new();
try!(result_override(
config_file.read_to_string(&mut contents),
format!("Failed to read config file at {}.", config_path.display())
));
return Ok(contents);
}
pub fn get_config() -> Result<Config, String> {
let config_str = try!(read());
let config: Config =
try!(result_prefix(serde_yaml::from_str(&config_str), "Config Parse Error".into()));
return Ok(config);
}