1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use dyn_clone::DynClone;
use std::fmt::{Debug, Display};

use crate::ast::Ast;

use thiserror::Error;

use crate::config::Format;
use crate::document::Document;
use crate::parser::ParserSettings;
use crate::parsers::split::Rule;
use crate::templates::TemplateManager;

pub mod exercises;

#[derive(Error, Debug)]
pub enum Error {
    #[error("code split syntax error at {}", .0)]
    CodeParseError(#[source] Box<pest::error::Error<Rule>>),
    #[error("could not parse attributes: {}", .0)]
    AttrParseError(#[from] toml::de::Error),

    #[error(transparent)]
    Any(#[from] anyhow::Error),
}

#[derive(Clone)]
pub struct PreprocessorContext<'a> {
    pub templates: &'a TemplateManager,
    pub output_format: &'a dyn Format,
}

pub trait AstPreprocessor: Display {
    fn name(&self) -> String;
    fn process(&mut self, input: Document<Ast>) -> Result<Document<Ast>, Error>;
}

#[typetag::serde(tag = "name")]
pub trait AstPreprocessorConfig: Debug + Send + Sync + DynClone {
    fn build(
        &self,
        ctx: &PreprocessorContext,
        settings: &ParserSettings,
    ) -> anyhow::Result<Box<dyn AstPreprocessor>>;
}

dyn_clone::clone_trait_object!(AstPreprocessorConfig);