| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using Itinero.Instructions.Types; |
| | | 5 | | |
| | | 6 | | [assembly: InternalsVisibleTo("Itinero.Tests")] |
| | | 7 | | [assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")] |
| | | 8 | | [assembly: InternalsVisibleTo("Itinero.Tests.Functional")] |
| | | 9 | | |
| | | 10 | | namespace Itinero.Instructions.ToText; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// The conditional to text simply contains a list of conditions (predicates) and appropriate 'toText' to generate; |
| | | 14 | | /// It basically implements a 'switch case'-instruction |
| | | 15 | | /// </summary> |
| | | 16 | | internal class ConditionalToText : IInstructionToText |
| | | 17 | | { |
| | | 18 | | internal readonly List<(Predicate<BaseInstruction> predicate, IInstructionToText toText)> _options; |
| | | 19 | | private readonly string _context; |
| | | 20 | | |
| | 18 | 21 | | public ConditionalToText( |
| | 18 | 22 | | List<(Predicate<BaseInstruction>, IInstructionToText)> options, |
| | 18 | 23 | | string context = "context not set" |
| | 18 | 24 | | ) |
| | 18 | 25 | | { |
| | 18 | 26 | | _options = options; |
| | 18 | 27 | | _context = context; |
| | 18 | 28 | | } |
| | | 29 | | |
| | | 30 | | public string ToText(BaseInstruction instruction) |
| | 36 | 31 | | { |
| | 214 | 32 | | foreach (var option in _options) |
| | 71 | 33 | | { |
| | 71 | 34 | | if (option.predicate(instruction)) |
| | 36 | 35 | | { |
| | 36 | 36 | | return option.toText.ToText(instruction); |
| | | 37 | | } |
| | 35 | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | throw new ArgumentException("Fallthrough on the predicates for instruction " + instruction + " during " + |
| | 0 | 41 | | _context); |
| | 36 | 42 | | } |
| | | 43 | | } |