using System.Collections.Generic;
using System.IO;
namespace SabreTools.Matching
{
///
/// A set of path matches that work together
///
public class PathMatchSet : IMatchSet
{
///
public List Matchers { get; }
///
public string SetName { get; }
///
/// Function to get a path version for this Matcher
///
///
/// A path version method takes the matched path and an enumerable of files
/// and returns a single string. That string is either a version string,
/// in which case it will be appended to the match name, or `null`,
/// in which case it will cause the match name to be omitted.
///
public GetPathVersion? GetVersion { get; }
#region Generic Constructors
///
/// Constructor
///
/// PathMatch representing the comparisons
/// Unique name for the set
public PathMatchSet(PathMatch needle, string setName)
: this([needle], setName) { }
///
/// Constructor
///
/// List of PathMatch objects representing the comparisons
/// Unique name for the set
///
/// Thrown if is empty.
///
public PathMatchSet(List needles, string setName)
{
// Validate the inputs
if (needles.Count == 0)
throw new InvalidDataException(nameof(needles));
Matchers = needles;
SetName = setName;
GetVersion = null;
}
#endregion
#region Version Constructors
///
/// Constructor
///
/// PathMatch representing the comparisons
/// Delegate for deriving a version on match
/// Unique name for the set
public PathMatchSet(PathMatch needle, GetPathVersion getVersion, string setName)
: this([needle], getVersion, setName) { }
///
/// Constructor
///
/// List of PathMatch objects representing the comparisons
/// Delegate for deriving a version on match
/// Unique name for the set
///
/// Thrown if is empty.
///
public PathMatchSet(List needles, GetPathVersion getVersion, string setName)
{
// Validate the inputs
if (needles.Count == 0)
throw new InvalidDataException(nameof(needles));
Matchers = needles;
SetName = setName;
GetVersion = getVersion;
}
#endregion
#region Matching
///
/// Get if this match can be found in a stack
///
/// List of strings to search for the given content
/// Matched item on success, null on error
public List MatchesAll(string[]? stack)
=> MatchesAll(stack is null ? null : new List(stack));
///
/// Determine whether all path matches pass
///
/// List of strings to try to match
/// List of matching values, if any
public List MatchesAll(List? stack)
{
// If either set is null or empty, we can't do anything
if (stack is null || stack.Count == 0 || Matchers.Count == 0)
return [];
// Initialize the value list
List values = [];
// Loop through all path matches and make sure all pass
foreach (var pathMatch in Matchers)
{
string? value = pathMatch.Match(stack);
if (value is null)
return [];
else
values.Add(value);
}
return values;
}
///
/// Get if this match can be found in a stack
///
/// List of strings to search for the given content
/// Matched item on success, null on error
public string? MatchesAny(string[]? stack)
=> MatchesAny(stack is null ? null : new List(stack));
///
/// Determine whether any path matches pass
///
/// List of strings to try to match
/// First matching value on success, null on error
public string? MatchesAny(List? stack)
{
// If either set is null or empty, we can't do anything
if (stack is null || stack.Count == 0 || Matchers.Count == 0)
return null;
// Loop through all path matches and make sure all pass
foreach (var pathMatch in Matchers)
{
string? value = pathMatch.Match(stack);
if (value is not null)
return value;
}
return null;
}
#endregion
}
}