~ruther/NosSmooth

552c6c8a8a07c4479a7534aaf77395f642f425ac — František Boháček 3 years ago e0d7901
feat: add interfaces for serializers, converters
A Core/NosSmooth.Packets/Converters/ITypeConverterRepository.cs => Core/NosSmooth.Packets/Converters/ITypeConverterRepository.cs +66 -0
@@ 0,0 1,66 @@
//
//  ITypeConverterRepository.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;
using NosSmooth.Packets.Converters.Special;
using NosSmooth.Packets.Errors;
using Remora.Results;

namespace NosSmooth.Packets.Converters;

/// <summary>
/// Repository for <see cref="ITypeConverter"/>.
/// </summary>
public interface ITypeConverterRepository
{
    /// <summary>
    /// Gets the type converter for the given type.
    /// </summary>
    /// <param name="type">The type to find converter for.</param>
    /// <returns>The type converter or an error.</returns>
    public Result<ITypeConverter> GetTypeConverter(Type type);

    /// <summary>
    /// Gets the type converter for the given type.
    /// </summary>
    /// <typeparam name="TParseType">The type to find converter for.</typeparam>
    /// <returns>The type converter or an error.</returns>
    public Result<ITypeConverter<TParseType>> GetTypeConverter<TParseType>();

    /// <summary>
    /// Convert the data from the enumerator to the given type.
    /// </summary>
    /// <param name="parseType">The type of the object to serialize.</param>
    /// <param name="stringEnumerator">The packet string enumerator with the current position.</param>
    /// <returns>The parsed object or an error.</returns>
    public Result<object?> Deserialize(Type parseType, PacketStringEnumerator stringEnumerator);

    /// <summary>
    /// Serializes the given object to string by appending to the packet string builder.
    /// </summary>
    /// <param name="parseType">The type of the object to serialize.</param>
    /// <param name="obj">The object to serialize.</param>
    /// <param name="builder">The string builder to append to.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result Serialize(Type parseType, object obj, PacketStringBuilder builder);

    /// <summary>
    /// Convert the data from the enumerator to the given type.
    /// </summary>
    /// <param name="stringEnumerator">The packet string enumerator with the current position.</param>
    /// <typeparam name="TParseType">The type of the object to serialize.</typeparam>
    /// <returns>The parsed object or an error.</returns>
    public Result<TParseType?> Deserialize<TParseType>(PacketStringEnumerator stringEnumerator);

    /// <summary>
    /// Serializes the given object to string by appending to the packet string builder.
    /// </summary>
    /// <param name="obj">The object to serialize.</param>
    /// <param name="builder">The string builder to append to.</param>
    /// <typeparam name="TParseType">The type of the object to deserialize.</typeparam>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result Serialize<TParseType>(TParseType obj, PacketStringBuilder builder);
}
\ No newline at end of file

M Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs => Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs +1 -1
@@ 15,7 15,7 @@ namespace NosSmooth.Packets.Converters;
/// <summary>
/// Repository for <see cref="ITypeConverter"/>.
/// </summary>
public class TypeConverterRepository
public class TypeConverterRepository : ITypeConverterRepository
{
    private readonly IServiceProvider _serviceProvider;


A Core/NosSmooth.Packets/IPacketSerializer.cs => Core/NosSmooth.Packets/IPacketSerializer.cs +33 -0
@@ 0,0 1,33 @@
//
//  IPacketSerializer.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.Attributes;
using NosSmooth.Packets.Errors;
using NosSmooth.Packets.Packets;
using Remora.Results;

namespace NosSmooth.Packets;

/// <summary>
/// Serializer of packets.
/// </summary>
public interface IPacketSerializer
{
    /// <summary>
    /// Serializes the given object to string by appending to the packet string builder.
    /// </summary>
    /// <param name="obj">The packet to serialize.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result<string> Serialize(IPacket obj);

    /// <summary>
    /// Convert the data from the enumerator to the given type.
    /// </summary>
    /// <param name="packetString">The packet string to deserialize.</param>
    /// <param name="preferredSource">The preferred source to check first. If packet with the given header is not found there, other sources will be checked as well.</param>
    /// <returns>The parsed object or an error.</returns>
    public Result<IPacket> Deserialize(string packetString, PacketSource preferredSource);
}
\ No newline at end of file

M Core/NosSmooth.Packets/PacketSerializer.cs => Core/NosSmooth.Packets/PacketSerializer.cs +1 -1
@@ 15,7 15,7 @@ namespace NosSmooth.Packets;
/// <summary>
/// Serializer of packets.
/// </summary>
public class PacketSerializer
public class PacketSerializer : IPacketSerializer
{
    /// <summary>
    /// Serializes the given object to string by appending to the packet string builder.

A Core/NosSmooth.Packets/Packets/IPacketTypesRepository.cs => Core/NosSmooth.Packets/Packets/IPacketTypesRepository.cs +57 -0
@@ 0,0 1,57 @@
//
//  IPacketTypesRepository.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;
using System.Collections.Generic;
using System.Reflection;
using NosSmooth.Packets.Attributes;
using NosSmooth.Packets.Errors;
using Remora.Results;

namespace NosSmooth.Packets.Packets;

/// <summary>
/// Repository of packet types for finding information about packets.
/// </summary>
public interface IPacketTypesRepository
{
    /// <summary>
    /// Add the given packet type to the repository.
    /// </summary>
    /// <param name="type">The type of the packet.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result AddPacketType(Type type);

    /// <summary>
    /// Gets the type of a packet that corresponds to the given header.
    /// </summary>
    /// <param name="header">The header of the packet.</param>
    /// <param name="preferredSource">The preferred source, first this source will be checked for the header and if packet with that source is not found, other sources will be accpeted as well.</param>
    /// <returns>Info that stores the packet's info. Or an error, if not found.</returns>
    public Result<PacketInfo> FindPacketInfo(string header, PacketSource preferredSource);

    /// <summary>
    /// Gets the packet info from the given packet type.
    /// </summary>
    /// <typeparam name="TPacket">The type of the packet.</typeparam>
    /// <returns>Info that stores the packet's info. Or an error, if not found.</returns>
    public Result<PacketInfo> FindPacketInfo<TPacket>()
        where TPacket : IPacket;

    /// <summary>
    /// Gets the packet info from the given packet type.
    /// </summary>
    /// <param name="packetType">The type of the packet.</param>
    /// <returns>Info that stores the packet's info. Or an error, if not found.</returns>
    public Result<PacketInfo> FindPacketInfo(Type packetType);

    /// <summary>
    /// Gets the packet info from the given packet type name.
    /// </summary>
    /// <param name="packetTypeFullName">The full name of the type of the packet.</param>
    /// <returns>Info that stores the packet's info. Or an error, if not found.</returns>
    public Result<PacketInfo> FindPacketInfoByTypeName(string packetTypeFullName);
}
\ No newline at end of file

M Core/NosSmooth.Packets/Packets/PacketTypesRepository.cs => Core/NosSmooth.Packets/Packets/PacketTypesRepository.cs +1 -1
@@ 19,7 19,7 @@ namespace NosSmooth.Packets.Packets;
/// <summary>
/// Repository of packet types for finding information about packets.
/// </summary>
public class PacketTypesRepository
public class PacketTypesRepository : IPacketTypesRepository
{
    private readonly TypeConverterRepository _typeConverterRepository;
    private readonly Dictionary<PacketSource, Dictionary<string, PacketInfo>> _headerToPacket;

Do not follow this link