use super::{ActionApiContinuable, ActionApiData, ActionApiRunnable};
use crate::api::NamespaceID;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct ActionApiListAlllinksData {
alcontinue: Option<String>,
alfrom: Option<String>,
alto: Option<String>,
alprefix: Option<String>,
alunique: bool,
alprop: Option<Vec<String>>,
alnamespace: NamespaceID,
allimit: usize,
aldir: Option<String>,
}
impl ActionApiData for ActionApiListAlllinksData {}
impl Default for ActionApiListAlllinksData {
fn default() -> Self {
Self {
alcontinue: None,
alfrom: None,
alto: None,
alprefix: None,
alunique: false,
alprop: None,
alnamespace: 0,
allimit: 10,
aldir: None,
}
}
}
impl ActionApiListAlllinksData {
pub(crate) fn params(&self) -> HashMap<String, String> {
let mut params = HashMap::new();
Self::add_str(&self.alcontinue, "alcontinue", &mut params);
Self::add_str(&self.alfrom, "alfrom", &mut params);
Self::add_str(&self.alto, "alto", &mut params);
Self::add_str(&self.alprefix, "alprefix", &mut params);
Self::add_boolean(self.alunique, "alunique", &mut params);
Self::add_vec(&self.alprop, "alprop", &mut params);
params.insert("alnamespace".to_string(), self.alnamespace.to_string());
params.insert("allimit".to_string(), self.allimit.to_string());
Self::add_str(&self.aldir, "aldir", &mut params);
params
}
}
#[derive(Debug, Clone)]
pub struct ActionApiListAlllinksBuilder {
pub(crate) data: ActionApiListAlllinksData,
pub(crate) continue_params: HashMap<String, String>,
}
impl ActionApiListAlllinksBuilder {
pub(crate) fn new() -> Self {
Self {
data: ActionApiListAlllinksData::default(),
continue_params: HashMap::new(),
}
}
pub fn alfrom<S: AsRef<str>>(mut self, alfrom: S) -> Self {
self.data.alfrom = Some(alfrom.as_ref().to_string());
self
}
pub fn alto<S: AsRef<str>>(mut self, alto: S) -> Self {
self.data.alto = Some(alto.as_ref().to_string());
self
}
pub fn alprefix<S: AsRef<str>>(mut self, alprefix: S) -> Self {
self.data.alprefix = Some(alprefix.as_ref().to_string());
self
}
pub fn alunique(mut self, alunique: bool) -> Self {
self.data.alunique = alunique;
self
}
pub fn alprop<S: Into<String> + Clone>(mut self, alprop: &[S]) -> Self {
self.data.alprop = Some(alprop.iter().map(|s| s.clone().into()).collect());
self
}
pub fn alnamespace(mut self, alnamespace: NamespaceID) -> Self {
self.data.alnamespace = alnamespace;
self
}
pub fn allimit(mut self, allimit: usize) -> Self {
self.data.allimit = allimit;
self
}
pub fn aldir<S: AsRef<str>>(mut self, aldir: S) -> Self {
self.data.aldir = Some(aldir.as_ref().to_string());
self
}
}
impl ActionApiRunnable for ActionApiListAlllinksBuilder {
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(), "alllinks".to_string());
ret.extend(self.continue_params.clone());
ret
}
}
impl ActionApiContinuable for ActionApiListAlllinksBuilder {
fn continue_params_mut(&mut self) -> &mut HashMap<String, String> {
&mut self.continue_params
}
}
#[cfg(test)]
mod tests {
use super::*;
fn new_builder() -> ActionApiListAlllinksBuilder {
ActionApiListAlllinksBuilder::new()
}
#[test]
fn default_alnamespace_is_0() {
let params = new_builder().data.params();
assert_eq!(params["alnamespace"], "0");
}
#[test]
fn default_allimit_is_10() {
let params = new_builder().data.params();
assert_eq!(params["allimit"], "10");
}
#[test]
fn alprefix_set() {
let params = new_builder().alprefix("Albert").data.params();
assert_eq!(params["alprefix"], "Albert");
}
#[test]
fn alnamespace_set() {
let params = new_builder().alnamespace(4).data.params();
assert_eq!(params["alnamespace"], "4");
}
#[test]
fn allimit_set() {
let params = new_builder().allimit(50).data.params();
assert_eq!(params["allimit"], "50");
}
#[test]
fn alunique_flag() {
let params = new_builder().alunique(true).data.params();
assert_eq!(params["alunique"], "");
}
#[test]
fn runnable_params_contain_action_list() {
let params = ActionApiRunnable::params(&new_builder());
assert_eq!(params["action"], "query");
assert_eq!(params["list"], "alllinks");
}
}