kaggle/
request.rs

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use serde::Serialize;

use crate::query::{
    CompetitionCategory,
    CompetitionGroup,
    CompetitionSortBy,
    DatasetFileType,
    DatasetGroup,
    DatasetLicenseName,
    Group,
    KernelType,
    Language,
    OutputType,
    SortBy,
};
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompetitionsList {
    /// Group to filter result to
    #[serde(with = "crate::none_as_empty")]
    group: Option<CompetitionGroup>,
    /// Category to filter result to
    #[serde(with = "crate::none_as_empty")]
    category: Option<CompetitionCategory>,
    /// How to sort the result
    #[serde(with = "crate::none_as_empty")]
    sort_by: Option<CompetitionSortBy>,
    /// The page to return.
    page: usize,
    /// Search term to use (default is empty string)
    #[serde(with = "crate::none_as_empty")]
    search: Option<String>,
}

impl CompetitionsList {
    pub fn new(page: usize) -> Self {
        Self {
            group: None,
            category: None,
            sort_by: None,
            page,
            search: None,
        }
    }

    pub fn group(mut self, group: CompetitionGroup) -> Self {
        self.group = Some(group);
        self
    }

    pub fn category(mut self, category: CompetitionCategory) -> Self {
        self.category = Some(category);
        self
    }

    pub fn sort_by(mut self, sort_by: CompetitionSortBy) -> Self {
        self.sort_by = Some(sort_by);
        self
    }

    pub fn search(mut self, search: impl ToString) -> Self {
        self.search = Some(search.to_string());
        self
    }
}

impl Default for CompetitionsList {
    fn default() -> Self {
        Self::new(1)
    }
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct KernelsList {
    /// The page to return.
    page: usize,
    /// Results per page, defaults to 20
    page_size: usize,
    /// Filter to this dataset
    #[serde(with = "crate::none_as_empty")]
    dataset: Option<String>,
    /// Filter to this competition
    #[serde(with = "crate::none_as_empty")]
    competition: Option<String>,
    /// Filter to those with specified parent
    #[serde(with = "crate::none_as_empty")]
    parent_kernel: Option<String>,
    /// A custom search string to pass to the list query
    #[serde(with = "crate::none_as_empty")]
    search: Option<String>,
    /// whit kind of kernels to return
    group: Group,
    /// Filter results to a specific user
    #[serde(with = "crate::none_as_empty")]
    user: Option<String>,
    /// The programming language of the kernel
    language: Language,
    /// The type of kernel
    kernel_type: KernelType,
    /// The output type
    output_type: OutputType,
    /// Sort results by this string
    sort_by: SortBy,
}

impl Default for KernelsList {
    fn default() -> Self {
        Self::with_page(1)
    }
}

impl KernelsList {
    pub fn with_page(page: usize) -> Self {
        Self {
            page,
            page_size: 20,
            dataset: None,
            competition: None,
            parent_kernel: None,
            search: None,
            group: Default::default(),
            user: None,
            language: Default::default(),
            kernel_type: Default::default(),
            output_type: Default::default(),
            sort_by: Default::default(),
        }
    }

    pub fn page_size(mut self, page_size: usize) -> Self {
        self.page_size = page_size;
        self
    }

    pub fn mine(mut self, group: Group) -> Self {
        self.group = group;
        self
    }

    pub fn dataset(mut self, dataset: impl ToString) -> Self {
        self.dataset = Some(dataset.to_string());
        self
    }

    pub fn competition(mut self, competition: impl ToString) -> Self {
        self.competition = Some(competition.to_string());
        self
    }

    pub fn parent_kernel(mut self, parent_kernel: impl ToString) -> Self {
        self.parent_kernel = Some(parent_kernel.to_string());
        self
    }

    pub fn search(mut self, search: impl ToString) -> Self {
        self.search = Some(search.to_string());
        self
    }

    pub fn user(mut self, user: impl ToString) -> Self {
        self.user = Some(user.to_string());
        self
    }

    pub fn language(mut self, language: Language) -> Self {
        self.language = language;
        self
    }

    pub fn kernel_type(mut self, kernel_type: KernelType) -> Self {
        self.kernel_type = kernel_type;
        self
    }

    pub fn output_type(mut self, output_type: OutputType) -> Self {
        self.output_type = output_type;
        self
    }

    pub fn sort_by(mut self, sort_by: SortBy) -> Self {
        self.sort_by = sort_by;
        self
    }
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DatasetsList {
    /// Display datasets by a particular group
    group: DatasetGroup,
    /// How to sort the result, see valid_dataset_sort_bys for options
    sort_by: SortBy,
    /// The format, see valid_dataset_file_types for string options
    filetype: DatasetFileType,
    /// Tag identifiers to filter the search
    #[serde(with = "crate::none_as_empty")]
    tagids: Option<String>,
    /// Descriptor for the license
    license: DatasetLicenseName,
    /// Search term to use (default is empty string)
    #[serde(with = "crate::none_as_empty")]
    search: Option<String>,
    /// Display datasets by a specific user or organization
    #[serde(with = "crate::none_as_empty")]
    user: Option<String>,
    /// The page to return.
    page: usize,
    /// The maximum size of the dataset to return
    #[serde(skip_serializing_if = "Option::is_none")]
    max_size: Option<usize>,
    /// The minimum size of the dataset to return
    #[serde(skip_serializing_if = "Option::is_none")]
    min_size: Option<usize>,
}

impl DatasetsList {
    pub fn with_page(page: usize) -> Self {
        Self {
            page,
            sort_by: SortBy::Hotness,
            filetype: DatasetFileType::All,
            tagids: None,
            license: DatasetLicenseName::All,
            search: None,
            user: None,
            max_size: None,
            min_size: None,
            group: DatasetGroup::default(),
        }
    }

    pub fn page(mut self, page: usize) -> Self {
        self.page = page;
        self
    }

    pub fn max_size(mut self, max_size: usize) -> Self {
        self.max_size = Some(max_size);
        self
    }

    pub fn min_size(mut self, min_size: usize) -> Self {
        self.min_size = Some(min_size);
        self
    }

    pub fn file_type(mut self, file_type: DatasetFileType) -> Self {
        self.filetype = file_type;
        self
    }

    pub fn search(mut self, search: impl ToString) -> Self {
        self.search = Some(search.to_string());
        self
    }

    pub fn user(mut self, user: impl ToString) -> Self {
        self.user = Some(user.to_string());
        self.group = DatasetGroup::User;
        self
    }

    pub fn mine(mut self) -> Self {
        self.user = None;
        self.group = DatasetGroup::My;
        self
    }

    pub fn public(mut self) -> Self {
        self.user = None;
        self.group = DatasetGroup::Public;
        self
    }

    pub fn license_name(mut self, license_name: DatasetLicenseName) -> Self {
        self.license = license_name;
        self
    }

    pub fn sort_by(mut self, sort_by: SortBy) -> Self {
        self.sort_by = sort_by;
        self
    }

    pub fn tag_ids(mut self, tag_ids: impl ToString) -> Self {
        self.tagids = Some(tag_ids.to_string());
        self
    }
}

impl Default for DatasetsList {
    fn default() -> Self {
        Self::with_page(1)
    }
}

#[derive(Debug, Clone)]
pub struct KernelPullRequest {
    pub with_metadata: bool,
    pub name: String,
    pub output: Option<PathBuf>,
}

impl KernelPullRequest {
    pub fn new(name: impl ToString) -> Self {
        Self {
            with_metadata: false,
            name: name.to_string(),
            output: None,
        }
    }

    pub fn output(mut self, output: impl AsRef<Path>) -> Self {
        self.output = Some(output.as_ref().to_path_buf());
        self
    }

    pub fn with_metadata(mut self, with_metadata: bool) -> Self {
        self.with_metadata = with_metadata;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ser_empty() {
        #[derive(Serialize)]
        struct Dummy {
            #[serde(with = "crate::none_as_empty")]
            group: Option<CompetitionGroup>,
        }
        let x = Dummy { group: None };
        assert_eq!(r#"{"group":""}"#, serde_json::to_string(&x).unwrap());
    }
}