From 552c6c8a8a07c4479a7534aaf77395f642f425ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 29 Dec 2021 15:14:11 +0100 Subject: [PATCH] feat: add interfaces for serializers, converters --- .../Converters/ITypeConverterRepository.cs | 66 +++++++++++++++++++ .../Converters/TypeConverterRepository.cs | 2 +- Core/NosSmooth.Packets/IPacketSerializer.cs | 33 ++++++++++ Core/NosSmooth.Packets/PacketSerializer.cs | 2 +- .../Packets/IPacketTypesRepository.cs | 57 ++++++++++++++++ .../Packets/PacketTypesRepository.cs | 2 +- 6 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 Core/NosSmooth.Packets/Converters/ITypeConverterRepository.cs create mode 100644 Core/NosSmooth.Packets/IPacketSerializer.cs create mode 100644 Core/NosSmooth.Packets/Packets/IPacketTypesRepository.cs diff --git a/Core/NosSmooth.Packets/Converters/ITypeConverterRepository.cs b/Core/NosSmooth.Packets/Converters/ITypeConverterRepository.cs new file mode 100644 index 0000000..e5e1d48 --- /dev/null +++ b/Core/NosSmooth.Packets/Converters/ITypeConverterRepository.cs @@ -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; + +/// +/// Repository for . +/// +public interface ITypeConverterRepository +{ + /// + /// Gets the type converter for the given type. + /// + /// The type to find converter for. + /// The type converter or an error. + public Result GetTypeConverter(Type type); + + /// + /// Gets the type converter for the given type. + /// + /// The type to find converter for. + /// The type converter or an error. + public Result> GetTypeConverter(); + + /// + /// Convert the data from the enumerator to the given type. + /// + /// The type of the object to serialize. + /// The packet string enumerator with the current position. + /// The parsed object or an error. + public Result Deserialize(Type parseType, PacketStringEnumerator stringEnumerator); + + /// + /// Serializes the given object to string by appending to the packet string builder. + /// + /// The type of the object to serialize. + /// The object to serialize. + /// The string builder to append to. + /// A result that may or may not have succeeded. + public Result Serialize(Type parseType, object obj, PacketStringBuilder builder); + + /// + /// Convert the data from the enumerator to the given type. + /// + /// The packet string enumerator with the current position. + /// The type of the object to serialize. + /// The parsed object or an error. + public Result Deserialize(PacketStringEnumerator stringEnumerator); + + /// + /// Serializes the given object to string by appending to the packet string builder. + /// + /// The object to serialize. + /// The string builder to append to. + /// The type of the object to deserialize. + /// A result that may or may not have succeeded. + public Result Serialize(TParseType obj, PacketStringBuilder builder); +} \ No newline at end of file diff --git a/Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs b/Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs index 3357f76..42a57f8 100644 --- a/Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs +++ b/Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs @@ -15,7 +15,7 @@ namespace NosSmooth.Packets.Converters; /// /// Repository for . /// -public class TypeConverterRepository +public class TypeConverterRepository : ITypeConverterRepository { private readonly IServiceProvider _serviceProvider; diff --git a/Core/NosSmooth.Packets/IPacketSerializer.cs b/Core/NosSmooth.Packets/IPacketSerializer.cs new file mode 100644 index 0000000..461e5c7 --- /dev/null +++ b/Core/NosSmooth.Packets/IPacketSerializer.cs @@ -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; + +/// +/// Serializer of packets. +/// +public interface IPacketSerializer +{ + /// + /// Serializes the given object to string by appending to the packet string builder. + /// + /// The packet to serialize. + /// A result that may or may not have succeeded. + public Result Serialize(IPacket obj); + + /// + /// Convert the data from the enumerator to the given type. + /// + /// The packet string to deserialize. + /// The preferred source to check first. If packet with the given header is not found there, other sources will be checked as well. + /// The parsed object or an error. + public Result Deserialize(string packetString, PacketSource preferredSource); +} \ No newline at end of file diff --git a/Core/NosSmooth.Packets/PacketSerializer.cs b/Core/NosSmooth.Packets/PacketSerializer.cs index b87913d..3c9dc93 100644 --- a/Core/NosSmooth.Packets/PacketSerializer.cs +++ b/Core/NosSmooth.Packets/PacketSerializer.cs @@ -15,7 +15,7 @@ namespace NosSmooth.Packets; /// /// Serializer of packets. /// -public class PacketSerializer +public class PacketSerializer : IPacketSerializer { /// /// Serializes the given object to string by appending to the packet string builder. diff --git a/Core/NosSmooth.Packets/Packets/IPacketTypesRepository.cs b/Core/NosSmooth.Packets/Packets/IPacketTypesRepository.cs new file mode 100644 index 0000000..6ee9243 --- /dev/null +++ b/Core/NosSmooth.Packets/Packets/IPacketTypesRepository.cs @@ -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; + +/// +/// Repository of packet types for finding information about packets. +/// +public interface IPacketTypesRepository +{ + /// + /// Add the given packet type to the repository. + /// + /// The type of the packet. + /// A result that may or may not have succeeded. + public Result AddPacketType(Type type); + + /// + /// Gets the type of a packet that corresponds to the given header. + /// + /// The header of the packet. + /// 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. + /// Info that stores the packet's info. Or an error, if not found. + public Result FindPacketInfo(string header, PacketSource preferredSource); + + /// + /// Gets the packet info from the given packet type. + /// + /// The type of the packet. + /// Info that stores the packet's info. Or an error, if not found. + public Result FindPacketInfo() + where TPacket : IPacket; + + /// + /// Gets the packet info from the given packet type. + /// + /// The type of the packet. + /// Info that stores the packet's info. Or an error, if not found. + public Result FindPacketInfo(Type packetType); + + /// + /// Gets the packet info from the given packet type name. + /// + /// The full name of the type of the packet. + /// Info that stores the packet's info. Or an error, if not found. + public Result FindPacketInfoByTypeName(string packetTypeFullName); +} \ No newline at end of file diff --git a/Core/NosSmooth.Packets/Packets/PacketTypesRepository.cs b/Core/NosSmooth.Packets/Packets/PacketTypesRepository.cs index 0a42f0d..569a8ee 100644 --- a/Core/NosSmooth.Packets/Packets/PacketTypesRepository.cs +++ b/Core/NosSmooth.Packets/Packets/PacketTypesRepository.cs @@ -19,7 +19,7 @@ namespace NosSmooth.Packets.Packets; /// /// Repository of packet types for finding information about packets. /// -public class PacketTypesRepository +public class PacketTypesRepository : IPacketTypesRepository { private readonly TypeConverterRepository _typeConverterRepository; private readonly Dictionary> _headerToPacket; -- 2.48.1