fusio_log/
option.rs

1use std::sync::Arc;
2
3use fusio::DynFs;
4pub use fusio_dispatch::FsOptions;
5use futures_core::TryStream;
6
7use crate::{error::LogError, Decode, Encode, Logger};
8
9const DEFAULT_BUF_SIZE: usize = 4 * 1024;
10
11#[derive(Clone)]
12pub struct Options {
13    pub(crate) path: fusio::path::Path,
14    pub(crate) buf_size: usize,
15    pub(crate) fs_option: FsOptions,
16    pub(crate) truncate: bool,
17}
18
19impl Options {
20    pub fn new(path: fusio::path::Path) -> Self {
21        Self {
22            path,
23            buf_size: DEFAULT_BUF_SIZE,
24            fs_option: FsOptions::Local,
25            truncate: false,
26        }
27    }
28
29    pub fn disable_buf(self) -> Self {
30        Self {
31            buf_size: 0,
32            ..self
33        }
34    }
35
36    pub fn buf_size(self, buf_size: usize) -> Self {
37        Self { buf_size, ..self }
38    }
39
40    pub fn fs(self, fs_option: FsOptions) -> Self {
41        Self { fs_option, ..self }
42    }
43
44    pub fn truncate(self, truncate: bool) -> Self {
45        Self { truncate, ..self }
46    }
47
48    pub async fn build<T>(self) -> Result<Logger<T>, LogError>
49    where
50        T: Encode,
51    {
52        let logger = Logger::<T>::new(self).await?;
53        Ok(logger)
54    }
55
56    pub async fn build_with_fs<T>(self, fs: Arc<dyn DynFs>) -> Result<Logger<T>, LogError>
57    where
58        T: Encode,
59    {
60        let logger = Logger::<T>::with_fs(fs, self).await?;
61        Ok(logger)
62    }
63
64    pub async fn recover<T>(
65        self,
66    ) -> Result<impl TryStream<Ok = Vec<T>, Error = LogError> + Unpin, LogError>
67    where
68        T: Decode,
69    {
70        Logger::<T>::recover(self).await
71    }
72}