1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
use crate::enums::{Duration, SearchSort, SearchTarget};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct IllustrationSearchRequestArg {
    word: String,
    search_target: SearchTarget,
    sort: SearchSort,
    duration: Option<Duration>,
    offset: Option<u32>,
    filter: Option<&'static str>,
}

impl IllustrationSearchRequestArg {
    pub fn new<T>(word: T) -> Self
    where
        T: Into<String>,
    {
        IllustrationSearchRequestArg {
            word: word.into(),
            search_target: SearchTarget::TagsPartial,
            sort: SearchSort::DateDescending,
            duration: None,
            offset: None,
            filter: None,
        }
    }

    pub fn set_search_target<T>(mut self, search_target: T) -> Self
    where
        T: Into<SearchTarget>,
    {
        self.search_target = search_target.into();
        self
    }

    pub fn set_sort<T>(mut self, sort: T) -> Self
    where
        T: Into<SearchSort>,
    {
        self.sort = sort.into();
        self
    }

    pub fn set_duration<T>(mut self, duration: T) -> Self
    where
        T: Into<Duration>,
    {
        self.duration = Some(duration.into());
        self
    }

    pub fn set_offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset.into());
        self
    }
}

impl IntoIterator for IllustrationSearchRequestArg {
    type Item = (&'static str, String);
    type IntoIter = IllustrationSearchParamIterator;

    fn into_iter(self) -> Self::IntoIter {
        IllustrationSearchParamIterator {
            vec: self,
            index: 0,
        }
    }
}

pub struct IllustrationSearchParamIterator {
    vec: IllustrationSearchRequestArg,
    index: usize,
}

// TODO: Remove this crap.
impl Iterator for IllustrationSearchParamIterator {
    type Item = (&'static str, String);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let result = match self.index {
                0 => Some(("word", self.vec.word.clone())),
                1 => Some(("search_target", self.vec.search_target.as_str().to_string())),
                2 => Some(("sort", self.vec.sort.as_str().to_string())),
                3 => self
                    .vec
                    .duration
                    .take()
                    .map(|x| ("duration", x.as_str().to_string())),
                4 => self.vec.offset.take().map(|x| ("offset", x.to_string())),
                5 => self.vec.filter.take().map(|x| ("filter", x.to_string())),
                _ => return None,
            };
            self.index += 1;
            if let Some(r) = result {
                return Some(r);
            }
        }
    }
}