using System; using MfGames.Gallium; using MfGames.Nitride.Contents; using YamlDotNet.Serialization; namespace MfGames.Nitride.Yaml; public static class NitrideYamlEntityExtensions { /// /// Parses the entity text as a YAML file and returns the results. This /// uses the default serializer, which may be configured. /// public static TType? GetTextContentYaml( this Entity entity, Action? configure = null) { DeserializerBuilder builder = new(); configure?.Invoke(builder); IDeserializer deserializer = builder.Build(); return entity.GetTextContentYaml(deserializer); } /// /// Parses the entity text as a YAML file using the given serializer and /// returns the results. /// public static TType? GetTextContentYaml( this Entity entity, IDeserializer deserializer) { string? text = entity.GetTextContentString(); return text != null ? deserializer.Deserialize(text) : default; } /// /// Sets the text content to the serialized value. If this is null, then /// the text content is removed. This uses the default serializer which /// may be configured. /// /// The same entity for chaining methods. public static Entity SetTextContentYaml( this Entity entity, TType? value, Action? configure = null) { SerializerBuilder builder = new(); configure?.Invoke(builder); ISerializer serializer = builder.Build(); return SetTextContentYaml(entity, value, serializer); } /// /// Sets the text content to the serialized value using the serializer /// provided. If the value is null, then the text content is removed. /// /// The same entity for chaining methods. public static Entity SetTextContentYaml( this Entity entity, TType? value, ISerializer serializer) { if (value == null) { return entity.Remove(); } string yaml = serializer.Serialize(value); return entity.SetTextContent(yaml); } }