diff options
Diffstat (limited to 'src/client/client_builder.rs')
-rw-r--r-- | src/client/client_builder.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/client/client_builder.rs b/src/client/client_builder.rs new file mode 100644 index 0000000..eaad59f --- /dev/null +++ b/src/client/client_builder.rs @@ -0,0 +1,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) + } +} |