<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;


namespace FreecraftCore.Serializer.KnownTypes
{
	/// <summary>
	/// Decorator for Enum serialization.
	/// </summary>
	/// <typeparam name="TEnumType">The type of the Enum.</typeparam>
	/// <typeparam name="TBaseType">The basetype of the Enum.</typeparam>
	public class EnumSerializerDecorator<TEnumType, TBaseType> : SimpleTypeSerializerStrategy<TEnumType>
		where TEnumType : struct
	{
		/// <summary>
		/// Decorated serializer. For example. An enum Type : byte would have a ITypeSerializerStrategy{byte}.
		/// </summary>
		[NotNull]
		private ITypeSerializerStrategy<TBaseType> serializerStrategy { get; }

		//Enums don't require context (Though closer look at TC/WoW payloads may indicate packing in the future)
		public override SerializationContextRequirement ContextRequirement => SerializationContextRequirement.Contextless;

		public EnumSerializerDecorator([NotNull] IGeneralSerializerProvider serializerProvider)
		{
			if (!typeof(TEnumType).GetTypeInfo().IsEnum)
				throw new InvalidOperationException($"Cannot create an enum decorator for type {typeof(TEnumType).FullName} because it is not an enum.");
#if !NET35
			if (typeof(TEnumType).GetTypeInfo().GetEnumUnderlyingType() != typeof(TBaseType))
				throw new InvalidOperationException($"Defining an Enum decorator requires {nameof(TEnumType)}'s base enum type to match {nameof(TBaseType)}.");
#else
			if (Enum.GetUnderlyingType(typeof(TEnumType)) != typeof(TBaseType))
				throw new InvalidOperationException($"Defining an Enum decorator requires {nameof(TEnumType)}'s base enum type to match {nameof(TBaseType)}.");
#endif

			if (serializerProvider == null)
				throw new ArgumentNullException(nameof(serializerProvider), $"Provided service {nameof(IGeneralSerializerProvider)} was null.");

			try
			{
				serializerStrategy = serializerProvider.Get<TBaseType>();
			}
			catch (InvalidOperationException e)
			{
				throw new InvalidOperationException($"Failed to create strategy for Type: {typeof(TBaseType).FullName} for enum decorator for enum Type: {typeof(TEnumType).FullName}", e);
			}
		}

		/// <inheritdoc />
		public override TEnumType Read(IWireStreamReaderStrategy source)
		{
			if (source == null) throw new ArgumentNullException(nameof(source));

			//TODO: Should be handle exceptions?
			return (TEnumType)Enum.ToObject(typeof(TEnumType), (TBaseType)serializerStrategy.Read(source));
		}

		/// <inheritdoc />
		public override void Write(TEnumType value, IWireStreamWriterStrategy dest)
		{
			if (dest == null) throw new ArgumentNullException(nameof(dest));

			//TODO: Increase perf
			//Not great. It's slow conversion and box. Then casting the box to call.
			object boxedBaseEnumValue = Convert.ChangeType(value, typeof(TBaseType));

			if (boxedBaseEnumValue == null)
				throw new Exception();

			serializerStrategy.Write((TBaseType)boxedBaseEnumValue, dest);
		}

		/// <inheritdoc />
		public override Task WriteAsync(TEnumType value, IWireStreamWriterStrategyAsync dest)
		{
			throw new NotImplementedException();
		}

		/// <inheritdoc />
		public override Task<TEnumType> ReadAsync(IWireStreamReaderStrategyAsync source)
		{
			throw new NotImplementedException();
		}
	}
}<commit_msg>Added Async to EnumSerializer; Cleaned it up too<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using MiscUtil;


namespace FreecraftCore.Serializer.KnownTypes
{
	/// <summary>
	/// Decorator for Enum serialization.
	/// </summary>
	/// <typeparam name="TEnumType">The type of the Enum.</typeparam>
	/// <typeparam name="TBaseType">The basetype of the Enum.</typeparam>
	public class EnumSerializerDecorator<TEnumType, TBaseType> : SimpleTypeSerializerStrategy<TEnumType>
		where TEnumType : struct
	{
		/// <summary>
		/// Decorated serializer. For example. An enum Type : byte would have a ITypeSerializerStrategy{byte}.
		/// </summary>
		[NotNull]
		private ITypeSerializerStrategy<TBaseType> serializerStrategy { get; }

		//Enums don't require context (Though closer look at TC/WoW payloads may indicate packing in the future)
		public override SerializationContextRequirement ContextRequirement => SerializationContextRequirement.Contextless;

		public EnumSerializerDecorator([NotNull] IGeneralSerializerProvider serializerProvider)
		{
			if (!typeof(TEnumType).GetTypeInfo().IsEnum)
				throw new InvalidOperationException($"Cannot create an enum decorator for type {typeof(TEnumType).FullName} because it is not an enum.");
#if !NET35
			if (typeof(TEnumType).GetTypeInfo().GetEnumUnderlyingType() != typeof(TBaseType))
				throw new InvalidOperationException($"Defining an Enum decorator requires {nameof(TEnumType)}'s base enum type to match {nameof(TBaseType)}.");
#else
			if (Enum.GetUnderlyingType(typeof(TEnumType)) != typeof(TBaseType))
				throw new InvalidOperationException($"Defining an Enum decorator requires {nameof(TEnumType)}'s base enum type to match {nameof(TBaseType)}.");
#endif

			if (serializerProvider == null)
				throw new ArgumentNullException(nameof(serializerProvider), $"Provided service {nameof(IGeneralSerializerProvider)} was null.");

			try
			{
				serializerStrategy = serializerProvider.Get<TBaseType>();
			}
			catch (InvalidOperationException e)
			{
				throw new InvalidOperationException($"Failed to create strategy for Type: {typeof(TBaseType).FullName} for enum decorator for enum Type: {typeof(TEnumType).FullName}", e);
			}
		}

		/// <inheritdoc />
		public override TEnumType Read(IWireStreamReaderStrategy source)
		{
			if (source == null) throw new ArgumentNullException(nameof(source));

			//TODO: Should be handle exceptions?
			return Operator.Convert<TBaseType, TEnumType>(serializerStrategy.Read(source));
		}

		/// <inheritdoc />
		public override void Write(TEnumType value, IWireStreamWriterStrategy dest)
		{
			if (dest == null) throw new ArgumentNullException(nameof(dest));

			serializerStrategy.Write(Operator.Convert<TEnumType, TBaseType>(value), dest);
		}

		/// <inheritdoc />
		public override async Task WriteAsync(TEnumType value, IWireStreamWriterStrategyAsync dest)
		{
			if (dest == null) throw new ArgumentNullException(nameof(dest));

			await serializerStrategy.WriteAsync(Operator.Convert<TEnumType, TBaseType>(value), dest);
		}

		/// <inheritdoc />
		public override async Task<TEnumType> ReadAsync(IWireStreamReaderStrategyAsync source)
		{
			if (source == null) throw new ArgumentNullException(nameof(source));

			//TODO: Should be handle exceptions?
			return Operator.Convert<TBaseType, TEnumType>(await serializerStrategy.ReadAsync(source));
		}
	}
}
{'fraction_non_alphanumeric': 0.10773480662983426, 'fraction_numerical': 0.0005815644082582146, 'mean_word_length': 5.1310160427807485, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5330346', 'n_tokens_mistral': 2159, 'n_tokens_neox': 1942, 'n_words': 521}