cloudconvert-sdk 0.1.0

Async Rust SDK primitives for the CloudConvert API v2.
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
use std::collections::BTreeMap;

use std::{error::Error as StdError, fmt};

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;

use crate::file_extension::normalize_file_extension;
use crate::tasks::TaskRequest;

#[derive(Clone, Debug, Default, Serialize)]
pub struct OperationListQuery {
    #[serde(rename = "filter[operation]", skip_serializing_if = "Option::is_none")]
    operation: Option<String>,
    #[serde(
        rename = "filter[input_format]",
        skip_serializing_if = "Option::is_none"
    )]
    input_format: Option<String>,
    #[serde(
        rename = "filter[output_format]",
        skip_serializing_if = "Option::is_none"
    )]
    output_format: Option<String>,
    #[serde(rename = "filter[engine]", skip_serializing_if = "Option::is_none")]
    engine: Option<String>,
    #[serde(
        rename = "filter[engine_version]",
        skip_serializing_if = "Option::is_none"
    )]
    engine_version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    include: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    alternatives: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    page: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    per_page: Option<u32>,
}

impl OperationListQuery {
    pub fn operation(mut self, operation: impl Into<String>) -> Self {
        self.operation = Some(operation.into());
        self
    }

    pub fn input_format(mut self, input_format: impl Into<String>) -> Self {
        self.input_format = Some(normalize_file_extension(input_format));
        self
    }

    pub fn output_format(mut self, output_format: impl Into<String>) -> Self {
        self.output_format = Some(normalize_file_extension(output_format));
        self
    }

    pub fn engine(mut self, engine: impl Into<String>) -> Self {
        self.engine = Some(engine.into());
        self
    }

    pub fn engine_version(mut self, engine_version: impl Into<String>) -> Self {
        self.engine_version = Some(engine_version.into());
        self
    }

    pub fn include(mut self, include: impl Into<String>) -> Self {
        self.include = Some(include.into());
        self
    }

    pub fn include_options(self) -> Self {
        self.include("options")
    }

    pub fn include_engine_versions(self) -> Self {
        self.include("engine_versions")
    }

    pub fn include_options_and_engine_versions(self) -> Self {
        self.include("options,engine_versions")
    }

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

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

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

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Operation {
    pub operation: String,
    #[serde(default)]
    pub input_format: Option<String>,
    #[serde(default)]
    pub output_format: Option<String>,
    #[serde(default)]
    pub engine: Option<String>,
    #[serde(default)]
    pub engine_version: Option<String>,
    #[serde(default)]
    pub credits: Option<u64>,
    #[serde(default, deserialize_with = "deserialize_options")]
    pub options: BTreeMap<String, OperationOption>,
    #[serde(default, deserialize_with = "deserialize_engine_versions")]
    pub engine_versions: Vec<OperationEngineVersion>,
    #[serde(default)]
    pub alternatives: Vec<Operation>,
    #[serde(default)]
    pub deprecated: Option<bool>,
    #[serde(default)]
    pub experimental: Option<bool>,
    #[serde(default)]
    pub meta: Option<BTreeMap<String, Value>>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl Operation {
    pub fn option(&self, name: &str) -> Option<&OperationOption> {
        self.options.get(name)
    }

    pub fn options(&self) -> impl Iterator<Item = (&str, &OperationOption)> {
        self.options
            .iter()
            .map(|(name, option)| (name.as_str(), option))
    }

    pub fn engine_version_values(&self) -> impl Iterator<Item = &str> {
        self.engine_versions
            .iter()
            .map(|version| version.version.as_str())
    }

    pub fn default_engine_version(&self) -> Option<&OperationEngineVersion> {
        self.engine_versions
            .iter()
            .find(|version| version.default.unwrap_or(false))
    }

    pub fn latest_engine_version(&self) -> Option<&OperationEngineVersion> {
        self.engine_versions
            .iter()
            .find(|version| version.latest.unwrap_or(false))
    }

    pub fn validate_task(&self, task: &TaskRequest) -> OperationValidationResult {
        self.validate_task_with_mode(task, OperationValidationMode::Lenient)
    }

    pub fn validate_task_strict(&self, task: &TaskRequest) -> OperationValidationResult {
        self.validate_task_with_mode(task, OperationValidationMode::Strict)
    }

    pub fn validate_task_with_mode(
        &self,
        task: &TaskRequest,
        mode: OperationValidationMode,
    ) -> OperationValidationResult {
        if task.operation() != self.operation {
            return Err(OperationValidationError::operation_mismatch(
                &self.operation,
                task.operation(),
            ));
        }

        for (name, option) in &self.options {
            let value = task.payload().get(name);
            if option.required.unwrap_or(false) && value.is_none_or(Value::is_null) {
                return Err(OperationValidationError::missing_required(
                    &self.operation,
                    name,
                ));
            }

            let Some(value) = value else {
                continue;
            };
            option.validate_value_for_operation(&self.operation, name, value)?;
        }

        if matches!(mode, OperationValidationMode::Strict) && !self.options.is_empty() {
            for name in task.payload().keys() {
                if !self.options.contains_key(name) && !is_common_task_field(name) {
                    return Err(OperationValidationError::unknown_option(
                        &self.operation,
                        name,
                    ));
                }
            }
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct OperationOption {
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default, rename = "type")]
    pub kind: Option<OperationOptionKind>,
    #[serde(default)]
    pub label: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub required: Option<bool>,
    #[serde(default)]
    pub default: Option<Value>,
    #[serde(default, alias = "values")]
    pub possible_values: Vec<Value>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl OperationOption {
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn kind(&self) -> Option<&OperationOptionKind> {
        self.kind.as_ref()
    }

    pub fn is_required(&self) -> bool {
        self.required.unwrap_or(false)
    }

    pub fn possible_values(&self) -> &[Value] {
        &self.possible_values
    }

    pub fn validate_value(&self, value: &Value) -> OperationValidationResult {
        self.validate_value_for_operation("", self.name.as_deref().unwrap_or("option"), value)
    }

    fn validate_value_for_operation(
        &self,
        operation: &str,
        name: &str,
        value: &Value,
    ) -> OperationValidationResult {
        if let Some(kind) = &self.kind
            && !kind.matches_value(value)
        {
            return Err(OperationValidationError::invalid_type(
                operation,
                name,
                kind.as_str(),
                value_type(value),
            ));
        }

        if !self.possible_values.is_empty()
            && !self.possible_values.iter().any(|allowed| allowed == value)
        {
            return Err(OperationValidationError::invalid_value(
                operation,
                name,
                "one of the documented possible_values",
                value,
            ));
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OperationOptionKind {
    String,
    Boolean,
    Integer,
    Float,
    Enum,
    Dictionary,
    Array,
    Other(String),
}

impl OperationOptionKind {
    pub fn as_str(&self) -> &str {
        match self {
            Self::String => "string",
            Self::Boolean => "boolean",
            Self::Integer => "integer",
            Self::Float => "float",
            Self::Enum => "enum",
            Self::Dictionary => "dictionary",
            Self::Array => "array",
            Self::Other(value) => value.as_str(),
        }
    }

    fn matches_value(&self, value: &Value) -> bool {
        match self {
            Self::String => value.is_string(),
            Self::Boolean => value.is_boolean(),
            Self::Integer => value
                .as_i64()
                .or_else(|| value.as_u64().and_then(|value| i64::try_from(value).ok()))
                .is_some(),
            Self::Float => value.is_number(),
            Self::Enum => value.is_string(),
            Self::Dictionary => value.is_object(),
            Self::Array => value.is_array(),
            Self::Other(_) => true,
        }
    }
}

impl Serialize for OperationOptionKind {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for OperationOptionKind {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Ok(match value.as_str() {
            "string" => Self::String,
            "boolean" => Self::Boolean,
            "integer" => Self::Integer,
            "float" => Self::Float,
            "enum" => Self::Enum,
            "dictionary" => Self::Dictionary,
            "array" => Self::Array,
            _ => Self::Other(value),
        })
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct OperationEngineVersion {
    pub version: String,
    #[serde(default)]
    pub default: Option<bool>,
    #[serde(default)]
    pub latest: Option<bool>,
    #[serde(default)]
    pub deprecated: Option<bool>,
    #[serde(default)]
    pub experimental: Option<bool>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OperationValidationMode {
    Lenient,
    Strict,
}

pub type OperationValidationResult = std::result::Result<(), OperationValidationError>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OperationValidationError {
    pub kind: OperationValidationErrorKind,
    pub operation: String,
    pub option: Option<String>,
    pub expected: Option<String>,
    pub actual: Option<String>,
}

impl OperationValidationError {
    fn operation_mismatch(expected: &str, actual: &str) -> Self {
        Self {
            kind: OperationValidationErrorKind::OperationMismatch,
            operation: expected.to_string(),
            option: None,
            expected: Some(expected.to_string()),
            actual: Some(actual.to_string()),
        }
    }

    fn missing_required(operation: &str, option: &str) -> Self {
        Self {
            kind: OperationValidationErrorKind::MissingRequiredOption,
            operation: operation.to_string(),
            option: Some(option.to_string()),
            expected: Some("present value".to_string()),
            actual: Some("missing".to_string()),
        }
    }

    fn invalid_type(operation: &str, option: &str, expected: &str, actual: &str) -> Self {
        Self {
            kind: OperationValidationErrorKind::InvalidOptionType,
            operation: operation.to_string(),
            option: Some(option.to_string()),
            expected: Some(expected.to_string()),
            actual: Some(actual.to_string()),
        }
    }

    fn invalid_value(operation: &str, option: &str, expected: &str, actual: &Value) -> Self {
        Self {
            kind: OperationValidationErrorKind::InvalidOptionValue,
            operation: operation.to_string(),
            option: Some(option.to_string()),
            expected: Some(expected.to_string()),
            actual: Some(actual.to_string()),
        }
    }

    fn unknown_option(operation: &str, option: &str) -> Self {
        Self {
            kind: OperationValidationErrorKind::UnknownOption,
            operation: operation.to_string(),
            option: Some(option.to_string()),
            expected: Some("documented operation option".to_string()),
            actual: Some("unknown option".to_string()),
        }
    }
}

impl fmt::Display for OperationValidationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.option {
            Some(option) => write!(
                formatter,
                "operation {} option {} failed validation: expected {}, got {}",
                self.operation,
                option,
                self.expected.as_deref().unwrap_or("valid value"),
                self.actual.as_deref().unwrap_or("invalid value")
            ),
            None => write!(
                formatter,
                "operation {} failed validation: expected {}, got {}",
                self.operation,
                self.expected.as_deref().unwrap_or("valid value"),
                self.actual.as_deref().unwrap_or("invalid value")
            ),
        }
    }
}

impl StdError for OperationValidationError {}

#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OperationValidationErrorKind {
    OperationMismatch,
    MissingRequiredOption,
    InvalidOptionType,
    InvalidOptionValue,
    UnknownOption,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum OperationOptionsWire {
    Map(BTreeMap<String, OperationOption>),
    List(Vec<OperationOption>),
}

fn deserialize_options<'de, D>(
    deserializer: D,
) -> std::result::Result<BTreeMap<String, OperationOption>, D::Error>
where
    D: Deserializer<'de>,
{
    let wire = Option::<OperationOptionsWire>::deserialize(deserializer)?;
    let mut options = BTreeMap::new();

    match wire {
        Some(OperationOptionsWire::Map(map)) => {
            for (name, mut option) in map {
                if option.name.is_none() {
                    option.name = Some(name.clone());
                }
                options.insert(name, option);
            }
        }
        Some(OperationOptionsWire::List(list)) => {
            for option in list {
                if let Some(name) = option.name.clone() {
                    options.insert(name, option);
                }
            }
        }
        None => {}
    }

    Ok(options)
}

#[derive(Deserialize)]
#[serde(untagged)]
enum OperationEngineVersionWire {
    String(String),
    Object(OperationEngineVersion),
}

fn deserialize_engine_versions<'de, D>(
    deserializer: D,
) -> std::result::Result<Vec<OperationEngineVersion>, D::Error>
where
    D: Deserializer<'de>,
{
    let wire = Option::<Vec<OperationEngineVersionWire>>::deserialize(deserializer)?;
    Ok(wire
        .unwrap_or_default()
        .into_iter()
        .map(|version| match version {
            OperationEngineVersionWire::String(version) => OperationEngineVersion {
                version,
                default: None,
                latest: None,
                deprecated: None,
                experimental: None,
                extra: BTreeMap::new(),
            },
            OperationEngineVersionWire::Object(version) => version,
        })
        .collect())
}

fn is_common_task_field(name: &str) -> bool {
    matches!(
        name,
        "input"
            | "ignore_error"
            | "input_format"
            | "output_format"
            | "engine"
            | "engine_version"
            | "filename"
            | "timeout"
    )
}

fn value_type(value: &Value) -> &'static str {
    match value {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(number) if number.is_i64() || number.is_u64() => "integer",
        Value::Number(_) => "float",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "dictionary",
    }
}