cloudconvert 0.2.1

Client library for CloudConvert
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! Types related to managing tasks. See the [crate level docs](crate) for a nice list of available
//! tasks.

use std::borrow::Cow;
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::Format;

/// The input of a task: either a single task, or a list of multiple tasks.
///
/// This implements `From<T>` for most sensible `T`.
#[derive(Debug)]
pub enum Input<'a, 'b> {
    Single(Cow<'a, str>),
    List(Cow<'b, [Cow<'a, str>]>),
}

impl<'a, 'b> Input<'a, 'b> {
    /// Convert `Input<'a, 'b> into Input<'static, 'static>`.
    pub fn into_static(self) -> Input<'static, 'static> {
        match self {
            Input::Single(Cow::Owned(s)) => Input::Single(Cow::Owned(s)),
            Input::Single(Cow::Borrowed(s)) => Input::Single(Cow::Owned(s.to_string())),
            Input::List(Cow::Owned(items)) => Input::List(Cow::Owned(
                items
                    .into_iter()
                    .map(Cow::into_owned)
                    .map(Cow::Owned)
                    .collect(),
            )),
            Input::List(Cow::Borrowed(items)) => Input::List(Cow::Owned(
                items
                    .iter()
                    .map(std::ops::Deref::deref)
                    .map(str::to_string)
                    .map(Cow::Owned)
                    .collect(),
            )),
        }
    }
}

impl<'a, 'b> Serialize for Input<'a, 'b> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Input::Single(item) => serializer.serialize_str(item),
            Input::List(items) => Serialize::serialize(items, serializer),
        }
    }
}

impl<'a, 'b> From<&'a str> for Input<'a, 'b> {
    fn from(s: &'a str) -> Input<'a, 'b> {
        Input::Single(Cow::Borrowed(s))
    }
}

impl<'a, 'b> From<String> for Input<'a, 'b> {
    fn from(s: String) -> Input<'a, 'b> {
        Input::Single(Cow::Owned(s))
    }
}

impl<'a, 'b, T: AsRef<str>> From<&'a T> for Input<'a, 'b> {
    fn from(s: &'a T) -> Input<'a, 'b> {
        Input::Single(Cow::Borrowed(s.as_ref()))
    }
}

impl<'a, 'b> From<Vec<String>> for Input<'a, 'b> {
    fn from(items: Vec<String>) -> Input<'a, 'b> {
        Input::List(Cow::Owned(items.into_iter().map(Cow::Owned).collect()))
    }
}

impl<'a, 'b> From<&'a [String]> for Input<'a, 'b> {
    fn from(items: &'a [String]) -> Input<'a, 'b> {
        Input::List(Cow::Owned(
            items
                .iter()
                .map(std::ops::Deref::deref)
                .map(Cow::Borrowed)
                .collect(),
        ))
    }
}

impl<'a, 'b, 'c: 'a> From<&'c [&'a str]> for Input<'a, 'b> {
    fn from(items: &'c [&'a str]) -> Input<'a, 'b> {
        Input::List(Cow::Owned(
            items.iter().map(|item| Cow::Borrowed(*item)).collect(),
        ))
    }
}

impl<'a, 'b> From<Vec<Cow<'a, str>>> for Input<'a, 'b> {
    fn from(items: Vec<Cow<'a, str>>) -> Input<'a, 'b> {
        Input::List(Cow::Owned(items))
    }
}

impl<'a, 'b> From<&'b [Cow<'a, str>]> for Input<'a, 'b> {
    fn from(items: &'b [Cow<'a, str>]) -> Input<'a, 'b> {
        Input::List(Cow::Borrowed(items))
    }
}

macro_rules! make_task_types {
    //(__field_type opt: str) => {
    //    make_task_types!(__field_type opt $field_name: Cow<'a str>);
    //};
    (__field_type opt: $field_type:ty) => {
        Option<$field_type>
    };
    //(__field_type req: str) => {
    //    make_task_types!(__field_type req: Cow<'a str>);
    //};
    (__field_type req: $field_type:ty) => {
        $field_type
    };

    (
        $(#[$task_enum_meta:meta])*
        $enum_vis:vis enum $Task:ident<'a>;

        $(
            $(#[$task_meta:meta])*
            $struct_vis:vis struct $TaskName:ident<'a> {
                operation: $operation:expr,
                $(
                    $(#[$req_field_meta:meta])*
                    req $req_field_name:ident: $req_field_type:ty,
                )*
                $(
                    opt $opt_field_name:ident: $opt_field_type:ty,
                )*
            }
            //$(=> struct $TaskJsonName:ident<'a> {
            //    $(
            //        $(#[$json_field_meta:meta])*
            //        $json_field_name:ident: $json_field_type:ty,
            //    )*
            //})?
        )*
    ) => {
        $(#[$task_enum_meta])*
        $enum_vis enum $Task<'a> {
            $(
                $TaskName($TaskName<'a>),
            )*
        }

        impl<'a> $Task<'a> {
            /// Returns the operation name of this task. For example `convert` or `import/url`.
            pub fn operation(&self) -> &'static str {
                match self {
                    $($Task::$TaskName(_) => $operation,)*
                }
            }

            /// Convert a job to an object to be included in a job call.
            ///
            /// This includes the fields of the task, with an additional `"operation"` field,
            /// containing the operation name.
            pub fn to_job_task(&self) -> serde_json::Result<serde_json::Value> {
                match self {
                    $($Task::$TaskName(task) => task.to_job_task(),)*
                }
            }
        }

        $(
            $(#[$task_meta])*
            #[derive(serde::Serialize, Debug)]
            $struct_vis struct $TaskName<'a> {
                $(
                    $(#[$req_field_meta])*
                    $struct_vis $req_field_name: make_task_types!(__field_type req: $req_field_type),
                )*
                $(
                    // $(#[$opt_field_meta])*
                    #[serde(skip_serializing_if = "Option::is_none")]
                    $struct_vis $opt_field_name: make_task_types!(__field_type opt: $opt_field_type),
                )*
            }

            //$(
            //    #[derive(serde::Serialize, Debug)]
            //    struct $TaskJsonName<'a> {
            //        $(#[$json_field_meta])*
            //        $($json_field_name: $json_field_type,)*
            //    }
            //)?

            hapic::json_api_call!(json <'a> ($crate::ApiCall) $operation: $TaskName<'a> as $TaskName<'a> => TasksOutput as Status);

            impl<'a> From<$TaskName<'a>> for $Task<'a> {
                fn from(task: $TaskName<'a>) -> $Task<'a> {
                    $Task::$TaskName(task)
                }
            }

            impl<'a> $TaskName<'a> {
                /// Convert a job to an object to be included in a job call.
                ///
                /// This includes the fields of the task, with an additional `"operation"` field,
                /// containing the operation name.
                pub fn to_job_task(&self) -> serde_json::Result<serde_json::Value> {
                    let task = self;
                    //$(
                    //    let task = $TaskJsonName::from(self);
                    //    let task = &task;
                    //)?
                    let mut value = serde_json::to_value(task)?;
                    match &mut value {
                        serde_json::Value::Object(value) => {
                            value.insert(
                                "operation".to_string(),
                                serde_json::Value::String($operation.to_string()),
                            );
                        },
                        _ => unreachable!(),
                    }
                    Ok(value)
                }
            }
        )*
    }
}

impl<'a> Task<'a> {
    pub fn task_uri(&self, endpoint: &str) -> String {
        format!("{endpoint}/{}", self.operation())
    }
}

make_task_types!(
    /// A task. See the [crate level docs](crate) for a nice list of available tasks.
    pub enum Task<'a>;

    /// Import a file by downloading it from a URL.
    ///
    /// Docs: [api/v2/import#import-url-tasks](https://cloudconvert.com/api/v2/import#import-url-tasks)
    pub struct ImportUrl<'a> {
        operation: "import/url",

        req url: Cow<'a, str>,
        opt filename: Cow<'a, str>,
        opt headers: HashMap<String, String>,
    }

    /// Import a document from an S3 compatible bucket.
    ///
    /// Docs: [api/v2/import#import-s3-tasks](https://cloudconvert.com/api/v2/import#import-s3-tasks)
    pub struct ImportS3<'a> {
        operation: "import/s3",

        req bucket: Cow<'a, str>,
        req region: Cow<'a, str>,
        req access_key_id: Cow<'a, str>,
        req secret_access_key: Cow<'a, str>,
        opt endpoint: Cow<'a, str>,
        opt key: Cow<'a, str>,
        opt key_prefix: Cow<'a, str>,
        opt session_token: Cow<'a, str>,
        opt filename: Cow<'a, str>,
    }

    /// Import a document from Azure Blob Storage.
    ///
    /// Docs: [api/v2/import#import-azure-blob-tasks](https://cloudconvert.com/api/v2/import#import-azure-blob-tasks)
    pub struct ImportAzureBlob<'a> {
        operation: "import/azure/blob",

        req storage_account: Cow<'a, str>,
        req container: Cow<'a, str>,
        opt storage_access_key: Cow<'a, str>,
        opt sas_token: Cow<'a, str>,
        opt blob: Cow<'a, str>,
        opt blob_prefix: Cow<'a, str>,
        opt filename: Cow<'a, str>,
    }

    /// Import a document from Google Cloud Storage.
    ///
    /// Docs: [api/v2/import#import-google-cloud-storage-tasks](https://cloudconvert.com/api/v2/import#import-google-cloud-storage-tasks)
    pub struct ImportGoogleCloud<'a> {
        operation: "import/google-cloud-storage",

        req project_id: Cow<'a, str>,
        req bucket: Cow<'a, str>,
        req client_email: Cow<'a, str>,
        req private_key: Cow<'a, str>,
        opt file: Cow<'a, str>,
        opt file_prefix: Cow<'a, str>,
        opt filename: Cow<'a, str>,
    }

    /// Import a document from OpenStack Object Storage (Swift).
    ///
    /// Docs: [api/v2/import#import-openstack-tasks](https://cloudconvert.com/api/v2/import#import-openstack-tasks)
    pub struct ImportOpenStack<'a> {
        operation: "import/openstack",

        req auth_url: Cow<'a, str>,
        req username: Cow<'a, str>,
        req password: Cow<'a, str>,
        req region: Cow<'a, str>,
        req container: Cow<'a, str>,
        opt tenant_name: Cow<'a, str>,
        opt file: Cow<'a, str>,
        opt file_prefix: Cow<'a, str>,
        opt filename: Cow<'a, str>,
    }

    /// Import a document from an SFTP server.
    ///
    /// Docs: [api/v2/import#import-sftp-tasks](https://cloudconvert.com/api/v2/import#import-sftp-tasks)
    pub struct ImportSFTP<'a> {
        operation: "import/sftp",

        req host: Cow<'a, str>,
        req username: Cow<'a, str>,
        req password: Cow<'a, str>,
        opt port: u16,
        opt private_key: Cow<'a, str>,
        opt file: Cow<'a, str>,
        opt path: Cow<'a, str>,
        opt filename: Cow<'a, str>,
    }

    /// Convert a document.
    ///
    /// Docs: [api/v2/convert](https://cloudconvert.com/api/v2/convert)
    ///
    /// **Note:** If you're importing from a URL, converting, then exporting to a URL, you can use
    /// [`crate::ImportConvertExport`] instead!
    pub struct Convert<'a> {
        operation: "convert",

        /// The ID of the task, or tasks, to convert.
        ///
        /// This is most likely an import task.
        req input: Input<'a, 'a>,
        req output_format: Format,
        opt input_format: Format,
        opt filename: Cow<'a, str>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Optimize/compress a PDF, PNG or JPG file.
    ///
    /// Docs: [api/v2/optimize](https://cloudconvert.com/api/v2/optimize)
    pub struct Optimize<'a> {
        operation: "optimize",

        /// The ID of the task, or tasks, to optimize.
        ///
        /// This is most likely an import task.
        req input: Input<'a, 'a>,
        opt input_format: Format,
        opt profile: OptimizationProfile,
        opt flatten_signatures: bool,
        opt colorspace: Colorspace,
        opt filename: Cow<'a, str>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Add a watermark to a PDF, image, or video.
    ///
    /// Docs: [api/v2/watermark](https://cloudconvert.com/api/v2/watermark)
    pub struct Watermark<'a> {
        operation: "watermark",

        /// The ID of the task, or tasks, to watermark.
        ///
        /// This is most likely an import task.
        req input: Input<'a, 'a>,
        opt input_format: Format,
        opt pages: Cow<'a, str>,
        opt layer: WatermarkLayer,
        opt text: Cow<'a, str>,
        opt font_size: u16,
        opt font_width_percent: u8,
        opt font_color: Cow<'a, str>,
        opt font_name: Cow<'a, str>,
        opt font_align: FontAlign,
        opt image: Cow<'a, str>,
        opt image_width: u32,
        opt image_width_percent: u8,
        opt position_vertical: VerticalPosition,
        opt position_horizontal: HorizontalPosition,
        opt margin_vertical: u32,
        opt opacity: u8,
        opt rotation: u16,
        opt filename: Cow<'a, str>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Create a thumbnail from a single input (PNG, JPG or WEBP).
    ///
    /// Docs: [api/v2/thumbnail](https://cloudconvert.com/api/v2/thumbnail)
    pub struct Thumbnail<'a> {
        operation: "thumbnail",

        /// The ID of the task, or tasks, to archive.
        ///
        /// This is most likely an import task.
        req input: Cow<'a, str>,
        /// This should be PNG, JPG or WEBP.
        req output_format: Format,
        opt input_format: Format,
        opt width: u32,
        opt height: u32,
        opt fit: ThumbnailFit,
        opt count: u32,
        opt timestamp: Cow<'a, str>,
        opt filename: Cow<'a, str>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Merge files into a single PDF.
    ///
    /// Docs: [api/v2/merge](https://cloudconvert.com/api/v2/merge)
    pub struct Merge<'a> {
        operation: "merge",

        /// The ID of the task, or tasks, to archive.
        ///
        /// This is most likely an import task.
        req input: Input<'a, 'a>,
        /// This should be `pdf`.
        req output_format: Format,
        opt filename: Cow<'a, str>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Create zip, rar, 7z, or tar archives.
    ///
    /// Docs: [api/v2/archive](https://cloudconvert.com/api/v2/archive)
    pub struct Archive<'a> {
        operation: "archive",

        /// The ID of the task, or tasks, to archive.
        ///
        /// This is most likely an import task.
        req input: Input<'a, 'a>,
        /// This should be `zip`, `rar`, `7z`, `tar`, `tar.gz`, or `tar.bz2`.
        req output_format: Format,
        opt filename: Cow<'a, str>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Capture a website as a PDF or image.
    ///
    /// Docs: [api/v2/capture-website](https://cloudconvert.com/api/v2/capture-website)
    pub struct Capture<'a> {
        operation: "capture-website",

        /// The URL to capture.
        req url: Cow<'a, str>,
        /// This should be `pdf`, `png` or `jpg`.
        req output_format: Format,
        req print_background: bool,
        req display_header_footer: bool,
        #[serde(skip_serializing_if = "HashMap::is_empty")]
        req headers: HashMap<String, String>,
        opt pages: Cow<'a, str>,
        opt zoom: f32,
        opt page_width: f32,
        opt page_height: f32,
        opt margin_top: f32,
        opt margin_bottom: f32,
        opt margin_left: f32,
        opt header_template: Cow<'a, str>,
        opt footer_template: Cow<'a, str>,
        opt wait_until: CaptureWaitUntil,
        opt wait_for_element: Cow<'a, str>,
        opt wait_time: u32,
        opt css_media_type: CssMediaType,
        opt filename: HashMap<String, String>,
        opt engine: Cow<'a, str>,
        opt engine_version: Cow<'a, str>,
        opt timeout: u32,
    }

    /// Generate a temporary URL to download files.
    ///
    /// Docs [api/v2/export#export-url-tasks](https://cloudconvert.com/api/v2/export#export-url-tasks)
    pub struct ExportUrl<'a> {
        operation: "export/url",

        /// The ID of the task, or tasks, to export.
        req input: Input<'a, 'a>,
        req inline: bool,
        req archive_multiple_files: bool,
    }

    /// Export files to an S3 compatible bucket.
    ///
    /// Docs [api/v2/export#export-s3-tasks](https://cloudconvert.com/api/v2/export#export-s3-tasks)
    pub struct ExportS3<'a> {
        operation: "export/s3",

        /// The ID of the task, or tasks, to export.
        req input: Input<'a, 'a>,
        req bucket: Cow<'a, str>,
        req region: Cow<'a, str>,
        req access_key_id: Cow<'a, str>,
        req secret_access_key: Cow<'a, str>,
        opt endpoint: Cow<'a, str>,
        opt key: Cow<'a, str>,
        opt key_prefix: Cow<'a, str>,
        opt session_token: Cow<'a, str>,
        opt acl: Cow<'a, str>,
        opt cache_control: Cow<'a, str>,
        opt content_disposition: Cow<'a, str>,
        opt content_type: Cow<'a, str>,
        opt metadata: serde_json::Value,
        opt server_side_encrpytion: Cow<'a, str>,
        opt tagging: serde_json::Value,
    }

    /// Export files to Azure Blob Storage
    ///
    /// Docs [api/v2/export#export-azure-blob-tasks](https://cloudconvert.com/api/v2/export#export-azure-blob-tasks)
    pub struct ExportAzureBlob<'a> {
        operation: "export/azure/blob",

        /// The ID of the task, or tasks, to export.
        req input: Input<'a, 'a>,
        req storage_account: Cow<'a, str>,
        req container: Cow<'a, str>,
        opt storage_access_key: Cow<'a, str>,
        opt sas_token: Cow<'a, str>,
        opt blob: Cow<'a, str>,
        opt blob_prefix: Cow<'a, str>,
        opt metadata: serde_json::Value,
    }

    /// Export files to Google Cloud Storage
    ///
    /// Docs [api/v2/export#export-google-cloud-storage-tasks](https://cloudconvert.com/api/v2/export#export-google-cloud-storage-tasks)
    pub struct ExportGoogleCloud<'a> {
        operation: "export/google-cloud-storage",

        /// The ID of the task, or tasks, to export.
        req input: Input<'a, 'a>,
        req project_id: Cow<'a, str>,
        req bucket: Cow<'a, str>,
        req client_email: Cow<'a, str>,
        req private_key: Cow<'a, str>,
        opt file: Cow<'a, str>,
        opt file_prefix: Cow<'a, str>,
    }

    /// Export files to OpenStack Object Storage (Swift)
    ///
    /// Docs [api/v2/export#export-openstack-tasks](https://cloudconvert.com/api/v2/export#export-openstack-tasks)
    pub struct ExportOpenStack<'a> {
        operation: "export/openstack",

        /// The ID of the task, or tasks, to export.
        req input: Input<'a, 'a>,
        req auth_url: Cow<'a, str>,
        req username: Cow<'a, str>,
        req password: Cow<'a, str>,
        req region: Cow<'a, str>,
        req container: Cow<'a, str>,
        opt tenant_name: Cow<'a, str>,
        opt file: Cow<'a, str>,
        opt file_prefix: Cow<'a, str>,
    }

    /// Export files to an SFTP server.
    ///
    /// Docs [api/v2/export#export-sftp-tasks](https://cloudconvert.com/api/v2/export#export-sftp-tasks)
    pub struct ExportSFTP<'a> {
        operation: "export/sftp",

        /// The ID of the task, or tasks, to export.
        req input: Input<'a, 'a>,
        req host: Cow<'a, str>,
        req username: Cow<'a, str>,
        opt port: u16,
        opt password: Cow<'a, str>,
        opt private_key: Cow<'a, str>,
        opt file: Cow<'a, str>,
        opt path: Cow<'a, str>,
    }
);

/// Enum for the `font_align` property of [`Watermark`] tasks.
///
/// Docs: [api/v2/watermark](https://cloudconvert.com/api/v2/watermark)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum FontAlign {
    #[serde(rename = "left")]
    Left,
    #[serde(rename = "right")]
    Right,
    #[serde(rename = "center")]
    Center,
}

/// Enum for the `layer` property of [`Watermark`] tasks.
///
/// Docs: [api/v2/watermark](https://cloudconvert.com/api/v2/watermark)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum WatermarkLayer {
    #[serde(rename = "above")]
    Above,
    #[serde(rename = "below")]
    Below,
}

/// Enum for the `position_vertical` property of [`Watermark`] tasks.
///
/// Docs: [api/v2/watermark](https://cloudconvert.com/api/v2/watermark)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum VerticalPosition {
    #[serde(rename = "left")]
    Left,
    #[serde(rename = "right")]
    Right,
    #[serde(rename = "center")]
    Center,
}

/// Enum for the `position_horizontal` property of [`Watermark`] tasks.
///
/// Docs: [api/v2/watermark](https://cloudconvert.com/api/v2/watermark)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum HorizontalPosition {
    #[serde(rename = "top")]
    Top,
    #[serde(rename = "bottom")]
    Bottom,
    #[serde(rename = "center")]
    Center,
}

/// Enum for the `fit` property of [`Thumbnail`] tasks.
///
/// Docs: [api/v2/thumbnail](https://cloudconvert.com/api/v2/thumbnail)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum ThumbnailFit {
    #[serde(rename = "max")]
    Max,
    #[serde(rename = "crop")]
    Crop,
    #[serde(rename = "scale")]
    Scale,
}

/// Enum for the `profile` property of [`Optimize`] tasks.
///
/// Docs: [api/v2/optimize](https://cloudconvert.com/api/v2/optimize)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum OptimizationProfile {
    #[serde(rename = "web")]
    Web,
    #[serde(rename = "print")]
    Print,
    #[serde(rename = "archive")]
    Archive,
    #[serde(rename = "mrc")]
    Mrc,
    #[serde(rename = "max")]
    Max,
}

/// Enum for the `colorspace` property of [`Optimize`] tasks.
///
/// Docs: [api/v2/optimize](https://cloudconvert.com/api/v2/optimize)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum Colorspace {
    #[serde(rename = "unchanged")]
    Unchanged,
    #[serde(rename = "rgb")]
    RGB,
    #[serde(rename = "cmyk")]
    CMYK,
    #[serde(rename = "greyscale")]
    Greyscale,
}

/// Enum for the `wait_until` property of [`Capture`] tasks.
///
/// Docs: [api/v2/capture-website](https://cloudconvert.com/api/v2/capture-website)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum CaptureWaitUntil {
    #[serde(rename = "Load")]
    Load,
    #[serde(rename = "domcontentloaded")]
    DOMContentLoaded,
    #[serde(rename = "networkidle0")]
    NetworkIdle0,
    #[serde(rename = "networkidle2")]
    NetworkIdle2,
}

/// Enum for the `css_media_type` property of [`CssMediaType`] tasks.
///
/// Docs: [api/v2/capture-website](https://cloudconvert.com/api/v2/capture-website)
#[derive(Debug, Serialize, PartialEq, Eq)]
pub enum CssMediaType {
    #[serde(rename = "print")]
    Print,
    #[serde(rename = "screen")]
    Screen,
}

#[doc(hidden)]
#[derive(Deserialize)]
pub struct TasksOutput {
    pub data: Status,
}

impl From<TasksOutput> for Status {
    fn from(output: TasksOutput) -> Status {
        output.data
    }
}

/// The status of a task.
///
/// Docs: [api/v2/tasks](https://cloudconvert.com/api/v2/tasks#tasks-show)
#[derive(Debug, Deserialize)]
pub struct Status {
    /// The task ID
    pub id: String,

    /// The ID of the job containing this task, if there is one.
    #[serde(default)]
    pub job_id: Option<String>,

    /// The name of the task, if it's part of a job.
    #[serde(default)]
    pub name: Option<String>,

    /// The name of the operation, for example `convert` or `export/url`.
    pub operation: String,

    /// The status of the task.
    pub status: super::Status,

    /// The status message for the task.
    ///
    /// If the `status` is `Error`, then this contains the error details.
    #[serde(default)]
    #[serde(rename = "message")]
    pub status_message: Option<String>,

    /// If `status` is `Error`, the error code.
    #[serde(default)]
    #[serde(rename = "code")]
    pub error_code: Option<String>,

    /// If `status` is `Finished`, the number of credits consumed.
    #[serde(default)]
    #[serde(rename = "code")]
    pub credits: Option<u16>,

    // TODO: started_at
    // TODO: ended_at
    // TODO: depends_on_tasks
    /// If this task is a retry, the original task ID.
    #[serde(default)]
    pub retry_of_task_id: Option<String>,

    /// The list of task IDs that are retries of this task.
    ///
    /// This isn't available unless the `include` field on the `Show` request is `retries`.
    #[serde(default)]
    pub retries: Vec<String>,

    #[serde(default)]
    /// The engine used
    pub engine: Option<String>,

    #[serde(default)]
    /// The engine version used
    pub engine_version: Option<String>,

    /// The payload submitted to the task.
    #[serde(default)]
    pub payload: serde_json::Value,

    /// The results of the task.
    #[serde(default)]
    pub result: Option<HashMap<String, serde_json::Value>>,

    #[serde(default)]
    pub links: Option<HashMap<String, String>>,
}