Skip to main content

katana_render_runtime/renderer/
api.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3use thiserror::Error;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub enum RenderKind {
7    Mermaid,
8    Drawio,
9    PlantUml,
10    MathJax,
11}
12
13pub type DiagramKind = RenderKind;
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct RuntimeVersion {
17    pub name: String,
18    pub version: String,
19    pub checksum: Option<String>,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct RendererProfile {
24    pub id: String,
25    pub description: Option<String>,
26}
27
28#[derive(Debug, Clone, Default, Serialize, Deserialize)]
29pub struct RenderConfig {
30    pub vendor_config: serde_json::Value,
31}
32
33#[derive(Debug, Clone, Default, Serialize, Deserialize)]
34pub struct RenderPolicy {
35    pub max_width: Option<u32>,
36    pub max_height: Option<u32>,
37    pub padding: Option<u32>,
38    pub background: Option<String>,
39    pub cache_profile: Option<String>,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
43pub enum RenderThemeMode {
44    Light,
45    Dark,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
49pub struct RenderThemeSnapshot {
50    pub mode: RenderThemeMode,
51    pub background: String,
52    pub text: String,
53    pub fill: String,
54    pub stroke: String,
55    pub arrow: String,
56    pub drawio_label_color: String,
57    pub mermaid_theme: String,
58    pub plantuml_class_bg: String,
59    pub plantuml_note_bg: String,
60    pub plantuml_note_text: String,
61    pub syntax_theme_dark: String,
62    pub syntax_theme_light: String,
63    pub preview_text: String,
64}
65
66#[derive(Debug, Clone, Default, Serialize, Deserialize)]
67pub struct RenderContext {
68    /// `theme` がある場合は描画と cache fingerprint の正とし、既存の文字列 fingerprint は識別補助として残す。
69    pub theme_fingerprint: Option<String>,
70    pub document_id: Option<String>,
71    pub theme: Option<RenderThemeSnapshot>,
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct RenderInput {
76    pub kind: RenderKind,
77    pub source: String,
78    pub config: RenderConfig,
79    pub policy: RenderPolicy,
80    pub context: RenderContext,
81}
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct RenderDiagnostics {
85    pub warnings: Vec<String>,
86    pub errors: Vec<String>,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct RenderOutput {
91    pub svg: String,
92    pub width: f32,
93    pub height: f32,
94    pub view_box: String,
95    pub runtime: RuntimeVersion,
96    pub profile: RendererProfile,
97    pub diagnostics: RenderDiagnostics,
98    pub cache_fingerprint: String,
99}
100
101#[derive(Debug, Error)]
102pub enum RenderError {
103    #[error("invalid input: {0}")]
104    InvalidInput(String),
105    #[error("{kind} runtime is not installed: {install_path}")]
106    NotInstalled {
107        kind: String,
108        download_url: String,
109        install_path: PathBuf,
110    },
111    #[error("runtime error: {0}")]
112    Runtime(String),
113    #[error("runtime path resolution failed: {0}")]
114    RuntimeResolution(String),
115    #[error("unsupported diagram kind")]
116    UnsupportedKind,
117}
118
119pub trait Renderer {
120    fn render(&self, input: &RenderInput) -> Result<RenderOutput, RenderError>;
121}