forked from syndicate-lang/preserves
Plugins are now part of CompilerConfig
This commit is contained in:
parent
eafc22fb1c
commit
06a08631aa
|
@ -28,9 +28,9 @@ use std::path::PathBuf;
|
|||
|
||||
pub type ModulePath = Vec<String>;
|
||||
|
||||
pub trait Plugin {
|
||||
pub trait Plugin: std::fmt::Debug {
|
||||
fn generate(
|
||||
&mut self,
|
||||
&self,
|
||||
module_ctxt: &mut context::ModuleContext,
|
||||
definition_name: &str,
|
||||
definition: &Definition,
|
||||
|
@ -44,6 +44,7 @@ pub struct CompilerConfig {
|
|||
pub fully_qualified_module_prefix: String,
|
||||
pub support_crate: String,
|
||||
pub module_aliases: Map<ModulePath, String>,
|
||||
pub plugins: Vec<Box<dyn Plugin>>,
|
||||
}
|
||||
|
||||
impl CompilerConfig {
|
||||
|
@ -57,6 +58,12 @@ impl CompilerConfig {
|
|||
fully_qualified_module_prefix: fully_qualified_module_prefix,
|
||||
support_crate: "preserves_schema".to_owned(),
|
||||
module_aliases: Map::new(),
|
||||
plugins: vec![
|
||||
Box::new(types::TypePlugin),
|
||||
Box::new(readers::ReaderPlugin),
|
||||
Box::new(parsers::ParserPlugin),
|
||||
Box::new(unparsers::UnparserPlugin),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,20 +151,13 @@ pub fn compile(config: &CompilerConfig) -> io::Result<()> {
|
|||
|
||||
let modes = modes; // rebind as non-mutable
|
||||
|
||||
let mut plugins: Vec<Box<dyn Plugin>> = vec![
|
||||
Box::new(types::TypePlugin),
|
||||
Box::new(readers::ReaderPlugin),
|
||||
Box::new(parsers::ParserPlugin),
|
||||
Box::new(unparsers::UnparserPlugin),
|
||||
];
|
||||
|
||||
for (n, d) in &v.definitions.0 {
|
||||
for plugin in plugins.iter_mut() {
|
||||
for plugin in config.plugins.iter() {
|
||||
plugin.generate(&mut m, n, d);
|
||||
}
|
||||
for mode in &modes {
|
||||
m.mode = Some(*mode);
|
||||
for plugin in plugins.iter_mut() {
|
||||
for plugin in config.plugins.iter() {
|
||||
plugin.generate(&mut m, n, d);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,10 +7,11 @@ use super::context::{ModuleContextMode, ModuleContext, FunctionContext};
|
|||
use super::names;
|
||||
use super::types::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParserPlugin;
|
||||
|
||||
impl compiler::Plugin for ParserPlugin {
|
||||
fn generate(&mut self, module_ctxt: &mut ModuleContext, definition_name: &str, definition: &Definition) {
|
||||
fn generate(&self, module_ctxt: &mut ModuleContext, definition_name: &str, definition: &Definition) {
|
||||
if module_ctxt.mode.is_some() {
|
||||
gen_definition_parser(module_ctxt, definition_name, definition)
|
||||
}
|
||||
|
|
|
@ -14,10 +14,11 @@ use super::context::{ModuleContext, FunctionContext};
|
|||
use super::names;
|
||||
use super::types::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReaderPlugin;
|
||||
|
||||
impl compiler::Plugin for ReaderPlugin {
|
||||
fn generate(&mut self, module_ctxt: &mut ModuleContext, definition_name: &str, definition: &Definition) {
|
||||
fn generate(&self, module_ctxt: &mut ModuleContext, definition_name: &str, definition: &Definition) {
|
||||
if module_ctxt.mode.is_none() {
|
||||
gen_definition_reader(module_ctxt, definition_name, definition)
|
||||
}
|
||||
|
|
|
@ -31,10 +31,11 @@ pub enum TField {
|
|||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
|
||||
pub struct TRecord(pub Vec<(String, TField)>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TypePlugin;
|
||||
|
||||
impl compiler::Plugin for TypePlugin {
|
||||
fn generate(&mut self, m: &mut ModuleContext, n: &str, d: &Definition) {
|
||||
fn generate(&self, m: &mut ModuleContext, n: &str, d: &Definition) {
|
||||
if m.mode.is_none() {
|
||||
m.define_type(item(render_definition_type(m, n, &definition_type(d))));
|
||||
m.define_type(item(seq![
|
||||
|
|
|
@ -10,10 +10,11 @@ use super::context::{ModuleContextMode, ModuleContext, FunctionContext};
|
|||
use super::names;
|
||||
use super::types::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnparserPlugin;
|
||||
|
||||
impl compiler::Plugin for UnparserPlugin {
|
||||
fn generate(&mut self, module_ctxt: &mut ModuleContext, definition_name: &str, definition: &Definition) {
|
||||
fn generate(&self, module_ctxt: &mut ModuleContext, definition_name: &str, definition: &Definition) {
|
||||
if module_ctxt.mode.is_some() {
|
||||
gen_definition_unparser(module_ctxt, definition_name, definition)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue