Skip to content

Commit

Permalink
Add string formatting methods to ParserResult.
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-tsirpanis committed Nov 10, 2023
1 parent 36e6df2 commit 0302ad6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/FarkleNeo/ParserResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ namespace Farkle;
/// </summary>
/// <typeparam name="T">The type of values held by successful parser
/// results.</typeparam>
public readonly struct ParserResult<T>
public readonly struct ParserResult<T> : IFormattable
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
{
private readonly T _value;

Expand All @@ -22,6 +25,43 @@ internal ParserResult(T value, object? error)
Error = error;
}

#if NET6_0_OR_GREATER
/// <summary>
/// Writes the <see cref="ParserResult{T}"/>'s success or error value to a span.
/// </summary>
/// <param name="destination">The span to write to.</param>
/// <param name="charsWritten">The number of characters written to <paramref name="destination"/>.</param>
/// <param name="format">Ignored.</param>
/// <param name="provider">The <see cref="IFormatProvider"/> to use to format the string.</param>
bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
IsSuccess
? destination.TryWrite(provider, $"{Value}", out charsWritten)
: destination.TryWrite(provider, $"{Error}", out charsWritten);
#endif

private string ToString(IFormatProvider? formatProvider) =>
IsSuccess
#if NET6_0_OR_GREATER
? string.Create(formatProvider, $"{Value}")
: string.Create(formatProvider, $"{Error}");
#else
? ((FormattableString)$"{Value}").ToString(formatProvider)
: ((FormattableString)$"{Error}").ToString(formatProvider);
#endif

/// <summary>
/// Converts the <see cref="ParserResult{T}"/>'s success or error value to a string.
/// </summary>
/// <param name="format">Ignored.</param>
/// <param name="formatProvider">The <see cref="IFormatProvider"/> to use to format the string.</param>
public string ToString(string? format, IFormatProvider? formatProvider) =>
ToString(formatProvider);

/// <summary>
/// Converts the <see cref="ParserResult{T}"/>'s success or error value to a string.
/// </summary>
public override string? ToString() => ToString(null, null);

/// <summary>
/// Whether the <see cref="ParserResult{T}"/> represents success.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions tests/Farkle.Tests.CSharp/ParserResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023 Theodore Tsirpanis.
// SPDX-License-Identifier: Apache-2.0

namespace Farkle.Tests.CSharp;

internal class ParserResultTests
{
[Test]
public void TestToString()
{
ParserResult<int> success = ParserResult.CreateSuccess(42);
ParserResult<int> failure = ParserResult.CreateError<int>("error");
Assert.Multiple(() =>
{
Assert.That(success.ToString(), Is.EqualTo("42"));
Assert.That(failure.ToString(), Is.EqualTo("error"));
});
}
}

0 comments on commit 0302ad6

Please sign in to comment.