< Summary

Class:Itinero.Instructions.RouteInstructionGeneratorSettings
Assembly:Itinero.Instructions
File(s):/home/runner/work/routing2/routing2/src/Itinero.Instructions/RouteInstructionGeneratorSettings.cs
Covered lines:19
Uncovered lines:17
Coverable lines:36
Total lines:99
Line coverage:52.7% (19 of 36)
Covered branches:1
Total branches:6
Branch coverage:16.6% (1 of 6)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Generators()100%1100%
get_Languages()100%1100%
.cctor()0%20%
get_Default()100%10%
FromConfigFile(...)100%10%
FromStream(...)100%1100%
FromJson(...)25%468.42%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.Instructions/RouteInstructionGeneratorSettings.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Text.Json;
 6using Itinero.Instructions.Configuration;
 7using Itinero.Instructions.ToText;
 8using Itinero.Instructions.Types.Generators;
 9// ReSharper disable MemberCanBePrivate.Global
 10
 11namespace Itinero.Instructions;
 12
 13/// <summary>
 14/// Settings for the route instruction generator.
 15/// </summary>
 16public class RouteInstructionGeneratorSettings
 17{
 18    /// <summary>
 19    /// Individual instruction generators.
 20    /// </summary>
 421    public List<IInstructionGenerator> Generators { get; private set; } = new();
 22
 23    /// <summary>
 24    /// Text generators per language code.
 25    /// </summary>
 626    public Dictionary<string, IInstructionToText> Languages { get; private set; } = new();
 27
 028    private static readonly Lazy<RouteInstructionGeneratorSettings> DefaultLazy = new(() =>
 029    {
 030        using var stream =
 031            typeof(IndexedRoute).Assembly.GetManifestResourceStream(
 032                "Itinero.Instructions.Configuration.default.json");
 033        return FromStream(stream ?? throw new InvalidOperationException("Default not found"));
 034    });
 35
 36    /// <summary>
 37    /// Gets the default settings.
 38    /// </summary>
 039    public static RouteInstructionGeneratorSettings Default => DefaultLazy.Value;
 40
 41    /// <summary>
 42    /// Loads settings from a file.
 43    /// </summary>
 44    /// <param name="path">The path to the settings file.</param>
 45    /// <param name="customGenerators">The custom generators.</param>
 46    /// <returns>The settings.</returns>
 47    public static RouteInstructionGeneratorSettings FromConfigFile(string path,
 48        IEnumerable<IInstructionGenerator>? customGenerators = null)
 049    {
 050        return FromStream(File.OpenRead(path), customGenerators);
 051    }
 52
 53    /// <summary>
 54    /// Parses settings from the given stream.
 55    /// </summary>
 56    /// <param name="stream">The stream.</param>
 57    /// <param name="customGenerators">Any custom generators.</param>
 58    /// <returns>The settings.</returns>
 59    public static RouteInstructionGeneratorSettings FromStream(Stream stream,
 60        IEnumerable<IInstructionGenerator>? customGenerators = null)
 261    {
 262        using var streamReader = new StreamReader(stream);
 263        return FromJson(streamReader.ReadToEnd(), customGenerators);
 264    }
 65
 66    /// <summary>
 67    /// Parses settings from the given json string.
 68    /// </summary>
 69    /// <param name="json">A string with the json.</param>
 70    /// <param name="customGenerators">Any custom generators.</param>
 71    /// <returns>The settings.</returns>
 72    public static RouteInstructionGeneratorSettings FromJson(string json,
 73        IEnumerable<IInstructionGenerator>? customGenerators = null)
 274    {
 75        // collect all the hardcoded generators.
 276        var allGenerators = new Dictionary<string, IInstructionGenerator>(AllGenerators.Generators
 3477            .ToDictionary(x => x.Name, x => x));
 78
 79        // add custom generators.
 280        if (customGenerators != null)
 081        {
 082            foreach (var customGenerator in customGenerators)
 083            {
 084                allGenerators[customGenerator.Name] = customGenerator;
 085            }
 086        }
 87
 88        // read settings.
 289        var (generators, translations) =
 290            ConfigurationParser.ParseRouteToInstructions(
 291                JsonDocument.Parse(json).RootElement, allGenerators);
 92
 293        return new RouteInstructionGeneratorSettings()
 294        {
 295            Generators = new List<IInstructionGenerator>(generators),
 296            Languages = translations
 297        };
 298    }
 99}