Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions src/CsToml.Extensions/CsTomlFileSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CsToml.Extensions.Utility;
using Cysharp.Collections;
using System.Buffers;
using System.Runtime.CompilerServices;

namespace CsToml.Extensions;

Expand All @@ -10,9 +11,11 @@ public partial class CsTomlFileSerializer
private static readonly string TomlExtension = ".toml";

public static T Deserialize<T>(string tomlFilePath, CsTomlSerializerOptions? options = null)
=> Deserialize<T>(tomlFilePath, options, TomlFileExtensionPolicy.Strict);

public static T Deserialize<T>(string tomlFilePath, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy)
{
Comment thread
prozolic marked this conversation as resolved.
if (Path.GetExtension(tomlFilePath) != TomlExtension)
throw new FormatException($"TOML files should use the extension .toml");
ValidateExtension(tomlFilePath, extensionPolicy);

using var handle = File.OpenHandle(tomlFilePath!, FileMode.Open, FileAccess.Read, options: FileOptions.SequentialScan);
var length = RandomAccess.GetLength(handle);
Expand Down Expand Up @@ -54,10 +57,15 @@ public static T Deserialize<T>(string tomlFilePath, CsTomlSerializerOptions? opt
}
}

public static async ValueTask<T> DeserializeAsync<T>(string tomlFilePath, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default)
public static ValueTask<T> DeserializeAsync<T>(string tomlFilePath, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default)
=> DeserializeAsyncCore<T>(tomlFilePath, options, TomlFileExtensionPolicy.Strict, configureAwait, cancellationToken);

public static ValueTask<T> DeserializeAsync<T>(string tomlFilePath, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy, bool configureAwait = false, CancellationToken cancellationToken = default)
=> DeserializeAsyncCore<T>(tomlFilePath, options, extensionPolicy, configureAwait, cancellationToken);

private static async ValueTask<T> DeserializeAsyncCore<T>(string tomlFilePath, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy, bool configureAwait, CancellationToken cancellationToken)
{
if (Path.GetExtension(tomlFilePath) != TomlExtension)
throw new FormatException($"TOML files should use the extension .toml");
ValidateExtension(tomlFilePath, extensionPolicy);

cancellationToken.ThrowIfCancellationRequested();
using var handle = File.OpenHandle(tomlFilePath!, FileMode.Open, FileAccess.Read, options: FileOptions.SequentialScan | FileOptions.Asynchronous);
Expand Down Expand Up @@ -99,9 +107,11 @@ public static async ValueTask<T> DeserializeAsync<T>(string tomlFilePath, CsToml
}

public static void Serialize<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options = null)
=> Serialize<T>(tomlFilePath, value, options, TomlFileExtensionPolicy.Strict);

public static void Serialize<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy)
{
if (Path.GetExtension(tomlFilePath) != TomlExtension)
throw new FormatException($"TOML file should use the extension .toml");
ValidateExtension(tomlFilePath, extensionPolicy);
Comment thread
prozolic marked this conversation as resolved.

var directory = new FileInfo(tomlFilePath).Directory;
if (!directory!.Exists)
Expand All @@ -116,10 +126,15 @@ public static void Serialize<T>(string tomlFilePath, T value, CsTomlSerializerOp
bufferWriter.WriteTo(fileWriter.ByteWriter);
}

public static async ValueTask SerializeAsync<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default)
public static ValueTask SerializeAsync<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default)
=> SerializeAsyncCore<T>(tomlFilePath, value, options, TomlFileExtensionPolicy.Strict, configureAwait, cancellationToken);

public static ValueTask SerializeAsync<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy, bool configureAwait = false, CancellationToken cancellationToken = default)
=> SerializeAsyncCore<T>(tomlFilePath, value, options, extensionPolicy, configureAwait, cancellationToken);

private static async ValueTask SerializeAsyncCore<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy, bool configureAwait, CancellationToken cancellationToken)
{
Comment thread
prozolic marked this conversation as resolved.
if (Path.GetExtension(tomlFilePath) != TomlExtension)
throw new FormatException($"TOML file should use the extension .toml");
ValidateExtension(tomlFilePath, extensionPolicy);

var directory = new FileInfo(tomlFilePath).Directory;
if (!directory!.Exists)
Expand All @@ -136,6 +151,34 @@ public static async ValueTask SerializeAsync<T>(string tomlFilePath, T value, Cs
await bufferWriter.WriteToAsync(fileWriter.ByteWriter, configureAwait, cancellationToken);
}

}
private static void ValidateExtension(string tomlFilePath, TomlFileExtensionPolicy extensionPolicy)
{
switch (extensionPolicy)
{
case TomlFileExtensionPolicy.Strict:
if (!string.Equals(Path.GetExtension(tomlFilePath), TomlExtension, StringComparison.Ordinal))
{
ThrowExtensionFormatException();
}
break;
case TomlFileExtensionPolicy.Relaxed:
// No validation needed for relaxed policy
break;
default:
ThrowExtensionOutOfRangeException(extensionPolicy);
break;
}
}
Comment thread
prozolic marked this conversation as resolved.

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowExtensionFormatException()
{
throw new FormatException("TOML files should use the extension .toml");
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowExtensionOutOfRangeException(TomlFileExtensionPolicy extensionPolicy)
{
throw new ArgumentOutOfRangeException(nameof(extensionPolicy));
}
}
8 changes: 8 additions & 0 deletions src/CsToml.Extensions/TomlFileExtensionPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace CsToml.Extensions;

public enum TomlFileExtensionPolicy
{
Strict = 0,
Relaxed = 1
}
Loading