~ruther/NosSmooth

ref: e67bb2bac9b1f0338596d5f2dfdf2508e7dee36b NosSmooth/Packets/NosSmooth.PacketSerializer/Converters/Special/Converters/ListStringConverter.cs -rw-r--r-- 3.2 KiB
e67bb2ba — Rutherther Merge pull request #47 from Rutherther/feat/pass-nullable 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
//  ListStringConverter.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.Collections.Generic;
using NosSmooth.PacketSerializer.Abstractions;
using NosSmooth.PacketSerializer.Abstractions.Errors;
using Remora.Results;

namespace NosSmooth.PacketSerializer.Converters.Special.Converters;

/// <summary>
/// Converter for list types.
/// </summary>
/// <typeparam name="TGeneric">The generic type argument of the list.</typeparam>
public class ListStringConverter<TGeneric> : BaseStringConverter<IReadOnlyList<TGeneric>>
{
    private readonly IStringSerializer _serializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="ListStringConverter{TGeneric}"/> class.
    /// </summary>
    /// <param name="serializer">The string serializer.</param>
    public ListStringConverter(IStringSerializer serializer)
    {
        _serializer = serializer;

    }

    /// <inheritdoc />
    public override Result Serialize(IReadOnlyList<TGeneric>? obj, PacketStringBuilder builder)
    {
        if (obj is null)
        {
            builder.Append('-');
            return Result.FromSuccess();
        }

        foreach (var item in obj)
        {
            if (!builder.PushPreparedLevel())
            {
                return new ArgumentInvalidError(nameof(builder), "The string builder has to have a prepared level for all lists.");
            }

            var serializeResult = _serializer.Serialize(item, builder);
            builder.PopLevel();
            if (!serializeResult.IsSuccess)
            {
                return serializeResult;
            }
        }

        return Result.FromSuccess();
    }

    /// <inheritdoc />
    public override Result<IReadOnlyList<TGeneric>?> Deserialize(ref PacketStringEnumerator stringEnumerator, DeserializeOptions options)
    {
        var list = new List<TGeneric>();

        while (!(stringEnumerator.IsOnLastToken() ?? false))
        {
            if (!stringEnumerator.PushPreparedLevel())
            {
                return new ArgumentInvalidError(nameof(stringEnumerator), "The string enumerator has to have a prepared level for all lists.");
            }

            var result = _serializer.Deserialize<TGeneric>(ref stringEnumerator, default);

            // 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.
            while (stringEnumerator.IsOnLastToken() == false)
            {
                stringEnumerator.GetNextToken(out _);
            }

            stringEnumerator.PopLevel();
            if (!result.IsSuccess)
            {
                return Result<IReadOnlyList<TGeneric>?>.FromError(new ListSerializerError(result, list.Count), result);
            }

            if (result.Entity is null)
            {
                return new DeserializedValueNullError(typeof(IReadOnlyList<TGeneric>));
            }

            list.Add(result.Entity);
        }

        return list;
    }
}
Do not follow this link