kroki_rs/interface/
mapping.rs1use crate::diagrams::error::DiagramError;
2use crate::interface::dtos::{RenderRequestDto, RenderResponseDto};
3
4pub struct DiagramRequest {
6 pub source: String,
7 pub format: String,
8 pub provider: String,
9}
10
11impl TryFrom<RenderRequestDto> for DiagramRequest {
12 type Error = DiagramError;
13
14 fn try_from(dto: RenderRequestDto) -> Result<Self, Self::Error> {
15 if dto.source.is_empty() {
16 return Err(DiagramError::ValidationFailed(
17 "Source content cannot be empty".to_string(),
18 ));
19 }
20
21 Ok(Self {
22 source: dto.source,
23 format: dto.format.ok_or_else(|| {
24 DiagramError::ValidationFailed("Format must be specified".to_string())
25 })?,
26 provider: dto.provider.ok_or_else(|| {
27 DiagramError::ValidationFailed("Provider must be specified".to_string())
28 })?,
29 })
30 }
31}
32
33impl RenderResponseDto {
34 pub fn success(result: Vec<u8>, content_type: &str, duration_ms: u64) -> Self {
35 Self {
36 result: if content_type.starts_with("text/") || content_type.contains("svg") {
37 String::from_utf8_lossy(&result).to_string()
38 } else {
39 base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &result)
40 },
41 content_type: content_type.to_string(),
42 duration_ms,
43 }
44 }
45}