arazzo-models 0.0.1

Rust models for the Arazzo Open API specification
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
//! Version 1.0.x specification models (https://spec.openapis.org/arazzo/v1.0.1.html)

use std::collections::HashMap;

#[cfg(feature = "yaml")] use anyhow::anyhow;
use itertools::Either;
use serde_json::Value;
#[cfg(feature = "yaml")] use yaml_rust2::Yaml;
#[cfg(feature = "yaml")] use yaml_rust2::yaml::Hash;

use crate::extensions::ExtensionValue;
#[cfg(feature = "yaml")] use crate::extensions::yaml_extract_extensions;
#[cfg(feature = "yaml")] use crate::yaml::{
  yaml_hash_entry_to_json,
  yaml_hash_lookup,
  yaml_hash_lookup_integer,
  yaml_hash_lookup_number,
  yaml_hash_lookup_string,
  yaml_hash_lookup_string_list,
  yaml_hash_require_string,
  yaml_type_name
};

/// 4.6.1 Arazzo Description is the root object of the loaded specification.
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#arazzo-description)
#[derive(Debug, Clone)]
pub struct ArazzoDescription {
  /// Version number of the Arazzo Specification
  pub arazzo: String,
  /// Metadata about API workflows defined in the Arazzo document
  pub info: Info,
  /// List of source descriptions
  pub source_descriptions: Vec<SourceDescription>,
  /// List of workflows
  pub workflows: Vec<Workflow>,
  /// An element to hold shared schemas.
  pub components: Components,
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>,
}

#[cfg(feature = "yaml")]
impl TryFrom<&Yaml> for ArazzoDescription {
  type Error = anyhow::Error;

  fn try_from(value: &Yaml) -> Result<Self, Self::Error> {
    if let Some(hash) = value.as_hash() {
      if let Ok(version) = yaml_hash_require_string(hash, "arazzo") {
        let info = Info::try_from(hash)?;
        let source_descriptions = yaml_load_source_descriptions(hash)?;
        let workflows = yaml_load_workflows(hash)?;
        let components = Components::try_from(hash)?;

        Ok(ArazzoDescription {
          arazzo: version,
          info,
          source_descriptions,
          workflows,
          components,
          extensions: yaml_extract_extensions(&hash)?
        })
      } else {
        Err(anyhow!("Arazzo version number is required [4.6.1.1 Fixed Fields]"))
      }
    } else {
      Err(anyhow!("YAML value must be a Hash, got {}", yaml_type_name(value)))
    }
  }
}

/// 4.6.2 Info Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#info-object)
#[derive(Debug, Clone)]
pub struct Info {
  /// A human-readable title of the Arazzo Description.
  pub title: String,
  /// A short summary of the Arazzo Description.
  pub summary: Option<String>,
  /// A description of the purpose of the workflows defined.
  pub description: Option<String>,
  /// Document version
  pub version: String,
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
impl TryFrom<&Hash> for Info {
  type Error = anyhow::Error;

  fn try_from(value: &Hash) -> Result<Self, Self::Error> {
    if let Some(hash) = yaml_hash_lookup(value, "info", |v | v.as_hash().cloned()) {
      Ok(Info {
        title: yaml_hash_require_string(&hash, "title")?,
        summary: yaml_hash_lookup_string(&hash, "summary"),
        description: yaml_hash_lookup_string(&hash, "description"),
        version: yaml_hash_require_string(&hash, "version")?,
        extensions: yaml_extract_extensions(&hash)?
      })
    } else {
      Err(anyhow!("Info Object is required [4.6.1.1 Fixed Fields]"))
    }
  }
}

/// 4.6.3 Source Description Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#source-description-object)
#[derive(Debug, Clone)]
pub struct SourceDescription {
  /// Unique name for the source description.
  pub name: String,
  /// URL to a source description to be used by a workflow.
  pub url: String,
  /// The type of source description.
  pub r#type: Option<String>,
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
fn yaml_load_source_descriptions(hash: &Hash) -> anyhow::Result<Vec<SourceDescription>> {
  if let Some(array) = yaml_hash_lookup(hash, "sourceDescriptions", |v | v.as_vec().cloned()) {
    if array.is_empty() {
      Err(anyhow!("Source Description list must have at least one entry [4.6.1.1 Fixed Fields]"))
    } else {
      let mut list = vec![];

      for item in &array {
        list.push(SourceDescription::try_from(item)?);
      }

      Ok(list)
    }
  } else {
    Err(anyhow!("Source Description Object is required [4.6.1.1 Fixed Fields]"))
  }
}

#[cfg(feature = "yaml")]
impl TryFrom<&Yaml> for SourceDescription {
  type Error = anyhow::Error;

  fn try_from(value: &Yaml) -> Result<Self, Self::Error> {
    if let Some(hash) = value.as_hash() {
      Ok(SourceDescription {
        name: yaml_hash_require_string(&hash, "name")?,
        url: yaml_hash_require_string(&hash, "url")?,
        r#type: yaml_hash_lookup_string(&hash, "type"),
        extensions: yaml_extract_extensions(&hash)?
      })
    } else {
      Err(anyhow!("YAML value must be a Hash, got {}", yaml_type_name(value)))
    }
  }
}

/// 4.6.4 Workflow Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#workflow-object)
#[derive(Debug, Clone)]
pub struct Workflow {
  /// Unique string to represent the workflow.
  pub workflow_id: String,
  /// Summary of the purpose or objective of the workflow.
  pub summary: Option<String>,
  /// Description of the workflow.
  pub description: Option<String>,
  /// JSON Schema 2020-12 object representing the input parameters used by this workflow.
  pub inputs: Value,
  /// List of workflows that must be completed before this workflow can be processed.
  pub depends_on: Vec<String>,
  /// An ordered list of workflow steps
  pub steps: Vec<Step>,
  /// List of success actions that are applicable for all steps described under this workflow.
  pub success_actions: Vec<Either<SuccessObject, ReusableObject>>,
  /// List of success actions that are applicable for all steps described under this workflow.
  pub failure_actions: Vec<Either<FailureObject, ReusableObject>>,
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
fn yaml_load_workflows(hash: &Hash) -> anyhow::Result<Vec<Workflow>> {
  if let Some(array) = yaml_hash_lookup(hash, "workflows", |v | v.as_vec().cloned()) {
    if array.is_empty() {
      Err(anyhow!("Workflows list must have at least one entry [4.6.1.1 Fixed Fields]"))
    } else {
      let mut list = vec![];

      for item in &array {
        list.push(Workflow::try_from(item)?);
      }

      Ok(list)
    }
  } else {
    Err(anyhow!("Workflow Object is required [4.6.1.1 Fixed Fields]"))
  }
}

#[cfg(feature = "yaml")]
impl TryFrom<&Yaml> for Workflow {
  type Error = anyhow::Error;

  fn try_from(value: &Yaml) -> Result<Self, Self::Error> {
    if let Some(hash) = value.as_hash() {
      Ok(Workflow {
        workflow_id: yaml_hash_require_string(&hash, "workflowId")?,
        summary: yaml_hash_lookup_string(&hash, "summary"),
        description: yaml_hash_lookup_string(&hash, "description"),
        inputs: yaml_hash_entry_to_json(&hash, "inputs")?,
        depends_on: yaml_hash_lookup_string_list(&hash, "dependsOn").unwrap_or_default(),
        steps: yaml_load_steps(hash)?,
        success_actions: yaml_load_success_actions(hash)?,
        failure_actions: yaml_load_failure_actions(hash)?,
        extensions: yaml_extract_extensions(&hash)?
      })
    } else {
      Err(anyhow!("YAML value must be a Hash, got {}", yaml_type_name(value)))
    }
  }
}

/// 4.6.5 Step Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#step-object)
#[derive(Debug, Clone)]
pub struct Step {
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
fn yaml_load_steps(hash: &Hash) -> anyhow::Result<Vec<Step>> {
  if let Some(array) = yaml_hash_lookup(hash, "steps", |v | v.as_vec().cloned()) {
    if array.is_empty() {
      Err(anyhow!("At lest one Step is required [4.6.4.1 Fixed Fields]"))
    } else {
      let mut list = vec![];

      for item in &array {
        list.push(Step::try_from(item)?);
      }

      Ok(list)
    }
  } else {
    Err(anyhow!("At lest one Step is required [4.6.4.1 Fixed Fields]"))
  }
}

#[cfg(feature = "yaml")]
impl TryFrom<&Yaml> for Step {
  type Error = anyhow::Error;

  fn try_from(value: &Yaml) -> Result<Self, Self::Error> {
    if let Some(hash) = value.as_hash() {
      Ok(Step {
        extensions: yaml_extract_extensions(&hash)?
      })
    } else {
      Err(anyhow!("YAML value must be a Hash, got {}", yaml_type_name(value)))
    }
  }
}

#[cfg(feature = "yaml")]
fn yaml_load_success_actions(hash: &Hash) -> anyhow::Result<Vec<Either<SuccessObject, ReusableObject>>> {
  if let Some(array) = yaml_hash_lookup(hash, "successActions", |v | v.as_vec().cloned()) {
    let mut list = vec![];

    for item in &array {
      if let Some(hash) = item.as_hash() {
        if hash.contains_key(&Yaml::String("reference".to_string())) {
          list.push(Either::Right(ReusableObject::try_from(hash)?));
        } else {
          list.push(Either::Left(SuccessObject::try_from(hash)?));
        }
      }
    }

    Ok(list)
  } else {
    Ok(vec![])
  }
}

#[cfg(feature = "yaml")]
fn yaml_load_failure_actions(hash: &Hash) -> anyhow::Result<Vec<Either<FailureObject, ReusableObject>>> {
  if let Some(array) = yaml_hash_lookup(hash, "failureActions", |v | v.as_vec().cloned()) {
    let mut list = vec![];

    for item in &array {
      if let Some(hash) = item.as_hash() {
        if hash.contains_key(&Yaml::String("reference".to_string())) {
          list.push(Either::Right(ReusableObject::try_from(hash)?));
        } else {
          list.push(Either::Left(FailureObject::try_from(hash)?));
        }
      }
    }

    Ok(list)
  } else {
    Ok(vec![])
  }
}

/// 4.6.7 Success Action Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#success-action-object)
#[derive(Debug, Clone)]
pub struct SuccessObject {
  /// The name of the success action.
  pub name: String,
  /// The type of action to take.
  pub r#type: String,
  /// The workflowId referencing an existing workflow within the Arazzo Description to transfer to
  /// upon success of the step.
  pub workflow_id: Option<String>,
  /// The stepId to transfer to upon success of the step.
  pub step_id: Option<String>,
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
impl TryFrom<&Hash> for SuccessObject {
  type Error = anyhow::Error;

  fn try_from(value: &Hash) -> Result<Self, Self::Error> {
    Ok(SuccessObject {
      name: yaml_hash_require_string(value, "name")?,
      r#type: yaml_hash_require_string(value, "type")?,
      workflow_id: yaml_hash_lookup_string(value, "workflowId"),
      step_id: yaml_hash_lookup_string(value, "stepId"),
      extensions: yaml_extract_extensions(value)?
    })
  }
}

/// 4.6.8 Failure Action Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#failure-action-object)
#[derive(Debug, Clone)]
pub struct FailureObject {
  /// The name of the success action.
  pub name: String,
  /// The type of action to take.
  pub r#type: String,
  /// The workflowId referencing an existing workflow within the Arazzo Description to transfer to
  /// upon success of the step.
  pub workflow_id: Option<String>,
  /// The stepId to transfer to upon success of the step.
  pub step_id: Option<String>,
  /// A non-negative decimal indicating the seconds to delay after the step failure before another
  /// attempt shall be made.
  pub retry_after: Option<f64>,
  /// A non-negative integer indicating how many attempts to retry the step may be attempted before
  /// failing the overall step.
  pub retry_limit: Option<i64>,
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
impl TryFrom<&Hash> for FailureObject {
  type Error = anyhow::Error;

  fn try_from(value: &Hash) -> Result<Self, Self::Error> {
    Ok(FailureObject {
      name: yaml_hash_require_string(value, "name")?,
      r#type: yaml_hash_require_string(value, "type")?,
      workflow_id: yaml_hash_lookup_string(value, "workflowId"),
      step_id: yaml_hash_lookup_string(value, "stepId"),
      retry_after: yaml_hash_lookup_number(value, "retryAfter"),
      retry_limit: yaml_hash_lookup_integer(value, "retryLimit"),
      extensions: yaml_extract_extensions(value)?
    })
  }
}

/// 4.6.9 Components Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#components-object)
#[derive(Debug, Clone, Default)]
pub struct Components {
  /// Extension values
  pub extensions: HashMap<String, ExtensionValue>
}

#[cfg(feature = "yaml")]
impl TryFrom<&Hash> for Components {
  type Error = anyhow::Error;

  fn try_from(value: &Hash) -> Result<Self, Self::Error> {
    if let Some(hash) = yaml_hash_lookup(value, "components", |v | v.as_hash().cloned()) {
      Ok(Components {
        extensions: yaml_extract_extensions(&hash)?
      })
    } else {
      Ok(Components::default())
    }
  }
}

/// 4.6.10 Reusable Object
/// [Reference](https://spec.openapis.org/arazzo/v1.0.1.html#reusable-object)
#[derive(Debug, Clone)]
pub struct ReusableObject {
  /// Runtime Expression used to reference the desired object.
  pub reference: String,
  /// Sets a value of the referenced parameter.
  pub value: Option<String>
}

#[cfg(feature = "yaml")]
impl TryFrom<&Hash> for ReusableObject {
  type Error = anyhow::Error;

  fn try_from(value: &Hash) -> Result<Self, Self::Error> {
    if let Ok(reference) = yaml_hash_require_string(value, "reference") {
      Ok(ReusableObject {
        reference,
        value: yaml_hash_lookup_string(value, "value")
      })
    } else {
      Err(anyhow!("Reference is required [4.6.10.1 Fixed Fields]"))
    }
  }
}

#[cfg(test)]
#[cfg(feature = "yaml")]
mod yaml_tests {
  use expectest::prelude::*;
  use maplit::hashmap;
  use yaml_rust2::Yaml;
  use yaml_rust2::yaml::Hash;

  use crate::extensions::ExtensionValue;
  use crate::v1_0::*;

  #[test]
  fn fails_to_load_if_the_main_value_is_not_a_yaml_hash() {
    expect!(ArazzoDescription::try_from(&Yaml::String("test".to_string()))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_version_is_missing() {
    expect!(ArazzoDescription::try_from(&Yaml::Hash(Hash::new()))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_version_is_not_a_string() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::Hash(Hash::new()));
    expect!(ArazzoDescription::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_info_is_missing() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::String("1.0.0".to_string()));
    expect!(ArazzoDescription::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_source_descriptions_are_missing() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::String("1.0.0".to_string()));
    hash.insert(Yaml::String("info".to_string()), Yaml::Hash(info_fixture()));
    expect!(ArazzoDescription::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_source_descriptions_are_empty() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::String("1.0.0".to_string()));
    hash.insert(Yaml::String("info".to_string()), Yaml::Hash(info_fixture()));
    hash.insert(Yaml::String("sourceDescriptions".to_string()), Yaml::Array(vec![]));
    expect!(ArazzoDescription::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_workflows_are_missing() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::String("1.0.0".to_string()));
    hash.insert(Yaml::String("info".to_string()), Yaml::Hash(info_fixture()));
    hash.insert(Yaml::String("sourceDescriptions".to_string()), Yaml::Array(source_descriptions_fixture()));
    expect!(ArazzoDescription::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn fails_to_load_if_the_workflows_are_empty() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::String("1.0.0".to_string()));
    hash.insert(Yaml::String("info".to_string()), Yaml::Hash(info_fixture()));
    hash.insert(Yaml::String("sourceDescriptions".to_string()), Yaml::Array(source_descriptions_fixture()));
    hash.insert(Yaml::String("workflows".to_string()), Yaml::Array(vec![]));
    expect!(ArazzoDescription::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn arazzo_description_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("arazzo".to_string()), Yaml::String("1.0.0".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    hash.insert(Yaml::String("info".to_string()), Yaml::Hash(info_fixture()));
    hash.insert(Yaml::String("sourceDescriptions".to_string()), Yaml::Array(source_descriptions_fixture()));
    hash.insert(Yaml::String("workflows".to_string()), Yaml::Array(workflows_fixture()));

    let desc = ArazzoDescription::try_from(&Yaml::Hash(hash)).unwrap();
    expect!(desc.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  fn info_fixture() -> Hash {
    let mut info = Hash::new();
    info.insert(Yaml::String("title".to_string()), Yaml::String("test".to_string()));
    info.insert(Yaml::String("version".to_string()), Yaml::String("1.0.0".to_string()));
    info
  }

  fn source_descriptions_fixture() -> Vec<Yaml> {
    let mut desc = Hash::new();
    desc.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    desc.insert(Yaml::String("url".to_string()), Yaml::String("http://test".to_string()));
    vec![Yaml::Hash(desc)]
  }

  fn workflows_fixture() -> Vec<Yaml> {
    let mut wf = Hash::new();
    wf.insert(Yaml::String("workflowId".to_string()), Yaml::String("test".to_string()));
    wf.insert(Yaml::String("steps".to_string()), Yaml::Array(steps_fixture()));
    vec![Yaml::Hash(wf)]
  }

  fn steps_fixture() -> Vec<Yaml> {
    let mut desc = Hash::new();
    desc.insert(Yaml::String("stepId".to_string()), Yaml::String("test".to_string()));
    vec![Yaml::Hash(desc)]
  }

  #[test]
  fn info_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("title".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("version".to_string()), Yaml::String("1.0.0".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let mut outer = Hash::new();
    outer.insert(Yaml::String("info".to_string()), Yaml::Hash(hash));
    let info = Info::try_from(&outer).unwrap();
    expect!(info.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn source_description_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("url".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let desc = SourceDescription::try_from(&Yaml::Hash(hash)).unwrap();
    expect!(desc.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn workflow_fails_to_load_if_there_are_no_steps() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("workflowId".to_string()), Yaml::String("test".to_string()));

    expect!(Workflow::try_from(&Yaml::Hash(hash.clone()))).to(be_err());

    hash.insert(Yaml::String("steps".to_string()), Yaml::Array(vec![]));
    expect!(Workflow::try_from(&Yaml::Hash(hash))).to(be_err());
  }

  #[test]
  fn workflow_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("workflowId".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("steps".to_string()), Yaml::Array(steps_fixture()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let wf = Workflow::try_from(&Yaml::Hash(hash)).unwrap();
    expect!(wf.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn steps_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("stepId".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let step = Step::try_from(&Yaml::Hash(hash)).unwrap();
    expect!(step.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn components_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("workflowId".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let mut outer = Hash::new();
    outer.insert(Yaml::String("components".to_string()), Yaml::Hash(hash));

    let components = Components::try_from(&outer).unwrap();
    expect!(components.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn load_success_object() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("type".to_string()), Yaml::String("end".to_string()));
    hash.insert(Yaml::String("workflowId".to_string()), Yaml::String("workflowId".to_string()));
    hash.insert(Yaml::String("stepId".to_string()), Yaml::String("stepId".to_string()));

    let success = SuccessObject::try_from(&hash).unwrap();
    expect!(&success.name).to(be_equal_to("test"));
    expect!(&success.r#type).to(be_equal_to("end"));
    expect!(success.workflow_id.clone()).to(be_some().value("workflowId"));
    expect!(success.step_id.clone()).to(be_some().value("stepId"));

    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("type".to_string()), Yaml::String("end".to_string()));

    let success = SuccessObject::try_from(&hash).unwrap();
    expect!(&success.name).to(be_equal_to("test"));
    expect!(&success.r#type).to(be_equal_to("end"));
    expect!(success.workflow_id.clone()).to(be_none());
    expect!(success.step_id.clone()).to(be_none());
  }

  #[test]
  fn success_object_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("type".to_string()), Yaml::String("end".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let success = SuccessObject::try_from(&hash).unwrap();
    expect!(success.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn load_failure_object() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("type".to_string()), Yaml::String("end".to_string()));
    hash.insert(Yaml::String("workflowId".to_string()), Yaml::String("workflowId".to_string()));
    hash.insert(Yaml::String("stepId".to_string()), Yaml::String("stepId".to_string()));
    hash.insert(Yaml::String("retryAfter".to_string()), Yaml::Real("10.5".to_string()));
    hash.insert(Yaml::String("retryLimit".to_string()), Yaml::Integer(10));

    let failure = FailureObject::try_from(&hash).unwrap();
    expect!(&failure.name).to(be_equal_to("test"));
    expect!(&failure.r#type).to(be_equal_to("end"));
    expect!(failure.workflow_id.clone()).to(be_some().value("workflowId"));
    expect!(failure.step_id.clone()).to(be_some().value("stepId"));
    expect!(failure.retry_after.clone()).to(be_some().value(10.5));
    expect!(failure.retry_limit.clone()).to(be_some().value(10));

    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("type".to_string()), Yaml::String("end".to_string()));

    let failure = FailureObject::try_from(&hash).unwrap();
    expect!(&failure.name).to(be_equal_to("test"));
    expect!(&failure.r#type).to(be_equal_to("end"));
    expect!(failure.workflow_id.clone()).to(be_none());
    expect!(failure.step_id.clone()).to(be_none());
    expect!(failure.retry_after.clone()).to(be_none());
    expect!(failure.retry_limit.clone()).to(be_none());
  }

  #[test]
  fn failure_object_supports_extensions() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("name".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("type".to_string()), Yaml::String("end".to_string()));
    hash.insert(Yaml::String("x-one".to_string()), Yaml::String("1".to_string()));
    hash.insert(Yaml::String("x-two".to_string()), Yaml::Integer(2));

    let failure = FailureObject::try_from(&hash).unwrap();
    expect!(failure.extensions).to(be_equal_to(hashmap!{
      "one".to_string() => ExtensionValue::String("1".to_string()),
      "two".to_string() => ExtensionValue::Integer(2)
    }));
  }

  #[test]
  fn load_reusable_object() {
    let mut hash = Hash::new();
    hash.insert(Yaml::String("reference".to_string()), Yaml::String("$test".to_string()));
    hash.insert(Yaml::String("value".to_string()), Yaml::String("test".to_string()));
    hash.insert(Yaml::String("workflowId".to_string()), Yaml::String("workflowId".to_string()));

    let obj = ReusableObject::try_from(&hash).unwrap();
    expect!(&obj.reference).to(be_equal_to("$test"));
    expect!(obj.value.clone()).to(be_some().value("test"));

    let mut hash = Hash::new();
    hash.insert(Yaml::String("reference".to_string()), Yaml::String("$test".to_string()));

    let obj = ReusableObject::try_from(&hash).unwrap();
    expect!(&obj.reference).to(be_equal_to("$test"));
    expect!(obj.value.clone()).to(be_none());
  }
}