using Mono.Cecil;
using MoreLinq;
using ReCodeIt.Enums;
using ReCodeIt.Models;
namespace ReCodeIt.ReMapper.Search;
internal static class Fields
{
///
/// Returns a match on any type with the provided fields
///
///
///
///
///
public static EMatchResult Include(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IncludeFields is null || parms.IncludeFields.Count == 0) return EMatchResult.Disabled;
var matches = type.Fields
.Where(field => parms.IncludeFields.Contains(field.Name));
score.Score += matches.Count();
score.FailureReason = matches.Any() ? EFailureReason.None : EFailureReason.FieldsInclude;
return matches.Any()
? EMatchResult.Match
: EMatchResult.NoMatch;
}
///
/// Returns a match on any type without the provided fields
///
///
///
///
///
public static EMatchResult Exclude(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.ExcludeFields is null || parms.ExcludeFields.Count == 0) return EMatchResult.Disabled;
var matches = type.Fields
.Where(field => parms.ExcludeFields.Contains(field.Name))
.Count();
score.Score += matches;
score.FailureReason = matches > 0 ? EFailureReason.None : EFailureReason.FieldsExclude;
return matches > 0
? EMatchResult.NoMatch
: EMatchResult.Match;
}
///
/// Returns a match on any type with a matching number of fields
///
///
///
///
///
public static EMatchResult Count(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.FieldCount is null) return EMatchResult.Disabled;
var match = type.Fields.Exactly((int)parms.FieldCount);
if (match) { score.Score++; }
score.FailureReason = match ? EFailureReason.None : EFailureReason.FieldsCount;
return match
? EMatchResult.Match
: EMatchResult.NoMatch;
}
}