mwbot/
builder.rs

1/*
2Copyright (C) 2022 Kunal Mehta <legoktm@debian.org>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18use crate::config::{Auth, Config};
19
20/// Build a `mwbot::Bot` instance programmatically
21pub struct Builder {
22    config: Config,
23}
24
25impl Builder {
26    pub(crate) fn new(api_url: String, rest_url: String) -> Self {
27        Self {
28            config: Config {
29                api_url,
30                rest_url,
31                auth: None,
32                general: Default::default(),
33                edit: Default::default(),
34            },
35        }
36    }
37
38    pub fn set_botpassword(
39        mut self,
40        username: String,
41        password: String,
42    ) -> Self {
43        self.config.auth = Some(Auth::BotPassword { username, password });
44        self
45    }
46
47    pub fn set_oauth2_token(mut self, username: String, token: String) -> Self {
48        self.config.auth = Some(Auth::OAuth2 {
49            username,
50            oauth2_token: token,
51        });
52        self
53    }
54
55    pub fn set_maxlag(mut self, maxlag: u32) -> Self {
56        self.config.general.maxlag = Some(maxlag);
57        self
58    }
59
60    pub fn set_retry_limit(mut self, retry_limit: u32) -> Self {
61        self.config.general.retry_limit = Some(retry_limit);
62        self
63    }
64
65    /// This adds the provided string to the User-agent, it does not
66    /// override the default generated one.
67    pub fn set_user_agent(mut self, user_agent: String) -> Self {
68        self.config.general.user_agent = Some(user_agent);
69        self
70    }
71
72    pub fn set_mark_as_bot(mut self, mark_as_bot: bool) -> Self {
73        self.config.edit.mark_as_bot = Some(mark_as_bot);
74        self
75    }
76
77    pub fn set_save_delay(mut self, save_delay: u64) -> Self {
78        self.config.edit.save_delay = Some(save_delay);
79        self
80    }
81
82    pub fn set_respect_nobots(mut self, respect_nobots: bool) -> Self {
83        self.config.edit.respect_nobots = Some(respect_nobots);
84        self
85    }
86
87    pub async fn build(self) -> Result<crate::Bot, crate::ConfigError> {
88        crate::Bot::from_config(self.config).await
89    }
90}