private static FrozenDictionary GetHistoryMapping()

in src/tooling/docs-assembler/AssembleSources.cs [110:157]


	private static FrozenDictionary<string, string> GetHistoryMapping(AssembleContext context)
	{
		var dictionary = new Dictionary<string, string>();
		var reader = new YamlStreamReader(context.HistoryMappingPath, context.Collector);
		string? stack = null;
		foreach (var entry in reader.Read())
		{
			switch (entry.Key)
			{
				case "stack":
					stack = reader.ReadString(entry.Entry);
					break;

				case "mappings":
					ReadHistoryMappings(dictionary, reader, entry, stack);
					break;
			}
		}

		return dictionary.OrderByDescending(x => x.Key.Length).ToFrozenDictionary();

		static void ReadHistoryMappings(IDictionary<string, string> dictionary, YamlStreamReader reader, YamlToplevelKey entry, string? newStack)
		{
			if (entry.Entry.Value is not YamlMappingNode mappings)
			{
				reader.EmitWarning($"It wasn't possible to read the mappings");
				return;
			}

			foreach (var mapping in mappings)
			{
				var mappingKey = $"{((YamlScalarNode)mapping.Key).Value}/";
				var mappingValue = ((YamlScalarNode)mapping.Value).Value;
				if (mappingKey.Length == 1 || mappingValue is null)
				{
					reader.EmitWarning($"'{mapping.Key}' or '{mapping.Value}' is not a valid mapping");
					continue;
				}

				if (mappingValue.Equals("stack", StringComparison.OrdinalIgnoreCase) && newStack is not null)
					mappingValue = newStack;
				if (dictionary.TryGetValue(mappingKey, out _))
					reader.EmitWarning($"'{mappingKey}' is already mapped to '{mappingValue}'");
				else
					dictionary[mappingKey] = mappingValue;
			}
		}
	}