~ruther/NosSmooth

ref: 5b49b8a66e8e72e0460177bf484cb5cef73081b3 NosSmooth/Packets/NosSmooth.PacketSerializersGenerator/ParameterChecker.cs -rw-r--r-- 2.4 KiB
5b49b8a6 — Rutherther fix(packets): make correct inner separator in eq sub packet 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
//  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;

/// <summary>
/// Helpers for checking packet parameters.
/// </summary>
public static class ParameterChecker
{
    /// <summary>
    /// Checks that the given parameter has exactly one packet attribute.
    /// </summary>
    /// <param name="packetInfo">The packet.</param>
    /// <param name="parameter">The parameter.</param>
    /// <returns>An error, if any.</returns>
    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<object?>
                (
                    new[]
                    {
                        parameter.Name,
                        packetInfo.Name
                    }
                )
            );
        }

        return null;
    }

    /// <summary>
    /// Checks that the given parameter is nullable, if it is optional.
    /// </summary>
    /// <param name="packetInfo">The packet.</param>
    /// <param name="parameter">The parameter.</param>
    /// <returns>An error, if any.</returns>
    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<object?>(new[] { parameter.Name, packetInfo.Name })
            );
        }

        return null;
    }
}
Do not follow this link