77 lines
2.4 KiB
C#
Raw Normal View History

2024-06-17 11:34:06 -04:00
using Mono.Cecil;
2024-06-13 07:45:40 -04:00
using MoreLinq;
2024-06-17 11:34:06 -04:00
using ReCodeIt.Enums;
using ReCodeIt.Models;
2024-06-13 07:45:40 -04:00
2024-06-14 19:06:21 -04:00
namespace ReCodeIt.ReMapper.Search;
2024-06-13 04:56:08 -04:00
internal static class Fields
{
2024-06-13 07:45:40 -04:00
/// <summary>
/// Returns a match on any type with the provided fields
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns></returns>
2024-06-17 11:34:06 -04:00
public static EMatchResult Include(TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-13 07:45:40 -04:00
{
2024-06-13 12:32:21 -04:00
if (parms.IncludeFields is null || parms.IncludeFields.Count == 0) return EMatchResult.Disabled;
2024-06-13 07:45:40 -04:00
var matches = type.Fields
2024-06-13 12:32:21 -04:00
.Where(field => parms.IncludeFields.Contains(field.Name));
score.Score += matches.Count();
2024-06-13 07:45:40 -04:00
2024-06-14 16:18:43 -04:00
score.FailureReason = matches.Any() ? EFailureReason.None : EFailureReason.FieldsInclude;
2024-06-13 12:32:21 -04:00
return matches.Any()
2024-06-13 07:45:40 -04:00
? EMatchResult.Match
: EMatchResult.NoMatch;
}
/// <summary>
/// Returns a match on any type without the provided fields
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns></returns>
2024-06-17 11:34:06 -04:00
public static EMatchResult Exclude(TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-13 07:45:40 -04:00
{
2024-06-13 12:32:21 -04:00
if (parms.ExcludeFields is null || parms.ExcludeFields.Count == 0) return EMatchResult.Disabled;
2024-06-13 07:45:40 -04:00
var matches = type.Fields
2024-06-13 12:32:21 -04:00
.Where(field => parms.ExcludeFields.Contains(field.Name))
2024-06-13 07:45:40 -04:00
.Count();
score.Score += matches;
2024-06-14 16:18:43 -04:00
score.FailureReason = matches > 0 ? EFailureReason.None : EFailureReason.FieldsExclude;
2024-06-13 07:45:40 -04:00
return matches > 0
2024-06-13 08:18:16 -04:00
? EMatchResult.NoMatch
: EMatchResult.Match;
2024-06-13 07:45:40 -04:00
}
/// <summary>
/// Returns a match on any type with a matching number of fields
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns></returns>
2024-06-17 11:34:06 -04:00
public static EMatchResult Count(TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-13 07:45:40 -04:00
{
if (parms.FieldCount is null) return EMatchResult.Disabled;
var match = type.Fields.Exactly((int)parms.FieldCount);
if (match) { score.Score++; }
2024-06-14 16:18:43 -04:00
score.FailureReason = match ? EFailureReason.None : EFailureReason.FieldsCount;
2024-06-13 07:45:40 -04:00
return match
? EMatchResult.Match
: EMatchResult.NoMatch;
}
2024-06-13 04:56:08 -04:00
}