//
// ConverterDeserializationGenerator.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 deserialization.
///
public class ConverterDeserializationGenerator
{
private readonly string _stringEnumeratorVariable = "stringEnumerator";
private readonly IndentedTextWriter _textWriter;
///
/// Initializes a new instance of the class.
///
/// The text writer.
public ConverterDeserializationGenerator(IndentedTextWriter textWriter)
{
_textWriter = textWriter;
}
///
/// Push level to the string enumerator.
///
/// The separator.
public void SetAfterSeparatorOnce(char separator)
{
_textWriter.WriteLine(@$"{_stringEnumeratorVariable}.SetAfterSeparatorOnce(""{separator}"");");
}
///
/// Push level to the string enumerator.
///
/// The separator.
/// The maximum number of tokens to read.
public void PushLevel(char separator, uint? maxTokens = default)
{
_textWriter.WriteLine(@$"{_stringEnumeratorVariable}.PushLevel(""{separator}"", {maxTokens?.ToString() ?? "null"});");
}
///
/// Pop level from the string enumerator.
///
public void PopLevel()
{
_textWriter.WriteLine($"{_stringEnumeratorVariable}.PopLevel();");
}
///
/// Prepare the level to the string enumerator.
///
/// The separator.
/// The maximum number of tokens to read.
public void PrepareLevel(char separator, uint? maxTokens = default)
{
_textWriter.WriteLine($@"{_stringEnumeratorVariable}.PrepareLevel(""{separator}"", {maxTokens?.ToString() ?? "null"});");
}
///
/// Prepare the level to the string enumerator.
///
public void RemovePreparedLevel()
{
_textWriter.WriteLine($@"{_stringEnumeratorVariable}.RemovePreparedLevel();");
}
///
/// Try to read to the last token of the level.
///
///
/// If we know that we are not on the last token in the item level, just skip to the end of the item.
/// Note that if this is the case, then that means the converter is either corrupted
/// or the packet has more fields.
///
public void ReadToLastToken()
{
_textWriter.WriteLine($@"while ({_stringEnumeratorVariable}.IsOnLastToken() == false)");
_textWriter.WriteLine("{");
_textWriter.Indent++;
_textWriter.WriteLine($"{_stringEnumeratorVariable}.GetNextToken();");
_textWriter.Indent--;
_textWriter.WriteLine("}");
}
///
/// Deserialize the given parameter and check the result.
///
/// The full name of the packet.
/// The parameter to deserialize.
/// Whether the token is the last one.
public void DeserializeAndCheck(string packetFullName, ParameterInfo parameter, bool isLast)
{
string isLastString = isLast ? "true" : "false";
_textWriter.WriteMultiline($@"
var {parameter.GetResultVariableName()} = _typeConverterRepository.Deserialize<{parameter.GetNullableType()}>({_stringEnumeratorVariable});
var {parameter.GetErrorVariableName()} = CheckDeserializationResult({parameter.GetResultVariableName()}, ""{parameter.Name}"", {_stringEnumeratorVariable}, {isLastString});
if ({parameter.GetErrorVariableName()} is not null)
{{
return Result<{packetFullName}?>.FromError({parameter.GetErrorVariableName()}, {parameter.GetResultVariableName()});
}}
");
}
///
/// Check taht the given variable is not null, if it is, return an error.
///
/// The result variable to check for null.
/// The parameter that is being parsed.
/// The reason for the error.
public void CheckNullError(string resultVariableName, string parameterName, string reason = "The converter has returned null even though it was not expected.")
{
_textWriter.WriteMultiline($@"
if ({resultVariableName}.Entity is null) {{
return new PacketParameterSerializerError(this, ""{parameterName}"", {resultVariableName}, ""{reason}"");
}}
");
}
///
/// Assign local variable with the result of the parameter deserialization.
///
/// The parameter.
public void AssignLocalVariable(ParameterInfo parameter)
{
_textWriter.WriteLine($"var {parameter.Name} = ({parameter.GetActualType()}){parameter.GetResultVariableName()}.Entity;");
}
}