Enum pact_models::PactSpecification
source · #[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§
source§impl PactSpecification
impl PactSpecification
sourcepub fn version_str(&self) -> String
pub fn version_str(&self) -> String
Returns the semantic version string of the specification version.
Examples found in repository?
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
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() },
}
}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)
}sourcepub fn parse_version<S: Into<String>>(input: S) -> Result<PactSpecification>
pub fn parse_version<S: Into<String>>(input: S) -> Result<PactSpecification>
Parse a version string into a PactSpecification
Examples found in repository?
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§
source§impl Clone for PactSpecification
impl Clone for PactSpecification
source§fn clone(&self) -> PactSpecification
fn clone(&self) -> PactSpecification
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for PactSpecification
impl Debug for PactSpecification
source§impl Default for PactSpecification
impl Default for PactSpecification
source§impl<'de> Deserialize<'de> for PactSpecification
impl<'de> Deserialize<'de> for PactSpecification
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Display for PactSpecification
impl Display for PactSpecification
source§impl From<&String> for PactSpecification
impl From<&String> for PactSpecification
source§impl From<&str> for PactSpecification
impl From<&str> for PactSpecification
source§impl From<String> for PactSpecification
impl From<String> for PactSpecification
source§impl PartialEq<PactSpecification> for PactSpecification
impl PartialEq<PactSpecification> for PactSpecification
source§fn eq(&self, other: &PactSpecification) -> bool
fn eq(&self, other: &PactSpecification) -> bool
self and other values to be equal, and is used
by ==.source§impl PartialOrd<PactSpecification> for PactSpecification
impl PartialOrd<PactSpecification> for PactSpecification
source§fn partial_cmp(&self, other: &PactSpecification) -> Option<Ordering>
fn partial_cmp(&self, other: &PactSpecification) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more