Add Config struct

This commit is contained in:
Kogia-sima 2020-06-06 11:58:14 +09:00
parent cd709be6e4
commit 420d83521c
4 changed files with 37 additions and 27 deletions

View File

@ -1,7 +1,8 @@
use quote::ToTokens;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;
use crate::config::Config;
use crate::error::*;
use crate::optimizer::Optimizer;
use crate::parser::Parser;
@ -9,20 +10,9 @@ use crate::resolver::Resolver;
use crate::translator::Translator;
use crate::util::rustfmt_block;
#[derive(Default)]
pub struct Compiler {
delimiter: char,
escape: bool,
cache_dir: PathBuf,
}
impl Default for Compiler {
fn default() -> Self {
Self {
delimiter: '%',
escape: true,
cache_dir: Path::new(env!("OUT_DIR")).join("cache"),
}
}
config: Config
}
impl Compiler {
@ -30,21 +20,15 @@ impl Compiler {
Self::default()
}
pub fn delimiter(mut self, new: char) -> Self {
self.delimiter = new;
self
}
pub fn escape(mut self, new: bool) -> Self {
self.escape = new;
self
pub fn with_config(config: Config) -> Self {
Self { config }
}
pub fn compile_file(&self, input: &Path, output: &Path) -> Result<(), Error> {
// TODO: introduce cache system
let parser = Parser::new().delimiter(self.delimiter);
let translator = Translator::new().escape(self.escape);
let parser = Parser::new().delimiter(self.config.delimiter);
let translator = Translator::new().escape(self.config.escape);
let resolver = Resolver::new();
let optimizer = Optimizer::new();

View File

@ -0,0 +1,23 @@
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub struct Config {
pub delimiter: char,
pub escape: bool,
pub cache_dir: PathBuf,
#[doc(hidden)]
pub _non_exhaustive: ()
}
impl Default for Config {
fn default() -> Self {
Self {
delimiter: '%',
escape: true,
cache_dir: Path::new(env!("OUT_DIR")).join("cache"),
_non_exhaustive: ()
}
}
}
// TODO: Global configration file

View File

@ -4,6 +4,7 @@
mod error;
mod compiler;
mod config;
mod optimizer;
mod parser;
mod resolver;

View File

@ -6,6 +6,7 @@ use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::punctuated::Punctuated;
use syn::{Fields, Ident, ItemStruct, Lifetime, LitBool, LitChar, LitStr, Token};
use crate::config::Config;
use crate::compiler::Compiler;
use crate::error::*;
@ -116,14 +117,15 @@ fn compile(
output_file: &Path,
options: &DeriveTemplateOptions,
) -> Result<(), Error> {
let mut compiler = Compiler::new();
let mut config = Config::default();
if let Some(ref delimiter) = options.delimiter {
compiler = compiler.delimiter(delimiter.value());
config.delimiter = delimiter.value();
}
if let Some(ref escape) = options.escape {
compiler = compiler.escape(escape.value);
config.escape = escape.value;
}
let compiler = Compiler::with_config(config);
compiler.compile_file(input_file, &*output_file)
}