2021-09-11 18:21:34 +00:00
|
|
|
pub mod format;
|
2021-10-09 16:05:32 +00:00
|
|
|
mod printers;
|
2021-09-11 18:21:34 +00:00
|
|
|
|
|
|
|
use crate::formatter::format::Format;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2022-04-09 18:34:15 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use rust_embed::RustEmbed;
|
|
|
|
use std::str;
|
|
|
|
use std::str::Utf8Error;
|
|
|
|
use std::fmt::{Display, Formatter};
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FormatConfigError {
|
|
|
|
IOError(std::io::Error),
|
|
|
|
TomlError(toml::de::Error),
|
|
|
|
Utf8Error(Utf8Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for FormatConfigError {}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for FormatConfigError {
|
|
|
|
fn from(e: std::io::Error) -> Self {
|
|
|
|
Self::IOError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<toml::de::Error> for FormatConfigError {
|
|
|
|
fn from(e: toml::de::Error) -> Self {
|
|
|
|
Self::TomlError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Utf8Error> for FormatConfigError {
|
|
|
|
fn from(e: Utf8Error) -> Self {
|
|
|
|
Self::Utf8Error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for FormatConfigError {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let err_msg = match self {
|
|
|
|
FormatConfigError::IOError(e) => e.to_string(),
|
|
|
|
FormatConfigError::TomlError(e) => e.to_string(),
|
|
|
|
FormatConfigError::Utf8Error(e) => e.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
write!(f, "Format Config Error: {}", err_msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustEmbed)]
|
|
|
|
#[folder = "formats/"]
|
|
|
|
#[include = "*.toml"]
|
|
|
|
struct BuiltInFormats;
|
2021-09-11 18:21:34 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct FormatConfig {
|
|
|
|
pub formats: Vec<Format>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FormatConfig {
|
2022-04-09 18:34:15 +00:00
|
|
|
pub fn new(config_path: &Option<PathBuf>) -> Result<Self, FormatConfigError> {
|
2021-09-11 18:21:34 +00:00
|
|
|
let mut contents = String::new();
|
|
|
|
|
2022-04-09 18:34:15 +00:00
|
|
|
if let Some(config_path) = config_path {
|
|
|
|
let mut config = File::open(config_path)?;
|
|
|
|
config.read_to_string(&mut contents)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
for format_file_path in BuiltInFormats::iter() {
|
|
|
|
if format_file_path.ends_with("md") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let format_file = BuiltInFormats::get(&format_file_path).unwrap();
|
|
|
|
|
|
|
|
contents.push_str(str::from_utf8(&format_file.data).unwrap());
|
|
|
|
}
|
|
|
|
|
2021-09-11 18:21:34 +00:00
|
|
|
|
2022-04-09 18:34:15 +00:00
|
|
|
Ok(toml::from_str(&contents)?)
|
2021-09-11 18:21:34 +00:00
|
|
|
}
|
|
|
|
}
|