mocklogger/submit1/
builder.rs

1// Author: D.S. Ljungmark <spider@skuggor.se>, Modio AB
2// SPDX-License-Identifier: AGPL-3.0-or-later
3use crate::logger::LogErr;
4use crate::timefail::Timefail;
5use modio_logger_db::{Datastore, SqlitePool};
6
7use super::Submit1;
8
9pub struct Builder {
10    ds: Option<Datastore>,
11    development: bool,
12    customer: bool,
13}
14
15impl Default for Builder {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl Builder {
22    /// New builder
23    #[must_use]
24    pub const fn new() -> Self {
25        Self {
26            ds: None,
27            development: false,
28            customer: false,
29        }
30    }
31
32    /// Set the datastore
33    ///
34    /// If this is not called, it will use a temporary datastore
35    #[must_use]
36    pub fn datastore(mut self, ds: Datastore) -> Self {
37        self.ds = Some(ds);
38        self
39    }
40
41    /// Set the datastore from a pool
42    ///
43    /// Replaces the datastore with one based from the pool
44    pub async fn datastore_pool(mut self, p: &SqlitePool) -> Self {
45        let pool = p.clone();
46        let ds = Datastore::new(pool)
47            .await
48            .expect("Failed to make datastore");
49        self.ds = Some(ds);
50        self
51    }
52
53    /// Set development mode to true/false
54    ///
55    /// default is false
56    #[must_use]
57    pub const fn development(mut self, val: bool) -> Self {
58        self.development = val;
59        self
60    }
61
62    /// Set customer mode to true/false
63    ///
64    /// default is false
65    #[must_use]
66    pub const fn customer(mut self, val: bool) -> Self {
67        self.customer = val;
68        self
69    }
70
71    pub async fn build(self) -> Result<Submit1, LogErr> {
72        let tf = if self.development {
73            Timefail::development()
74        } else {
75            Timefail::default()
76        };
77
78        let datastore = if let Some(ds) = self.ds {
79            ds
80        } else {
81            Datastore::temporary().await
82        };
83        let res = Submit1::new(tf, datastore, self.customer).await?;
84        Ok(res)
85    }
86}