1use std::collections::BTreeSet;
20use std::path::Path;
21
22use capsule_core::media::{EmbeddedPreview, extract_embedded_preview};
23
24use super::FfiError;
25use crate::fetch::{
26 LocalHoldings, NextStep, PixelSize, RenderRequest, RenderStep, Rendition, Representation,
27 RungSource,
28};
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
32pub enum FfiRepresentation {
33 Lqip,
35 Thumbnail,
37 Preview,
39 Original,
41}
42
43impl From<FfiRepresentation> for Representation {
44 fn from(rung: FfiRepresentation) -> Self {
45 match rung {
46 FfiRepresentation::Lqip => Self::Lqip,
47 FfiRepresentation::Thumbnail => Self::Thumbnail,
48 FfiRepresentation::Preview => Self::Preview,
49 FfiRepresentation::Original => Self::Original,
50 }
51 }
52}
53
54impl From<Representation> for FfiRepresentation {
55 fn from(rung: Representation) -> Self {
56 match rung {
57 Representation::Lqip => Self::Lqip,
58 Representation::Thumbnail => Self::Thumbnail,
59 Representation::Preview => Self::Preview,
60 Representation::Original => Self::Original,
61 }
62 }
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
67pub enum FfiRungSource {
68 Lqip,
70 Embedded,
72 Encoded,
74 Original,
76}
77
78impl From<FfiRungSource> for RungSource {
79 fn from(source: FfiRungSource) -> Self {
80 match source {
81 FfiRungSource::Lqip => Self::Lqip,
82 FfiRungSource::Embedded => Self::Embedded,
83 FfiRungSource::Encoded => Self::Encoded,
84 FfiRungSource::Original => Self::Original,
85 }
86 }
87}
88
89impl From<RungSource> for FfiRungSource {
90 fn from(source: RungSource) -> Self {
91 match source {
92 RungSource::Lqip => Self::Lqip,
93 RungSource::Embedded => Self::Embedded,
94 RungSource::Encoded => Self::Encoded,
95 RungSource::Original => Self::Original,
96 }
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)]
103pub struct FfiRendition {
104 pub rung: FfiRepresentation,
106 pub source: FfiRungSource,
108}
109
110impl From<FfiRendition> for Rendition {
111 fn from(shown: FfiRendition) -> Self {
112 Self {
113 rung: shown.rung.into(),
114 source: shown.source.into(),
115 }
116 }
117}
118
119impl From<Rendition> for FfiRendition {
120 fn from(shown: Rendition) -> Self {
121 Self {
122 rung: shown.rung.into(),
123 source: shown.source.into(),
124 }
125 }
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)]
130pub struct FfiRenderRequest {
131 pub intent: FfiRepresentation,
134 pub surface_width: u32,
136 pub surface_height: u32,
138}
139
140#[derive(Debug, Clone, Default, PartialEq, Eq, uniffi::Record)]
143pub struct FfiLocalHoldings {
144 pub held: Vec<FfiRepresentation>,
146 pub embedded_preview_width: Option<u32>,
148 pub embedded_preview_height: Option<u32>,
150}
151
152#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
154pub enum FfiNextStep {
155 Fetch {
157 representation: FfiRepresentation,
159 },
160 Generate {
162 representation: FfiRepresentation,
164 },
165}
166
167impl From<NextStep> for FfiNextStep {
168 fn from(next: NextStep) -> Self {
169 match next {
170 NextStep::Fetch(rung) => Self::Fetch {
171 representation: rung.into(),
172 },
173 NextStep::Generate(rung) => Self::Generate {
174 representation: rung.into(),
175 },
176 }
177 }
178}
179
180#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)]
182pub struct FfiRenderStep {
183 pub show: FfiRendition,
185 pub changed: bool,
187 pub next: Option<FfiNextStep>,
189}
190
191impl From<RenderStep> for FfiRenderStep {
192 fn from(step: RenderStep) -> Self {
193 Self {
194 show: step.show.into(),
195 changed: step.changed,
196 next: step.next.map(Into::into),
197 }
198 }
199}
200
201#[derive(Clone, PartialEq, Eq, uniffi::Record)]
203pub struct FfiEmbeddedPreview {
204 pub jpeg: Vec<u8>,
206 pub width: u32,
208 pub height: u32,
210 pub orientation: u16,
213}
214
215impl std::fmt::Debug for FfiEmbeddedPreview {
216 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
218 f.debug_struct("FfiEmbeddedPreview")
219 .field("bytes", &self.jpeg.len())
220 .field("width", &self.width)
221 .field("height", &self.height)
222 .field("orientation", &self.orientation)
223 .finish()
224 }
225}
226
227impl From<EmbeddedPreview> for FfiEmbeddedPreview {
228 fn from(preview: EmbeddedPreview) -> Self {
229 Self {
230 jpeg: preview.jpeg,
231 width: preview.width,
232 height: preview.height,
233 orientation: preview.orientation,
234 }
235 }
236}
237
238#[uniffi::export]
242#[must_use]
243pub fn render_step(
244 request: FfiRenderRequest,
245 shown: Option<FfiRendition>,
246 local: FfiLocalHoldings,
247 fetchable: Vec<FfiRepresentation>,
248) -> FfiRenderStep {
249 let request = RenderRequest {
250 intent: request.intent.into(),
251 surface: PixelSize {
252 width: request.surface_width,
253 height: request.surface_height,
254 },
255 };
256 let local = LocalHoldings {
257 held: local.held.into_iter().map(Into::into).collect(),
258 embedded_preview: local
259 .embedded_preview_width
260 .zip(local.embedded_preview_height)
261 .map(|(width, height)| PixelSize { width, height }),
262 };
263 let fetchable: BTreeSet<Representation> = fetchable.into_iter().map(Into::into).collect();
264 crate::fetch::render_step(&request, shown.map(Into::into), &local, &fetchable).into()
265}
266
267#[uniffi::export]
282#[tracing::instrument(level = "debug", skip(original_path))]
283pub fn embedded_preview(
284 original_path: String,
285 ext: String,
286) -> Result<Option<FfiEmbeddedPreview>, FfiError> {
287 let path = Path::new(&original_path);
288 let bytes = std::fs::read(path).map_err(|e| FfiError::Workspace {
289 detail: format!("reading the original {}: {e}", path.display()),
290 })?;
291 Ok(embedded_preview_of(&bytes, &ext))
292}
293
294fn embedded_preview_of(bytes: &[u8], ext: &str) -> Option<FfiEmbeddedPreview> {
296 match extract_embedded_preview(bytes, ext) {
297 Ok(preview) => preview.map(Into::into),
298 Err(error) => {
299 tracing::info!(%error, ext, "ffi: no embedded preview; the rung falls to the next source");
300 None
301 }
302 }
303}
304
305#[cfg(test)]
306mod tests;