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 { // need Option here to transfer ownership to client, when building handler: Option, timeout: f64, } impl ClientBuilder { /// 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>(&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) } }