// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Linq;
using Stef.Validation;
using WireMock.Extensions;
namespace WireMock.Matchers;
///
/// The MatchResult which contains the score (value between 0.0 - 1.0 of the similarity) and an optional error message.
///
public struct MatchResult
{
///
/// A value between 0.0 - 1.0 of the similarity.
///
public double Score { get; set; }
///
/// The exception message) in case the matching fails.
/// [Optional]
///
public Exception? Exception { get; set; }
///
/// Create a MatchResult
///
/// A value between 0.0 - 1.0 of the similarity.
/// The exception in case the matching fails. [Optional]
public MatchResult(double score, Exception? exception = null)
{
Score = score;
Exception = exception;
}
///
/// Create a MatchResult
///
/// The exception in case the matching fails.
public MatchResult(Exception exception)
{
Exception = Guard.NotNull(exception);
}
///
/// Implicitly converts a double to a MatchResult.
///
/// The score
public static implicit operator MatchResult(double score)
{
return new MatchResult(score);
}
///
/// Is the value a perfect match?
///
public bool IsPerfect() => MatchScores.IsPerfect(Score);
///
/// Create a MatchResult from multiple MatchResults.
///
/// A list of MatchResults.
/// The MatchOperator
/// MatchResult
public static MatchResult From(IReadOnlyList matchResults, MatchOperator matchOperator)
{
Guard.NotNullOrEmpty(matchResults);
if (matchResults.Count == 1)
{
return matchResults[0];
}
return new MatchResult
{
Score = MatchScores.ToScore(matchResults.Select(r => r.Score).ToArray(), matchOperator),
Exception = matchResults.Select(m => m.Exception).OfType().ToArray().ToException()
};
}
///
/// Expand to Tuple
///
/// Tuple : Score and Exception
public (double Score, Exception? Exception) Expand()
{
return (Score, Exception);
}
}