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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
pub mod character;
pub mod producer;
pub mod release;
pub mod staff;
pub mod ulist;
pub mod ulistlabels;
pub mod user;
pub mod vn;

use super::error::{VndbError, VndbResult};
use character::GetCharacterResponse;
use producer::GetProducerResponse;
use release::GetReleaseResponse;
use serde::{Deserialize, Serialize};
use serde_repr::Deserialize_repr;
use staff::GetStaffResponse;
use strum_macros::AsRefStr;
use ulist::GetUListResponse;
use ulistlabels::GetUListLabelsResponse;
use user::GetUserResponse;
use vn::GetVnResponse;

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub(crate) enum GetResponse {
    Vn(Box<GetVnResponse>),
    Release(Box<GetReleaseResponse>),
    Producer(Box<GetProducerResponse>),
    Character(Box<GetCharacterResponse>),
    Staff(Box<GetStaffResponse>),
    User(Box<GetUserResponse>),
    UListLabels(Box<GetUListLabelsResponse>),
    UList(Box<GetUListResponse>),
}

/// Describes number of items return
#[derive(Deserialize, Debug, PartialEq)]
pub struct Results {
    pub num: usize,
    pub more: bool,
}

#[derive(AsRefStr, Debug)]
#[strum(serialize_all = "lowercase")]
pub(crate) enum GetType {
    Vn,
    Release,
    Producer,
    Character,
    Staff,
    User,
    #[strum(serialize = "ulist-labels")]
    UlistLabels,
    Ulist,
}

#[derive(AsRefStr, Clone, Copy, Debug)]
#[strum(serialize_all = "lowercase")]
pub enum GetFlag {
    Basic,
    Details,
    Anime,
    Relations,
    Tags,
    Stats,
    Screens,
    Staff,
    Vn,
    Producers,
    #[strum(serialize = "meas")]
    Measures,
    Traits,
    Vns,
    Voiced,
    Instances,
    Aliases,
    Labels,
}

#[derive(Debug)]
pub(crate) struct GetRequest<'a> {
    get_type: GetType,
    flags: &'a [GetFlag],
    filters: String,
    options: Option<Options>,
}

impl<'a> GetRequest<'a> {
    pub fn new(
        get_type: GetType,
        flags: &'a [GetFlag],
        filters: String,
        options: Option<Options>,
    ) -> Self {
        Self {
            get_type,
            flags,
            filters,
            options,
        }
    }
    pub(crate) fn to_request(&self) -> VndbResult<String> {
        let mut flags = String::new();
        for flag in self.flags {
            flags += flag.as_ref();
            flags += ",";
        }
        let options = match &self.options {
            Some(o) => match serde_json::to_string(&o) {
                Ok(de) => de,
                Err(err) => {
                    return Err(VndbError::Other {
                        msg: err.to_string(),
                    })
                }
            },
            None => String::default(),
        };
        Ok(format!(
            "{} {} {} {}",
            self.get_type.as_ref(),
            flags,
            self.filters,
            options
        ))
    }
}

/// The options argument is optional, and influences the behaviour of the returned results.
#[derive(Serialize, Debug)]
pub struct Options {
    /// Page 1 (the default) returns the first 10 results (1-10), page 2 returns the following 10 (11-20), etc.
    #[serde(skip_serializing_if = "Option::is_none")]
    page: Option<usize>,
    /// Maximum number of results to return.
    /// Also affects the "page" option above.
    /// For example: with "page" set to 2 and "results" set to 5, the second five results (that is, results 6-10) will be returned.
    /// Default: 10.
    #[serde(skip_serializing_if = "Option::is_none")]
    results: Option<usize>,
    /// The field to order the results by.
    /// The accepted field names differ per type.
    /// The default sort field is the ID of the database entry.
    #[serde(skip_serializing_if = "Option::is_none")]
    sort: Option<String>,
    /// Set to true to reverse the order of the results.
    /// Default false.
    #[serde(skip_serializing_if = "Option::is_none")]
    reverse: Option<bool>,
}

impl Options {
    pub fn new(
        page: Option<usize>,
        results: Option<usize>,
        sort: Option<String>,
        reverse: Option<bool>,
    ) -> Self {
        Self {
            page,
            results,
            sort,
            reverse,
        }
    }
}

#[repr(u8)]
#[derive(Deserialize_repr, Debug, PartialEq)]
pub enum SpoilerLevel {
    None = 0,
    Minor = 1,
    Major = 2,
}

#[derive(Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Gender {
    M,
    F,
    Both,
}