use super::{ActionApiContinuable, ActionApiData, ActionApiRunnable};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct ActionApiListAllusersData {
aufrom: Option<String>,
auto: Option<String>,
auprefix: Option<String>,
audir: Option<String>,
augroup: Option<Vec<String>>,
auexcludegroup: Option<Vec<String>>,
aurights: Option<Vec<String>>,
auprop: Option<Vec<String>>,
aulimit: usize,
auwitheditsonly: bool,
auactiveusers: bool,
}
impl ActionApiData for ActionApiListAllusersData {}
impl Default for ActionApiListAllusersData {
fn default() -> Self {
Self {
aufrom: None,
auto: None,
auprefix: None,
audir: None,
augroup: None,
auexcludegroup: None,
aurights: None,
auprop: None,
aulimit: 10,
auwitheditsonly: false,
auactiveusers: false,
}
}
}
impl ActionApiListAllusersData {
pub(crate) fn params(&self) -> HashMap<String, String> {
let mut params = HashMap::new();
Self::add_str(&self.aufrom, "aufrom", &mut params);
Self::add_str(&self.auto, "auto", &mut params);
Self::add_str(&self.auprefix, "auprefix", &mut params);
Self::add_str(&self.audir, "audir", &mut params);
Self::add_vec(&self.augroup, "augroup", &mut params);
Self::add_vec(&self.auexcludegroup, "auexcludegroup", &mut params);
Self::add_vec(&self.aurights, "aurights", &mut params);
Self::add_vec(&self.auprop, "auprop", &mut params);
params.insert("aulimit".to_string(), self.aulimit.to_string());
Self::add_boolean(self.auwitheditsonly, "auwitheditsonly", &mut params);
Self::add_boolean(self.auactiveusers, "auactiveusers", &mut params);
params
}
}
#[derive(Debug, Clone)]
pub struct ActionApiListAllusersBuilder {
pub(crate) data: ActionApiListAllusersData,
pub(crate) continue_params: HashMap<String, String>,
}
impl ActionApiListAllusersBuilder {
pub(crate) fn new() -> Self {
Self {
data: ActionApiListAllusersData::default(),
continue_params: HashMap::new(),
}
}
pub fn aufrom<S: AsRef<str>>(mut self, aufrom: S) -> Self {
self.data.aufrom = Some(aufrom.as_ref().to_string());
self
}
pub fn auto<S: AsRef<str>>(mut self, auto: S) -> Self {
self.data.auto = Some(auto.as_ref().to_string());
self
}
pub fn auprefix<S: AsRef<str>>(mut self, auprefix: S) -> Self {
self.data.auprefix = Some(auprefix.as_ref().to_string());
self
}
pub fn audir<S: AsRef<str>>(mut self, audir: S) -> Self {
self.data.audir = Some(audir.as_ref().to_string());
self
}
pub fn augroup<S: Into<String> + Clone>(mut self, augroup: &[S]) -> Self {
self.data.augroup = Some(augroup.iter().map(|s| s.clone().into()).collect());
self
}
pub fn auexcludegroup<S: Into<String> + Clone>(mut self, auexcludegroup: &[S]) -> Self {
self.data.auexcludegroup =
Some(auexcludegroup.iter().map(|s| s.clone().into()).collect());
self
}
pub fn aurights<S: Into<String> + Clone>(mut self, aurights: &[S]) -> Self {
self.data.aurights = Some(aurights.iter().map(|s| s.clone().into()).collect());
self
}
pub fn auprop<S: Into<String> + Clone>(mut self, auprop: &[S]) -> Self {
self.data.auprop = Some(auprop.iter().map(|s| s.clone().into()).collect());
self
}
pub fn aulimit(mut self, aulimit: usize) -> Self {
self.data.aulimit = aulimit;
self
}
pub fn auwitheditsonly(mut self, auwitheditsonly: bool) -> Self {
self.data.auwitheditsonly = auwitheditsonly;
self
}
pub fn auactiveusers(mut self, auactiveusers: bool) -> Self {
self.data.auactiveusers = auactiveusers;
self
}
}
impl ActionApiRunnable for ActionApiListAllusersBuilder {
fn params(&self) -> HashMap<String, String> {
let mut ret = self.data.params();
ret.insert("action".to_string(), "query".to_string());
ret.insert("list".to_string(), "allusers".to_string());
ret.extend(self.continue_params.clone());
ret
}
}
impl ActionApiContinuable for ActionApiListAllusersBuilder {
fn continue_params_mut(&mut self) -> &mut HashMap<String, String> {
&mut self.continue_params
}
}
#[cfg(test)]
mod tests {
use super::*;
fn new_builder() -> ActionApiListAllusersBuilder {
ActionApiListAllusersBuilder::new()
}
#[test]
fn default_aulimit_is_10() {
let params = new_builder().data.params();
assert_eq!(params["aulimit"], "10");
}
#[test]
fn default_aufrom_absent() {
let params = new_builder().data.params();
assert!(!params.contains_key("aufrom"));
}
#[test]
fn aufrom_set() {
let params = new_builder().aufrom("Admin").data.params();
assert_eq!(params["aufrom"], "Admin");
}
#[test]
fn auprefix_set() {
let params = new_builder().auprefix("Bot").data.params();
assert_eq!(params["auprefix"], "Bot");
}
#[test]
fn augroup_set() {
let params = new_builder().augroup(&["sysop", "bureaucrat"]).data.params();
assert_eq!(params["augroup"], "sysop|bureaucrat");
}
#[test]
fn aulimit_set() {
let params = new_builder().aulimit(50).data.params();
assert_eq!(params["aulimit"], "50");
}
#[test]
fn auwitheditsonly_flag() {
let params = new_builder().auwitheditsonly(true).data.params();
assert_eq!(params["auwitheditsonly"], "");
}
#[test]
fn runnable_params_contain_action_list() {
let params = ActionApiRunnable::params(&new_builder());
assert_eq!(params["action"], "query");
assert_eq!(params["list"], "allusers");
}
}