~ruther/NosSmooth

ref: e844e11e5d4b0b9989b75b408b541f74ae102e6e NosSmooth/Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs -rw-r--r-- 1.3 KiB
e844e11e — František Boháček chore: add pinit sub packet unknown properties description 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
//
//  NameStringConverter.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.Packets.Common;
using NosSmooth.Packets.Converters.Basic;
using Remora.Results;

namespace NosSmooth.Packets.Converters.Common;

/// <summary>
/// Converter of <see cref="NameString"/>.
/// </summary>
public class NameStringConverter : BaseTypeConverter<NameString>
{
    /// <inheritdoc />
    public override Result Serialize(NameString? obj, PacketStringBuilder builder)
    {
        if (obj is null)
        {
            builder.Append("-");
            return Result.FromSuccess();
        }

        builder.Append(obj.PacketName);
        return Result.FromSuccess();
    }

    /// <inheritdoc />
    public override Result<NameString?> Deserialize(PacketStringEnumerator stringEnumerator)
    {
        var tokenResult = stringEnumerator.GetNextToken();
        if (!tokenResult.IsSuccess)
        {
            return Result<NameString?>.FromError(tokenResult);
        }

        if (tokenResult.Entity.Token == "-")
        {
            return Result<NameString?>.FromSuccess(null);
        }

        return NameString.FromPacket(tokenResult.Entity.Token);
    }
}
Do not follow this link