feat: added some YAML parsing methods

This commit is contained in:
Dylan R. E. Moonfire 2022-07-18 17:13:33 -05:00
parent d1c73f2bce
commit 8ce9a12847

View file

@ -0,0 +1,45 @@
using System;
using Gallium;
using Nitride.Contents;
using YamlDotNet.Serialization;
namespace Nitride.Yaml;
public static class NitrideYamlEntityExtensions
{
/// <summary>
/// Parses the entity text as a YAML file and returns the results.
/// </summary>
public static TType? GetYaml<TType>(
this Entity entity,
Action<DeserializerBuilder>? configure = null)
{
DeserializerBuilder builder = new();
configure?.Invoke(builder);
IDeserializer deserializer = builder.Build();
return entity.GetYaml<TType>(deserializer);
}
/// <summary>
/// Parses the entity text as a YAML file and returns the results.
/// </summary>
public static TType? GetYaml<TType>(
this Entity entity,
IDeserializer deserializer)
{
if (!entity.TryGet(out ITextContent content))
{
return default;
}
string text = content.GetText();
return deserializer.Deserialize<TType>(text);
}
}