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

#![forbid(unsafe_code)]
#![warn(missing_docs)]

//! This crate provides a compiler for the [Papyri markup language](https://kaya3.github.io/papyri).

pub mod compiler;
pub mod errors;
pub mod parser;
pub mod utils;

/// Compiles Papyri source given as a string into HTML, as a string. If any
/// errors or warnings occur during compilation, the diagnostics are returned
/// instead.
pub fn compile_str(src: &str) -> Result<String, errors::Diagnostics> {
    let src = utils::SourceFile::synthetic("string", src);
    
    let mut ctx = compiler::Context::new(errors::ReportingLevel::Warning, None);
    let result = ctx.compile(src);
    
    if ctx.diagnostics.is_empty() {
        let mut out = Vec::new();
        ctx.render(&result.out, true, &mut out)
            .unwrap();
        
        let out = String::from_utf8(out).unwrap();
        Ok(out)
    } else {
        Err(ctx.diagnostics)
    }
}