summaryrefslogtreecommitdiffstats
path: root/src/client/client_builder.rs
blob: eaad59f2f63141ccf11dff2fcd0b9fce625cc27a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::raw_ptr::{LogLevel, TdPtr};
use crate::update::Handler;

/// Build client with some options.
///
/// This builder will execute functions as soon as builder functions will be called
///
/// e.g. if you call [`ClientBuilder::log_level`], it will immediately call
/// underlying API to change logging in TDLib, as it will be done globally
/// disregarding actual TDLib client being used, if any
#[derive(Debug)]
pub struct ClientBuilder<H: Handler> {
    // need Option here to transfer ownership to client, when building
    handler: Option<H>,
    timeout: f64,
}

impl<H: Handler> ClientBuilder<H> {
    /// Create new `ClientBuilder` with update handler
    pub fn new(handler: H) -> Self {
        Self {
            handler: Some(handler),
            timeout: 10.0,
        }
    }

    /// Set up internal TDLib logging level.
    ///
    /// See also [`LogLevel`] doc for explanation of logging levels.
    /// By default, logging level will be [`LogLevel::Verbose`]
    pub fn log_level<'a>(&'a mut self, level: LogLevel) -> &'a mut Self {
        TdPtr::set_log_verbosity_level(level);
        self
    }

    /// Set up internal TDLib logging file size.
    /// Sets the maximum size of the file to where the internal TDLib log is written
    /// before the file will be auto-rotated. Unused if log is not written to a file.
    /// Defaults to 10 MB.
    /// Should be positive.
    pub fn log_max_size(&mut self, size: i64) -> &mut Self {
        TdPtr::set_log_max_file_size(size);
        self
    }

    /// Sets the path to the file where the internal TDLib log will be written.
    /// By default TDLib writes logs to stderr or an OS specific log.
    /// Use this method to write the log to a file instead.
    pub fn log_file<S: AsRef<str>>(&mut self, file: S) -> &mut Self {
        TdPtr::set_log_file_path(file).unwrap();
        self
    }

    /// Reset file of internal TDLib logging.
    /// TDLib will revert to default logging behaviour (log to stderr)
    pub fn log_reset_file(&mut self) -> &mut Self {
        TdPtr::reset_log_file_path().unwrap();
        self
    }

    /// Set timeout for receiver.
    /// Usually, you do not want to customize this
    pub fn receive_timeout(&mut self, timeout: f64) -> &mut Self {
        self.timeout = timeout;
        self
    }

    /// Build client
    pub fn build(&mut self) -> crate::client::Client {
        // this unwrap is safe, because handler is always Some in `new`
        // and cannot be mutated during building
        crate::Client::new(self.handler.take().unwrap(), self.timeout)
    }
}