~ruther/NosSmooth

ref: 58d8743477820408507f19d1c1ce75b73e23615f NosSmooth/Packets/NosSmooth.PacketSerializer/Converters/Common/NameStringConverter.cs -rw-r--r-- 1.4 KiB
58d87434 — Rutherther chore: bump versions 2 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
//
//  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.PacketSerializer.Abstractions;
using NosSmooth.PacketSerializer.Abstractions.Common;
using Remora.Results;

namespace NosSmooth.PacketSerializer.Converters.Common;

/// <summary>
/// Converter of <see cref="NameString"/>.
/// </summary>
public class NameStringConverter : BaseStringConverter<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(ref PacketStringEnumerator stringEnumerator, DeserializeOptions options)
    {
        var tokenResult = stringEnumerator.GetNextToken(out var packetToken);
        if (!tokenResult.IsSuccess)
        {
            return Result<NameString?>.FromError(tokenResult);
        }

        if (options.CanBeNull)
        {
            if (packetToken.Token[0] == '-' && packetToken.Token.Length == 1)
            {
                return Result<NameString?>.FromSuccess(null);
            }
        }

        return NameString.FromPacket(packetToken.Token.ToString());
    }
}
Do not follow this link