use crate::error::Error;
use crate::parser::ParseOptions;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
#[serde(default)]
pub parser: ParserConfig,
#[serde(default)]
pub renderer: RendererConfig,
#[serde(default)]
pub theme: ThemeConfig,
#[serde(default)]
pub components: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParserConfig {
#[serde(default = "default_true")]
pub gfm: bool,
#[serde(default = "default_true")]
pub smart_punctuation: bool,
#[serde(default = "default_true")]
pub frontmatter: bool,
#[serde(default = "default_true")]
pub custom_components: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RendererConfig {
#[serde(default = "default_title")]
pub title: String,
#[serde(default = "default_true")]
pub include_default_css: bool,
#[serde(default)]
pub minify: bool,
#[serde(default)]
pub toc: bool,
#[serde(default = "default_true")]
pub syntax_highlight: bool,
#[serde(default = "default_true")]
pub code_copy_button: bool,
#[serde(default = "default_highlight_theme")]
pub highlight_theme: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeConfig {
#[serde(default = "default_theme")]
pub name: String,
#[serde(default)]
pub variables: HashMap<String, String>,
}
impl Default for ParserConfig {
fn default() -> Self {
Self {
gfm: true,
smart_punctuation: true,
frontmatter: true,
custom_components: true,
}
}
}
impl Default for RendererConfig {
fn default() -> Self {
Self {
title: default_title(),
include_default_css: true,
minify: false,
toc: false,
syntax_highlight: true,
code_copy_button: true,
highlight_theme: default_highlight_theme(),
}
}
}
impl Default for ThemeConfig {
fn default() -> Self {
Self {
name: default_theme(),
variables: HashMap::new(),
}
}
}
impl Config {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let content = fs::read_to_string(path)?;
let config: Config = toml::from_str(&content)?;
Ok(config)
}
}
impl From<&Config> for ParseOptions {
fn from(config: &Config) -> Self {
ParseOptions {
gfm: config.parser.gfm,
smart_punctuation: config.parser.smart_punctuation,
frontmatter: config.parser.frontmatter,
custom_components: config.parser.custom_components,
}
}
}
fn default_true() -> bool {
true
}
fn default_title() -> String {
"Markrust Document".to_string()
}
fn default_theme() -> String {
"modern".to_string()
}
fn default_highlight_theme() -> String {
"InspiredGitHub".to_string()
}