// // ParameterChecker.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 NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Errors; using NosSmooth.PacketSerializersGenerator.Extensions; namespace NosSmooth.PacketSerializersGenerator; /// /// Helpers for checking packet parameters. /// public static class ParameterChecker { /// /// Checks that the given parameter has exactly one packet attribute. /// /// The packet. /// The parameter. /// An error, if any. public static IError? CheckHasOneAttribute(PacketInfo packetInfo, ParameterInfo parameter) { if (parameter.Attributes.Count > 1) { return new DiagnosticError ( "SGAttr", "Packet constructor parameter with multiple packet attributes", "Found multiple packet attributes on parameter {0} in {1}. That is not supported for this type of packet attribute.", parameter.Parameter.SyntaxTree, parameter.Parameter.FullSpan, new List ( new[] { parameter.Name, packetInfo.Name } ) ); } return null; } /// /// Checks that the given parameter is nullable, if it is optional. /// /// The packet. /// The parameter. /// An error, if any. public static IError? CheckOptionalIsNullable(PacketInfo packetInfo, ParameterInfo parameter) { if (parameter.IsOptional() && !parameter.Nullable) { return new DiagnosticError ( "SGNull", "Optional parameters must be nullable", "The parameter {0} in {1} has to be nullable, because it is optional.", parameter.Parameter.SyntaxTree, parameter.Parameter.FullSpan, new List(new[] { parameter.Name, packetInfo.Name }) ); } return null; } }