Skip to main content

spreadsheet_kit/recalc/
backend.rs

1use super::executor::RecalcResult;
2use anyhow::Result;
3use async_trait::async_trait;
4use std::path::Path;
5
6#[async_trait]
7pub trait RecalcBackend: Send + Sync {
8    async fn recalculate(
9        &self,
10        fork_work_path: &Path,
11        timeout_ms: Option<u64>,
12    ) -> Result<RecalcResult>;
13    fn is_available(&self) -> bool;
14    fn name(&self) -> &'static str;
15}
16
17#[cfg(feature = "recalc-libreoffice")]
18pub struct LibreOfficeBackend {
19    config: super::RecalcConfig,
20}
21
22#[cfg(feature = "recalc-libreoffice")]
23impl LibreOfficeBackend {
24    pub fn new(config: super::RecalcConfig) -> Self {
25        Self { config }
26    }
27}
28
29#[cfg(feature = "recalc-libreoffice")]
30#[async_trait]
31impl RecalcBackend for LibreOfficeBackend {
32    async fn recalculate(
33        &self,
34        fork_work_path: &Path,
35        timeout_ms: Option<u64>,
36    ) -> Result<RecalcResult> {
37        let mut config = self.config.clone();
38        if let Some(timeout_ms) = timeout_ms {
39            config.timeout_ms = Some(timeout_ms);
40        }
41        super::create_executor(&config)
42            .recalculate(fork_work_path)
43            .await
44    }
45
46    fn is_available(&self) -> bool {
47        super::create_executor(&self.config).is_available()
48    }
49
50    fn name(&self) -> &'static str {
51        "libreoffice"
52    }
53}