#[repr(C)]
pub enum PactSpecification {
    Unknown,
    V1,
    V1_1,
    V2,
    V3,
    V4,
}
Expand description

Enum defining the pact specification versions supported by the library

Variants§

§

Unknown

Unknown or unsupported specification version

§

V1

First version of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-1)

§

V1_1

Second version of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-1.1)

§

V2

Version two of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-2)

§

V3

Version three of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-3)

§

V4

Version four of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-4)

Implementations§

Returns the semantic version string of the specification version.

Examples found in repository?
src/sync_pact.rs (line 203)
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
  pub fn metadata_to_json(&self, pact_spec: &PactSpecification) -> BTreeMap<String, Value> {
    let mut md_map: BTreeMap<String, Value> = self.metadata.iter()
      .map(|(k, v)| {
        let key = match k.as_str() {
          "pact-specification" => "pactSpecification".to_string(),
          "pact-rust" => "pactRust".to_string(),
          _ => k.clone()
        };
        (key, json!(v.iter()
                  .map(|(k, v)| (k.clone(), v.clone()))
                  .collect::<BTreeMap<String, String>>()))
      })
      .collect();

    md_map.insert("pactSpecification".to_string(), json!({"version" : pact_spec.version_str()}));

    let version_entry = md_map.entry("pactRust".to_string())
      .or_insert(Value::Object(Map::default()));
    if let Value::Object(map) = version_entry {
      map.insert("models".to_string(), Value::String(PACT_RUST_VERSION.unwrap_or("unknown").to_string()));
    }

    md_map
  }

  /// Reads the pact file from a URL and parses the resulting JSON into a `Pact` struct
  #[cfg(not(target_family = "wasm"))]
  pub fn from_url(url: &str, auth: &Option<HttpAuth>) -> anyhow::Result<RequestResponsePact> {
    let (url, json) = http_utils::fetch_json_from_url(&url.to_string(), auth)?;
    RequestResponsePact::from_json(&url, &json)
  }

  /// Returns a default RequestResponsePact struct
  pub fn default() -> RequestResponsePact {
    RequestResponsePact {
      consumer: Consumer { name: "default_consumer".to_string() },
      provider: Provider { name: "default_provider".to_string() },
      interactions: Vec::new(),
      metadata: RequestResponsePact::default_metadata(),
      specification_version: PactSpecification::V3
    }
  }

  /// Returns the default metadata
  pub fn default_metadata() -> BTreeMap<String, BTreeMap<String, String>> {
    btreemap!{
      "pactSpecification".to_string() => btreemap!{ "version".to_string() => PactSpecification::V3.version_str() },
      "pactRust".to_string() => btreemap!{ "models".to_string() => PACT_RUST_VERSION.unwrap_or("unknown").to_string() }
    }
  }
More examples
Hide additional examples
src/message_pact.rs (line 226)
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
    pub fn metadata_to_json(&self, pact_spec: &PactSpecification) -> BTreeMap<String, Value> {
        let mut md_map: BTreeMap<String, Value> = self.metadata.iter()
            .map(|(k, v)| {
                let key = match k.as_str() {
                  "pact-specification" => "pactSpecification".to_string(),
                  "pact-rust" => "pactRust".to_string(),
                  _ => k.clone()
                };
                (key, json!(v.iter()
                  .map(|(k, v)| (k.clone(), v.clone()))
                  .collect::<BTreeMap<String, String>>()))
            })
            .collect();

        md_map.insert(
            "pactSpecification".to_string(),
            json!({"version" : pact_spec.version_str()}));
        let version_entry = md_map.entry("pactRust".to_string())
          .or_insert(Value::Object(Map::default()));
        if let Value::Object(map) = version_entry {
          map.insert("models".to_string(), Value::String(PACT_RUST_VERSION.unwrap_or("unknown").to_string()));
        }
        md_map
    }

    /// Determines the default file name for the pact.
    /// This is based on the consumer and provider names.
    pub fn default_file_name(&self) -> String {
        format!("{}-{}.json", self.consumer.name, self.provider.name)
    }

    /// Reads the pact file from a URL and parses the resulting JSON
    /// into a `MessagePact` struct
    #[cfg(not(target_family = "wasm"))]
    pub fn from_url(url: &String, auth: &Option<HttpAuth>) -> anyhow::Result<MessagePact> {
        let (url, json) = http_utils::fetch_json_from_url(url, auth)?;
        MessagePact::from_json(&url, &json)
    }

    /// Writes this pact out to the provided file path.
    /// All directories in the path will automatically created.
    /// If there is already a file at the path, it will be overwritten.
    #[cfg(not(target_family = "wasm"))]
    pub fn overwrite_pact(
        &self,
        path: &Path,
        pact_spec: PactSpecification,
    ) -> anyhow::Result<()> {
        fs::create_dir_all(path.parent().unwrap())?;

        debug!("Writing new pact file to {:?}", path);
        let mut file = File::create(path)?;

        file.write_all(
          serde_json::to_string_pretty(&self.to_json(pact_spec)?)?.as_bytes()
        )?;

        Ok(())
    }

    /// Returns a default MessagePact struct
    pub fn default() -> MessagePact {
        MessagePact {
            consumer: Consumer { name: "default_consumer".to_string() },
            provider: Provider { name: "default_provider".to_string() },
            messages: Vec::new(),
            metadata: MessagePact::default_metadata(),
            specification_version: PactSpecification::V3,
        }
    }

    /// Returns the default metadata
    pub fn default_metadata()
    -> BTreeMap<String, BTreeMap<String, String>> {
        btreemap!{
            "pact-specification".to_string() =>
                btreemap!{ "version".to_string() =>
                    PactSpecification::V3.version_str() },
            "pact-rust".to_string() =>
                btreemap!{ "version".to_string() =>
                    PACT_RUST_VERSION.unwrap_or("unknown").to_string() },
        }
    }
src/v4/pact.rs (line 56)
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
  fn metadata_to_json(&self) -> Value {
    let mut md_map: serde_json::Map<String, Value> = self.metadata.iter()
      .map(|(k, v)| {
        let key = match k.as_str() {
          "pact-specification" => "pactSpecification".to_string(),
          "pact-rust" => "pactRust".to_string(),
          _ => k.clone()
        };
        (key, v.clone())
      })
      .collect();

    md_map.insert("pactSpecification".to_string(), json!({"version" : PactSpecification::V4.version_str()}));

    let version_entry = md_map.entry("pactRust")
      .or_insert(Value::Object(Map::default()));
    if let Value::Object(map) = version_entry {
      map.insert("models".to_string(), Value::String(PACT_RUST_VERSION.unwrap_or("unknown").to_string()));
    }

    if !self.plugin_data.is_empty() {
      let mut v = vec![];
      for plugin in &self.plugin_data {
        match plugin.to_json() {
          Ok(json) => v.push(json),
          Err(err) => warn!("Could not convert plugin data to JSON - {}", err)
        }
      }
      md_map.insert("plugins".to_string(), Value::Array(v));
    }

    Value::Object(md_map)
  }

Parse a version string into a PactSpecification

Examples found in repository?
src/pact.rs (line 405)
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
pub(crate) fn verify_metadata(metadata: &Value, _spec_version: PactSpecification) -> Vec<PactFileVerificationResult> {
  let mut results = vec![];

  match metadata {
    Value::Object(values) => {
      let spec_value = if let Some(spec_value) = values.get("pactSpecification") {
        Some(spec_value)
      } else if let Some(spec_value) = values.get("pact-specification") {
        results.push(PactFileVerificationResult::new("/metadata", ResultLevel::WARNING,
                                                     &format!("'pact-specification' is deprecated, use 'pactSpecification' instead")));
        Some(spec_value)
      } else {
        None
      };

      if values.contains_key("pactSpecificationVersion") {
        results.push(PactFileVerificationResult::new("/metadata", ResultLevel::WARNING,
                                                     &format!("'pactSpecificationVersion' is deprecated, use 'pactSpecification/version' instead")));
      }

      if let Some(spec) = spec_value {
        match spec {
          Value::Object(values) => {
            if let Some(version) = values.get("version") {
              match version {
                Value::Null => results.push(PactFileVerificationResult::new("/metadata/pactSpecification/version", ResultLevel::WARNING,
                                                                            &format!("pactSpecification version is NULL"))),
                Value::String(version) => if PactSpecification::parse_version(version).is_err() {
                  results.push(PactFileVerificationResult::new("/metadata/pactSpecification/version", ResultLevel::ERROR,
                                                               &format!("'{}' is not a valid Pact specification version", version)))
                }
                _ => results.push(PactFileVerificationResult::new("/metadata/pactSpecification/version", ResultLevel::ERROR,
                                                                  &format!("Version must be a String, got {}", json_type_of(version))))
              }
            } else {
              results.push(PactFileVerificationResult::new("/metadata/pactSpecification", ResultLevel::WARNING,
                                                           &format!("pactSpecification is missing the version attribute")));
            }
          }
          _ => results.push(PactFileVerificationResult::new("/metadata/pactSpecification", ResultLevel::ERROR,
                                                            &format!("pactSpecification must be an Object, got {}", json_type_of(spec))))
        }
      }
    }
    _ => results.push(PactFileVerificationResult::new("/metadata", ResultLevel::ERROR,
                                                      &format!("Metadata must be an Object, got {}", json_type_of(metadata))))
  }

  results
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Give this value the specified foreground colour
Give this value the specified background colour

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more