pact_plugin_driver/
verification.rs1use std::collections::HashMap;
4
5use bytes::Bytes;
6use itertools::Either;
7use pact_models::prelude::OptionalBody;
8use serde_json::Value;
9
10use crate::proto::{VerificationResult, verification_result_item};
11
12#[derive(Clone, Debug, Default)]
14pub struct InteractionVerificationData {
15 pub request_data: OptionalBody,
17 pub metadata: HashMap<String, Either<Value, Bytes>>
19}
20
21impl InteractionVerificationData {
22 pub fn new(request_data: OptionalBody, metadata: HashMap<String, Either<Value, Bytes>>) -> Self {
24 InteractionVerificationData {
25 request_data,
26 metadata,
27 }
28 }
29}
30
31#[derive(Clone, Debug, Default)]
33pub struct InteractionVerificationResult {
34 pub ok: bool,
36 pub details: Vec<InteractionVerificationDetails>,
38 pub output: Vec<String>
40}
41
42#[derive(Clone, Debug)]
44pub enum InteractionVerificationDetails {
45 Error(String),
47
48 Mismatch {
50 expected: Bytes,
51 actual: Bytes,
52 mismatch: String,
53 path: String
54 }
55}
56
57impl From<&VerificationResult> for InteractionVerificationResult {
58 fn from(result: &VerificationResult) -> Self {
59 InteractionVerificationResult {
60 ok: result.success,
61 details: result.mismatches.iter()
62 .filter_map(|r| r.result.as_ref().map(|r| match r {
63 verification_result_item::Result::Error(err) => InteractionVerificationDetails::Error(err.to_string()),
64 verification_result_item::Result::Mismatch(mismatch) => InteractionVerificationDetails::Mismatch {
65 expected: mismatch.expected.clone().map(|b| Bytes::from(b)).unwrap_or_default(),
66 actual: mismatch.actual.clone().map(|b| Bytes::from(b)).unwrap_or_default(),
67 mismatch: mismatch.mismatch.to_string(),
68 path: mismatch.path.to_string()
69 }
70 }))
71 .collect(),
72 output: result.output.clone()
73 }
74 }
75}