hyperlit_backend/backend.rs
1use hyperlit_base::result::HyperlitResult;
2use hyperlit_model::directive_evaluation::DirectiveEvaluation;
3use hyperlit_model::segment::{Segment, SegmentId};
4use std::path::Path;
5/* 📖 Adding a new output backend #backend #howto
6
7To add a new output backend, you need to implement the `Backend` trait.
8
9See `mdbook_backend.rs` for an example.
10
11 */
12
13/// Parameters for the compilation process
14pub trait BackendCompileParams {
15 /// Path to the directory containing the documentation files
16 fn docs_directory(&self) -> &Path;
17 /// Path to the directory where the documentation will be built
18 fn build_directory(&self) -> &Path;
19 /// Path to the directory where the documentation will be output
20 fn output_directory(&self) -> &Path;
21 /// Retrieve all segments containing the given tag
22 fn evaluate_directive(&self, tag: &str) -> HyperlitResult<DirectiveEvaluation>;
23
24 /// Mark a segment as included in the output
25 fn set_segment_included(&mut self, segment_id: SegmentId) -> HyperlitResult<()>;
26}
27
28/// An output backend
29pub trait Backend {
30 /// Perform an (optional)preparation step before files are copied to the build directory
31 fn prepare(&mut self, _params: &mut dyn BackendCompileParams) -> HyperlitResult<()> {
32 Ok(())
33 }
34 /// Perform the actual compilation of the documentation
35 /// In this step the files in the build_directory should be transformed into the output_directory
36 fn compile(&self, params: &dyn BackendCompileParams) -> HyperlitResult<()>;
37 /// Transform a given segment into its representation in the backend language (e.g., markdown)
38 fn transform_segment(&self, segment: &Segment) -> HyperlitResult<String>;
39}
40
41pub type BackendBox = Box<dyn Backend>;