Skip to content

Commit

Permalink
Add ToString extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
thinker227 committed Dec 31, 2023
1 parent 281c1b5 commit d824af5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
- Add async versions of `Result<T>.Match` and `Result<T>.Switch`.

- Rename `Result<T>.ToType` to `To` and allow attempting to convert to non-derived types.

- Add ToString extensions supporting `IFormattable`.
19 changes: 19 additions & 0 deletions src/Rascal.Tests/ResultExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
using System.Globalization;
using Rascal.Errors;

namespace Rascal.Tests;

public class ResultExtensionsTests
{
[Fact]
public void ToString_UsesFormat_ForOk()
{
var result = Ok(15);
var str = result.ToString("b", CultureInfo.InvariantCulture);

str.ShouldBe("Ok { 1111 }");
}

[Fact]
public void ToString_ReturnsErrorMessage_ForErr()
{
var result = Err<int>("error");
var str = result.ToString("b", CultureInfo.InvariantCulture);

str.ShouldBe("Error { error }");
}

[Fact]
public void Unnest()
{
Expand Down
37 changes: 37 additions & 0 deletions src/Rascal/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ namespace Rascal;
/// </summary>
public static class ResultExtensions
{
/// <summary>
/// Returns a string representation of the result
/// using a specified format and <see cref="IFormatProvider"/> to format an ok value.
/// </summary>
/// <param name="result">The result to return a string representation of.</param>
/// <param name="format">The format to use.</param>
/// <param name="formatProvider">The provider to use to format the ok value.</param>
/// <typeparam name="T">The type of the ok value in the result.</typeparam>
public static string ToString<T>(this Result<T> result, string? format, IFormatProvider? formatProvider)
where T : IFormattable =>
result.Match(
x => $"Ok {{ {x.ToString(format, formatProvider)} }}",
e => $"Error {{ {e} }}"
);

/// <summary>
/// Returns a string representation of the result
/// using a specified format to format an ok value.
/// </summary>
/// <param name="result">The result to return a string representation of.</param>
/// <param name="format">The format to use.</param>
/// <typeparam name="T">The type of the ok value in the result.</typeparam>
public static string ToString<T>(this Result<T> result, string format)
where T : IFormattable =>
result.ToString(format, null);

/// <summary>
/// Returns a string representation of the result
/// using a specified <see cref="IFormatProvider"/> to format an ok value.
/// </summary>
/// <param name="result">The result to return a string representation of.</param>
/// <param name="formatProvider">The provider to use to format the ok value.</param>
/// <typeparam name="T">The type of the ok value in the result.</typeparam>
public static string ToString<T>(this Result<T> result, IFormatProvider formatProvider)
where T : IFormattable =>
result.ToString(null, formatProvider);

/// <summary>
/// Takes a result containing another result and un-nests the inner result.
/// </summary>
Expand Down

0 comments on commit d824af5

Please sign in to comment.