Skip to main content

opentalk_roomserver_report_generation/
lib.rs

1// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5//! OpenTalk report generation functionality
6//!
7//! This crate provides an abstraction for generating PDF reports using
8//! [`typst`](https://docs.rs/typst). ! The implementation here is strongly
9//! opinionated with regard to the requirements in the OpenTalk ecosystem,
10//! but you still may find pieces of it useful for general purpose scenarios.
11
12mod report_date_time;
13mod report_generation_error;
14mod world;
15
16use std::{borrow::Cow, collections::BTreeMap, path::Path};
17
18pub use report_date_time::{ReportDateTime, ToReportDateTime};
19pub use report_generation_error::ReportGenerationError;
20use typst::{World as _, diag::SourceResult};
21use typst_pdf::PdfOptions;
22use world::World;
23
24/// Generate a pdf file from a typst source string and data.
25pub fn generate_pdf_report(
26    source: String,
27    data: BTreeMap<&Path, Cow<'static, [u8]>>,
28) -> Result<Vec<u8>, ReportGenerationError> {
29    let world = World::new(source, data);
30
31    let report = match generate_pdf_report_inner(&world) {
32        Ok(d) => d,
33        Err(e) => {
34            for diagnostic in &e {
35                if let Ok(source) = world.source(*world::MAIN_ID) {
36                    let range = source.range(diagnostic.span);
37                    tracing::warn!("{}: {:?}", diagnostic.message, range);
38                }
39            }
40            return Err(e.into());
41        }
42    };
43
44    Ok(report)
45}
46
47fn generate_pdf_report_inner(world: &World) -> SourceResult<Vec<u8>> {
48    let document = typst::compile(&world).output?;
49    typst_pdf::pdf(&document, &PdfOptions::default())
50}