Skip to content

Commit

Permalink
chore: Add From implementation for CommonMismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Apr 16, 2024
1 parent 05c8d53 commit 3706f6e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 61 additions & 1 deletion rust/pact_matching/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ use pact_models::v4::message_parts::MessageContents;
use pact_models::v4::sync_message::SynchronousMessage;
#[cfg(feature = "plugins")] use pact_plugin_driver::catalogue_manager::find_content_matcher;
#[cfg(feature = "plugins")] use pact_plugin_driver::plugin_models::PluginInteractionConfig;
use serde::__private::from_utf8_lossy;
use serde_json::{json, Value};
use tracing::{debug, error, info, instrument, trace, warn};

Expand Down Expand Up @@ -763,9 +764,13 @@ fn match_xml(
/// Store common mismatch information so it can be converted to different type of mismatches
#[derive(Debug, Clone, PartialOrd, Ord, Eq)]
pub struct CommonMismatch {
path: String,
/// path expression to where the mismatch occurred
pub path: String,
/// expected value (as a string)
expected: String,
/// actual value (as a string)
actual: String,
/// Description of the mismatch
description: String
}

Expand Down Expand Up @@ -813,6 +818,61 @@ impl PartialEq for CommonMismatch {
}
}

impl From<Mismatch> for CommonMismatch {
fn from(value: Mismatch) -> Self {
match value {
Mismatch::MethodMismatch { expected, actual } => CommonMismatch {
path: "".to_string(),
expected: expected.clone(),
actual: actual.clone(),
description: "Method mismatch".to_string()
},
Mismatch::PathMismatch { expected, actual, mismatch } => CommonMismatch {
path: "".to_string(),
expected: expected.clone(),
actual: actual.clone(),
description: mismatch.clone()
},
Mismatch::StatusMismatch { expected, actual, mismatch } => CommonMismatch {
path: "".to_string(),
expected: expected.to_string(),
actual: actual.to_string(),
description: mismatch.clone()
},
Mismatch::QueryMismatch { parameter, expected, actual, mismatch } => CommonMismatch {
path: parameter.clone(),
expected: expected.clone(),
actual: actual.clone(),
description: mismatch.clone()
},
Mismatch::HeaderMismatch { key, expected, actual, mismatch } => CommonMismatch {
path: key.clone(),
expected: expected.clone(),
actual: actual.clone(),
description: mismatch.clone()
},
Mismatch::BodyTypeMismatch { expected, actual, mismatch, .. } => CommonMismatch {
path: "".to_string(),
expected: expected.clone(),
actual: actual.clone(),
description: mismatch.clone()
},
Mismatch::BodyMismatch { path, expected, actual, mismatch } => CommonMismatch {
path: path.clone(),
expected: from_utf8_lossy(expected.unwrap_or_default().as_ref()).to_string(),
actual: from_utf8_lossy(actual.unwrap_or_default().as_ref()).to_string(),
description: mismatch.clone()
},
Mismatch::MetadataMismatch { key, expected, actual, mismatch } => CommonMismatch {
path: key.clone(),
expected: expected.clone(),
actual: actual.clone(),
description: mismatch.clone()
}
}
}
}

/// Enum that defines the different types of mismatches that can occur.
#[derive(Debug, Clone, PartialOrd, Ord, Eq)]
pub enum Mismatch {
Expand Down

0 comments on commit 3706f6e

Please sign in to comment.