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
use crate::archive::ArchiveMode;
use crate::error::KaggleError;
use crate::models::metadata::Metadata;
use crate::models::{DatasetUploadFile, License};
use crate::KaggleApiClient;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct DatasetNew {
    /// The location of the metadata files, can only be empty if the dataset
    /// does not have any resources.
    pub dataset_folder: Option<PathBuf>,
    /// The metadata of the dataset
    pub metadata: Metadata,
    /// Whether or not the dataset should be private
    pub is_private: bool,
    /// Whether or not a tabular dataset should be converted to csv
    pub convert_to_csv: bool,
    /// How to archive the files beforehand
    pub archive_mode: ArchiveMode,
}

impl DatasetNew {
    pub fn with_metadata(metadata: Metadata) -> Self {
        Self {
            dataset_folder: None,
            metadata,
            is_private: true,
            convert_to_csv: true,
            archive_mode: Default::default(),
        }
    }

    pub async fn with_metadata_file(path: impl AsRef<Path>) -> anyhow::Result<Self> {
        let dataset_folder = path.as_ref().to_path_buf();
        let mut new = Self::with_metadata(
            KaggleApiClient::read_dataset_metadata_file(&dataset_folder).await?,
        );
        new.dataset_folder = Some(dataset_folder);
        Ok(new)
    }

    pub(crate) fn validate_resources(&self) -> Result<(), KaggleError> {
        if let Some(folder) = &self.dataset_folder {
            self.metadata.validate_resource(folder)
        } else {
            Ok(())
        }
    }

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

    pub fn private(mut self) -> Self {
        self.is_private = true;
        self
    }

    pub fn public(mut self) -> Self {
        self.is_private = false;
        self
    }

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

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

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

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DatasetNewRequest {
    /// The title of the new dataset
    title: String,
    /// The slug that the dataset should be created with
    slug: Option<String>,
    /// The owner's username
    owner_slug: Option<String>,
    /// The license that should be associated with the dataset
    license_name: Option<String>,
    /// The subtitle to be set on the dataset
    subtitle: Option<String>,
    /// The description to be set on the dataset
    description: Option<String>,
    /// A list of files that should be associated with the dataset
    files: Vec<DatasetUploadFile>,
    /// Whether or not the dataset should be private
    is_private: bool,
    /// Whether or not a tabular dataset should be converted to csv
    convert_to_csv: bool,
    /// A list of tag IDs to associated with the dataset
    category_ids: Vec<String>,
}

impl DatasetNewRequest {
    pub fn builder(title: impl ToString) -> DatasetNewRequestBuilder {
        DatasetNewRequestBuilder::new(title)
    }

    pub fn title(&self) -> &String {
        &self.title
    }

    pub fn slug(&self) -> Option<&String> {
        self.slug.as_ref()
    }

    pub fn owner_slug(&self) -> Option<&String> {
        self.owner_slug.as_ref()
    }

    pub fn license_name(&self) -> Option<&String> {
        self.license_name.as_ref()
    }

    pub fn subtitle(&self) -> Option<&String> {
        self.subtitle.as_ref()
    }

    pub fn description(&self) -> Option<&String> {
        self.description.as_ref()
    }

    pub fn files(&self) -> &Vec<DatasetUploadFile> {
        &self.files
    }

    pub fn is_private(&self) -> bool {
        self.is_private
    }

    pub fn convert_to_csv(&self) -> bool {
        self.convert_to_csv
    }

    pub fn category_ids(&self) -> &Vec<String> {
        &self.category_ids
    }
}

pub struct DatasetNewRequestBuilder {
    /// The title of the new dataset
    title: String,
    /// The slug that the dataset should be created with
    slug: Option<String>,
    /// The owner's username
    owner_slug: Option<String>,
    /// The license that should be associated with the dataset
    license_name: Option<String>,
    /// The subtitle to be set on the dataset
    subtitle: Option<String>,
    /// The description to be set on the dataset
    description: Option<String>,
    /// A list of files that should be associated with the dataset
    files: Vec<DatasetUploadFile>,
    /// Whether or not the dataset should be private
    is_private: bool,
    /// Whether or not a tabular dataset should be converted to csv
    convert_to_csv: bool,
    /// A list of tag IDs to associated with the dataset
    category_ids: Vec<String>,
}

impl DatasetNewRequestBuilder {
    pub fn new(title: impl ToString) -> Self {
        Self {
            title: title.to_string(),
            slug: None,
            owner_slug: None,
            license_name: None,
            subtitle: None,
            description: None,
            files: vec![],
            is_private: true,
            convert_to_csv: true,
            category_ids: vec![],
        }
    }

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

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

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

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

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

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

    pub fn files(mut self, files: Vec<DatasetUploadFile>) -> Self {
        self.files = files;
        self
    }

    pub fn file(mut self, file: DatasetUploadFile) -> Self {
        self.files.push(file);
        self
    }

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

    pub fn private(mut self) -> Self {
        self.is_private = true;
        self
    }

    pub fn public(mut self) -> Self {
        self.is_private = false;
        self
    }

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

    pub fn category_ids(mut self, category_ids: Vec<String>) -> Self {
        self.category_ids = category_ids;
        self
    }

    pub fn category_id(mut self, category_id: impl ToString) -> Self {
        self.category_ids.push(category_id.to_string());
        self
    }

    pub fn build(self) -> DatasetNewRequest {
        DatasetNewRequest {
            title: self.title,
            slug: self.slug,
            owner_slug: self.owner_slug,
            license_name: self.license_name,
            subtitle: self.subtitle,
            description: self.description,
            files: self.files,
            is_private: self.is_private,
            convert_to_csv: self.convert_to_csv,
            category_ids: self.category_ids,
        }
    }
}