feat: added some YAML parsing methods
This commit is contained in:
parent
d1c73f2bce
commit
8ce9a12847
1 changed files with 45 additions and 0 deletions
45
src/Nitride.Yaml/NitrideYamlEntityExtensions.cs
Normal file
45
src/Nitride.Yaml/NitrideYamlEntityExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
Reference in a new issue