// // ConverterSerializationGenerator.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CodeDom.Compiler; using NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Extensions; namespace NosSmooth.PacketSerializersGenerator; /// /// Various templates for converter serialization. /// public class ConverterSerializationGenerator { private readonly string _builderVariable = "builder"; private readonly IndentedTextWriter _textWriter; /// /// Initializes a new instance of the class. /// /// The text writer. public ConverterSerializationGenerator(IndentedTextWriter textWriter) { _textWriter = textWriter; } /// /// Push level to the string enumerator. /// /// The separator. public void SetAfterSeparatorOnce(char separator) { _textWriter.WriteLine(@$"{_builderVariable}.SetAfterSeparatorOnce('{separator}');"); } /// /// Push level to the string enumerator. /// /// The separator. public void PushLevel(char separator) { _textWriter.WriteLine (@$"{_builderVariable}.PushLevel('{separator}');"); } /// /// Pop level from the string enumerator. /// public void PopLevel() { _textWriter.WriteLine($"{_builderVariable}.PopLevel();"); } /// /// Prepare the level to the string enumerator. /// /// The separator. public void PrepareLevel(char separator) { _textWriter.WriteLine ($@"{_builderVariable}.PrepareLevel('{separator}');"); } /// /// Prepare the level to the string enumerator. /// public void RemovePreparedLevel() { _textWriter.WriteLine($@"{_builderVariable}.RemovePreparedLevel();"); } /// /// Deserialize the given parameter and check the result. /// /// The parameter to deserialize. public void SerializeAndCheck(ParameterInfo parameter) { _textWriter.WriteMultiline ( $@" var {parameter.GetResultVariableName()} = _typeConverterRepository.Serialize<{parameter.GetActualType()}>(obj.{parameter.Name}, {_builderVariable}); if (!{parameter.GetResultVariableName()}.IsSuccess) {{ return Result.FromError(new PacketParameterSerializerError(this, ""{parameter.Name}"", {parameter.GetResultVariableName()}), {parameter.GetResultVariableName()}); }} " ); } }