Change settings format
Settings are now stored in a specific format defined in the settings module. The Manifest.toml file will be loaded by default. Further config files can be imported with the default file import syntax. Signed-off-by: trivernis <trivernis@protonmail.com>pull/6/head
parent
31a4abff39
commit
d84b0d86dd
@ -1,31 +0,0 @@
|
||||
#![allow(unused)]
|
||||
|
||||
pub const BIB_REF_DISPLAY: &str = "bib-ref-display";
|
||||
pub const META_LANG: &str = "language";
|
||||
|
||||
// import and include options
|
||||
pub const IMP_IGNORE: &str = "ignored-imports";
|
||||
pub const IMP_STYLESHEETS: &str = "included-stylesheets";
|
||||
pub const IMP_CONFIGS: &str = "included-configs";
|
||||
pub const IMP_BIBLIOGRAPHY: &str = "included-bibliography";
|
||||
pub const IMP_GLOSSARY: &str = "included-glossary";
|
||||
pub const EMBED_EXTERNAL: &str = "embed-external";
|
||||
pub const SMART_ARROWS: &str = "smart-arrows";
|
||||
pub const INCLUDE_MATHJAX: &str = "include-math-jax";
|
||||
|
||||
// PDF options
|
||||
pub const PDF_DISPLAY_HEADER_FOOTER: &str = "pfd-display-header-footer";
|
||||
pub const PDF_HEADER_TEMPLATE: &str = "pdf-header-template";
|
||||
pub const PDF_FOOTER_TEMPLATE: &str = "pdf-footer-template";
|
||||
pub const PDF_MARGIN_TOP: &str = "pdf-margin-top";
|
||||
pub const PDF_MARGIN_BOTTOM: &str = "pdf-margin-bottom";
|
||||
pub const PDF_MARGIN_LEFT: &str = "pdf-margin-left";
|
||||
pub const PDF_MARGIN_RIGHT: &str = "pdf-margin-right";
|
||||
pub const PDF_PAGE_HEIGHT: &str = "pdf-page-height";
|
||||
pub const PDF_PAGE_WIDTH: &str = "pdf-page-width";
|
||||
pub const PDF_PAGE_SCALE: &str = "pdf-page-scale";
|
||||
|
||||
// Image Options
|
||||
pub const IMAGE_FORMAT: &str = "image-format";
|
||||
pub const IMAGE_MAX_WIDTH: &str = "image-max-width";
|
||||
pub const IMAGE_MAX_HEIGHT: &str = "image-max-height";
|
@ -1,188 +0,0 @@
|
||||
use crate::elements::MetadataValue;
|
||||
use crate::references::configuration::keys::{
|
||||
BIB_REF_DISPLAY, META_LANG, PDF_DISPLAY_HEADER_FOOTER, PDF_FOOTER_TEMPLATE,
|
||||
PDF_HEADER_TEMPLATE, PDF_MARGIN_BOTTOM, PDF_MARGIN_TOP,
|
||||
};
|
||||
use crate::references::templates::Template;
|
||||
use serde::export::TryFrom;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub(crate) mod keys;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Value {
|
||||
String(String),
|
||||
Bool(bool),
|
||||
Float(f64),
|
||||
Integer(i64),
|
||||
Template(Template),
|
||||
Array(Vec<Value>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ConfigEntry {
|
||||
inner: Value,
|
||||
}
|
||||
|
||||
pub type ConfigRefEntry = Arc<RwLock<ConfigEntry>>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Configuration {
|
||||
config: Arc<RwLock<HashMap<String, ConfigRefEntry>>>,
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn as_string(&self) -> String {
|
||||
match self {
|
||||
Value::String(string) => string.clone(),
|
||||
Value::Integer(int) => format!("{}", int),
|
||||
Value::Float(f) => format!("{:02}", f),
|
||||
Value::Bool(b) => format!("{}", b),
|
||||
Value::Array(a) => a.iter().fold("".to_string(), |a, b| {
|
||||
format!("{} \"{}\"", a, b.as_string())
|
||||
}),
|
||||
_ => "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the bool value if the value is a boolean
|
||||
pub fn as_bool(&self) -> Option<bool> {
|
||||
match self {
|
||||
Value::Bool(b) => Some(*b),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_float(&self) -> Option<f64> {
|
||||
match self {
|
||||
Value::Float(v) => Some(*v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigEntry {
|
||||
pub fn new(value: Value) -> Self {
|
||||
Self { inner: value }
|
||||
}
|
||||
|
||||
pub fn set(&mut self, value: Value) {
|
||||
self.inner = value;
|
||||
}
|
||||
|
||||
pub fn get(&self) -> &Value {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Configuration {
|
||||
fn default() -> Self {
|
||||
let mut self_config = Self::new();
|
||||
self_config.set(BIB_REF_DISPLAY, Value::String("{{number}}".to_string()));
|
||||
self_config.set(META_LANG, Value::String("en".to_string()));
|
||||
self_config.set(PDF_MARGIN_BOTTOM, Value::Float(0.5));
|
||||
self_config.set(PDF_MARGIN_TOP, Value::Float(0.5));
|
||||
self_config.set(PDF_DISPLAY_HEADER_FOOTER, Value::Bool(true));
|
||||
self_config.set(
|
||||
PDF_HEADER_TEMPLATE,
|
||||
Value::String("<div></div>".to_string()),
|
||||
);
|
||||
self_config.set(
|
||||
PDF_FOOTER_TEMPLATE,
|
||||
Value::String(
|
||||
include_str!("../../format/chromium_pdf/assets/default-footer-template.html")
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
|
||||
self_config
|
||||
}
|
||||
}
|
||||
|
||||
impl Configuration {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
config: Arc::new(RwLock::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
/// returns the value of a config entry
|
||||
pub fn get_entry(&self, key: &str) -> Option<ConfigEntry> {
|
||||
let config = self.config.read().unwrap();
|
||||
if let Some(entry) = config.get(key) {
|
||||
let value = entry.read().unwrap();
|
||||
Some(value.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// returns a config entry that is a reference to a value
|
||||
pub fn get_ref_entry(&self, key: &str) -> Option<ConfigRefEntry> {
|
||||
let config = self.config.read().unwrap();
|
||||
if let Some(entry) = config.get(&key.to_string()) {
|
||||
Some(Arc::clone(entry))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a config parameter
|
||||
pub fn set(&mut self, key: &str, value: Value) {
|
||||
let mut config = self.config.write().unwrap();
|
||||
if let Some(entry) = config.get(&key.to_string()) {
|
||||
entry.write().unwrap().set(value)
|
||||
} else {
|
||||
config.insert(
|
||||
key.to_string(),
|
||||
Arc::new(RwLock::new(ConfigEntry::new(value))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a config value based on a metadata value
|
||||
pub fn set_from_meta(&mut self, key: &str, value: MetadataValue) {
|
||||
match value {
|
||||
MetadataValue::String(string) => self.set(key, Value::String(string)),
|
||||
MetadataValue::Bool(bool) => self.set(key, Value::Bool(bool)),
|
||||
MetadataValue::Float(f) => self.set(key, Value::Float(f)),
|
||||
MetadataValue::Integer(i) => self.set(key, Value::Integer(i)),
|
||||
MetadataValue::Template(t) => self.set(key, Value::Template(t)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_from_toml(&mut self, value: &toml::Value) -> Option<()> {
|
||||
let table = value.as_table().cloned()?;
|
||||
table.iter().for_each(|(k, v)| {
|
||||
match v {
|
||||
toml::Value::Table(_) => self.set_from_toml(v).unwrap_or(()),
|
||||
_ => self.set(k, Value::try_from(v.clone()).unwrap()),
|
||||
};
|
||||
});
|
||||
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<toml::Value> for Value {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: toml::Value) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
toml::Value::Table(_) => Err(()),
|
||||
toml::Value::Float(f) => Ok(Value::Float(f)),
|
||||
toml::Value::Integer(i) => Ok(Value::Integer(i)),
|
||||
toml::Value::String(s) => Ok(Value::String(s)),
|
||||
toml::Value::Boolean(b) => Ok(Value::Bool(b)),
|
||||
toml::Value::Datetime(dt) => Ok(Value::String(dt.to_string())),
|
||||
toml::Value::Array(a) => Ok(Value::Array(
|
||||
a.iter()
|
||||
.cloned()
|
||||
.filter_map(|e| Value::try_from(e).ok())
|
||||
.collect::<Vec<Value>>(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
pub mod bibliography;
|
||||
pub mod configuration;
|
||||
pub mod glossary;
|
||||
pub mod placeholders;
|
||||
pub mod templates;
|
||||
|
@ -0,0 +1,18 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct FeatureSettings {
|
||||
pub embed_external: bool,
|
||||
pub smart_arrows: bool,
|
||||
pub include_mathjax: bool,
|
||||
}
|
||||
|
||||
impl Default for FeatureSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
embed_external: true,
|
||||
smart_arrows: true,
|
||||
include_mathjax: true,
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct FormatSettings {
|
||||
pub bib_ref_display: String,
|
||||
}
|
||||
|
||||
impl Default for FormatSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
bib_ref_display: "{{number}}".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct ImageSettings {
|
||||
pub format: Option<String>,
|
||||
pub max_width: Option<u32>,
|
||||
pub max_height: Option<u32>,
|
||||
}
|
||||
|
||||
impl Default for ImageSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
format: None,
|
||||
max_height: None,
|
||||
max_width: None,
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct ImportSettings {
|
||||
pub ignored_imports: Vec<String>,
|
||||
pub included_stylesheets: Vec<String>,
|
||||
pub included_bibliography: Vec<String>,
|
||||
pub included_glossaries: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for ImportSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ignored_imports: Vec::with_capacity(0),
|
||||
included_stylesheets: vec!["style.css".to_string()],
|
||||
included_bibliography: vec!["Bibliography.toml".to_string()],
|
||||
included_glossaries: vec!["Glossary.toml".to_string()],
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct MetadataSettings {
|
||||
pub title: Option<String>,
|
||||
pub author: Option<String>,
|
||||
pub language: String,
|
||||
}
|
||||
|
||||
impl Default for MetadataSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: None,
|
||||
author: None,
|
||||
language: "en".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
use crate::elements::{Metadata, MetadataValue};
|
||||
use crate::settings::feature_settings::FeatureSettings;
|
||||
use crate::settings::format_settings::FormatSettings;
|
||||
use crate::settings::image_settings::ImageSettings;
|
||||
use crate::settings::import_settings::ImportSettings;
|
||||
use crate::settings::metadata_settings::MetadataSettings;
|
||||
use crate::settings::pdf_settings::PDFSettings;
|
||||
use config::{ConfigError, Source};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt::{self, Display};
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub mod feature_settings;
|
||||
pub mod format_settings;
|
||||
pub mod image_settings;
|
||||
pub mod import_settings;
|
||||
pub mod metadata_settings;
|
||||
pub mod pdf_settings;
|
||||
|
||||
pub type SettingsResult<T> = Result<T, SettingsError>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SettingsError {
|
||||
IoError(io::Error),
|
||||
ConfigError(ConfigError),
|
||||
TomlError(toml::ser::Error),
|
||||
}
|
||||
|
||||
impl Display for SettingsError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::IoError(e) => write!(f, "IO Error: {}", e),
|
||||
Self::ConfigError(e) => write!(f, "Config Error: {}", e),
|
||||
Self::TomlError(e) => write!(f, "Toml Error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for SettingsError {}
|
||||
|
||||
impl From<io::Error> for SettingsError {
|
||||
fn from(e: io::Error) -> Self {
|
||||
Self::IoError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConfigError> for SettingsError {
|
||||
fn from(e: ConfigError) -> Self {
|
||||
Self::ConfigError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<toml::ser::Error> for SettingsError {
|
||||
fn from(e: toml::ser::Error) -> Self {
|
||||
Self::TomlError(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
||||
pub struct Settings {
|
||||
pub metadata: MetadataSettings,
|
||||
pub features: FeatureSettings,
|
||||
pub imports: ImportSettings,
|
||||
pub pdf: PDFSettings,
|
||||
pub images: ImageSettings,
|
||||
pub formatting: FormatSettings,
|
||||
pub custom_attributes: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Source for Settings {
|
||||
fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn collect(&self) -> Result<HashMap<String, config::Value>, config::ConfigError> {
|
||||
let source_str =
|
||||
toml::to_string(&self).map_err(|e| config::ConfigError::Foreign(Box::new(e)))?;
|
||||
let result = toml::de::from_str(&source_str)
|
||||
.map_err(|e| config::ConfigError::Foreign(Box::new(e)))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// Loads the settings from the specified path
|
||||
pub fn load(path: PathBuf) -> SettingsResult<Self> {
|
||||
let mut settings = config::Config::default();
|
||||
settings
|
||||
.merge(Self::default())?
|
||||
.merge(config::File::from(path))?;
|
||||
let settings: Self = settings.try_into()?;
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
/// Merges the current settings with the settings from the given path
|
||||
/// returning updated settings
|
||||
pub fn merge(&mut self, path: PathBuf) -> SettingsResult<()> {
|
||||
let mut settings = config::Config::default();
|
||||
settings
|
||||
.merge(self.clone())?
|
||||
.merge(config::File::from(path))?;
|
||||
let mut settings: Self = settings.try_into()?;
|
||||
mem::swap(self, &mut settings); // replace the old settings with the new ones
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn append_metadata<M: Metadata>(&mut self, metadata: M) {
|
||||
let entries = metadata.get_string_map();
|
||||
for (key, value) in entries {
|
||||
self.custom_attributes.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_from_meta(&mut self, key: &str, value: MetadataValue) {
|
||||
self.custom_attributes
|
||||
.insert(key.to_string(), value.to_string());
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct PDFSettings {
|
||||
pub display_header_footer: bool,
|
||||
pub header_template: Option<String>,
|
||||
pub footer_template: Option<String>,
|
||||
pub page_height: Option<f32>,
|
||||
pub page_width: Option<f32>,
|
||||
pub page_scale: f32,
|
||||
pub margin: PDFMarginSettings,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct PDFMarginSettings {
|
||||
pub top: Option<f32>,
|
||||
pub bottom: Option<f32>,
|
||||
pub left: Option<f32>,
|
||||
pub right: Option<f32>,
|
||||
}
|
||||
|
||||
impl Default for PDFMarginSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
top: Some(0.5),
|
||||
bottom: Some(0.5),
|
||||
left: None,
|
||||
right: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PDFSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
display_header_footer: true,
|
||||
header_template: Some("<div></div>".to_string()),
|
||||
footer_template: Some(
|
||||
include_str!("../format/chromium_pdf/assets/default-footer-template.html")
|
||||
.to_string(),
|
||||
),
|
||||
page_height: None,
|
||||
page_width: None,
|
||||
page_scale: 1.0,
|
||||
margin: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue