using System; using System.Collections.Generic; namespace QFSW.QC { /// /// The context to provide suggestions for. /// public struct SuggestionContext { /// /// The depth of the suggestion context. /// public int Depth; /// /// The prompt to generate suggestions for at this depth. /// public string Prompt; /// /// If any, a specific type to target when producing suggestions. /// public Type TargetType; /// /// Any tags added to the suggestion context that may be queried by suggestors. /// public IQcSuggestorTag[] Tags; /// /// Checks if the specified tag exists. /// /// The tag to check. /// If the tag exists. public bool HasTag() where T : IQcSuggestorTag { if (Tags == null) { return false; } // foreach loop has better performance // ReSharper disable once LoopCanBeConvertedToQuery foreach (IQcSuggestorTag tag in Tags) { if (tag is T) { return true; } } return false; } /// /// Gets the specified tag from the context. /// /// The tag to get from the context. /// The tag if it could be found, otherwise a KeyNotFoundException exception is thrown. public T GetTag() where T : IQcSuggestorTag { if (Tags != null) { foreach (IQcSuggestorTag tag in Tags) { if (tag is T foundTag) { return foundTag; } } } throw new KeyNotFoundException($"No tags of type {typeof(T)} could be found."); } /// /// Gets all instances of the specified tag from the context. /// /// The tag to get from the context. /// The tags in the context. public IEnumerable GetTags() where T : IQcSuggestorTag { if (Tags != null) { foreach (IQcSuggestorTag tag in Tags) { if (tag is T foundTag) { yield return foundTag; } } } } } }