Skip to content

Commit

Permalink
Add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thinker227 committed Dec 27, 2023
1 parent 8e56fc9 commit 273c0c7
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/Rascal/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static implicit operator Error(string message) =>
/// <param name="message">The message to display.</param>
public sealed class StringError(string message) : Error
{
/// <inheritdoc/>
public override string Message => message;

/// <summary>
Expand All @@ -38,6 +39,9 @@ public sealed class StringError(string message) : Error
/// <param name="exception">The exception in the error.</param>
public sealed class ExceptionError(Exception exception) : Error
{
/// <summary>
/// The exception in the error.
/// </summary>
public Exception Exception { get; } = exception;

/// <summary>
Expand All @@ -57,6 +61,7 @@ public sealed class AggregateError(IEnumerable<Error> errors) : Error
/// </summary>
public IReadOnlyCollection<Error> Errors { get; } = errors.ToList();

/// <inheritdoc/>
public override string Message => string.Join("\n", Errors.Select(x => x.Message));
}

Expand All @@ -66,6 +71,7 @@ public sealed class AggregateError(IEnumerable<Error> errors) : Error
/// <param name="message">A message which describes the thing which wasn't found.</param>
public sealed class NotFoundError(string message) : Error
{
/// <inheritdoc/>
public override string Message => message;
}

Expand All @@ -75,6 +81,7 @@ public sealed class NotFoundError(string message) : Error
/// <param name="message">An optional message describing the operation and why it is invalid.</param>
public sealed class InvalidOperationError(string? message = null) : Error
{
/// <inheritdoc/>
public override string Message => message ?? "Invalid operation.";
}

Expand All @@ -84,6 +91,7 @@ public sealed class InvalidOperationError(string? message = null) : Error
/// <param name="message">An optional message describing the thing which is <see langword="null"/>.</param>
public sealed class NullError(string? message = null) : Error
{
/// <inheritdoc/>
public override string Message => message ?? "Value is null.";
}

Expand All @@ -99,6 +107,7 @@ public sealed class ValidationError(string message, object? source) : Error
/// </summary>
public object? Source => source;

/// <inheritdoc/>
public override string Message => message;
}

Expand All @@ -114,6 +123,7 @@ public sealed class ParseError(string message, object? source) : Error
/// </summary>
public object? Source => source;

/// <inheritdoc/>
public override string Message => message;
}

Expand All @@ -123,5 +133,6 @@ public sealed class ParseError(string message, object? source) : Error
/// <param name="message">The optional message describing the task cancellation.</param>
public sealed class CancellationError(string? message = null) : Error
{
/// <inheritdoc/>
public override string Message => message ?? "Task was cancelled.";
}
3 changes: 3 additions & 0 deletions src/Rascal/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public Result(Error error)
this.error = error;
}

/// <summary>
/// Gets a string representation of the result.
/// </summary>
[Pure]
public override string ToString() =>
HasValue
Expand Down
2 changes: 2 additions & 0 deletions src/Rascal/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static Result<IReadOnlyList<T>> Sequence<T>(this IEnumerable<Result<T>> r
/// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
/// <param name="dict">The dictionary to try locate the key in.</param>
/// <param name="key">The key to locate.</param>
/// <param name="error">The error to return if the key is not present.</param>
/// <returns>A result containing the value associated with <paramref name="key"/> in the dictionary,
/// or an error if the key is not present.</returns>
public static Result<TValue> TryGetValueR<TKey, TValue>(
Expand All @@ -118,6 +119,7 @@ public static Result<TValue> TryGetValueR<TKey, TValue>(
/// <typeparam name="T">The type of the elements in the list.</typeparam>
/// <param name="list">The list to index.</param>
/// <param name="index">The zero-based index of the element to get.</param>
/// <param name="error">The error to return if the index is out of range.</param>
/// <returns>A result containing the value at <paramref name="index"/> in the list,
/// or an error if the index is out of range of the list.</returns>
public static Result<T> Index<T>(this IReadOnlyList<T> list, int index, Error? error = null) =>
Expand Down
38 changes: 37 additions & 1 deletion src/Rascal/Result_Equality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,74 @@ namespace Rascal;
System.Numerics.IEqualityOperators<Result<T>, T, bool>
#endif
{
/// <summary>
/// Checks whether the result has a value and the value is equal to another value.
/// </summary>
/// <param name="other">The value to check for equality with the value in the result.</param>
[Pure]
public bool Equals(T? other) =>
HasValue && (other?.Equals(value) ?? value is null);

/// <summary>
/// Checks whether the result is equal to another result.
/// Results are equal if both results have values which are equal,
/// or if both results are errors.
/// </summary>
/// <param name="other">The result to check for equality with the current result.</param>
[Pure]
public bool Equals(Result<T> other) =>
HasValue && other.HasValue && (other.value?.Equals(value) ?? value is null) ||
!HasValue && !other.HasValue;

/// <inheritdoc/>
[Pure]
public override bool Equals(object? other) =>
other is T x && Equals(x) ||
other is Result<T> r && Equals(r);

/// <inheritdoc/>
[Pure]
public override int GetHashCode() =>
HasValue
? value?.GetHashCode() ?? 0
: 0;

/// <summary>
/// Checks whether two results are equal.
/// Results are equal if both results have values which are equal,
/// or if both results are errors.
/// </summary>
/// <param name="a">The first result to compare.</param>
/// <param name="b">The second result to compare.</param>
[Pure]
public static bool operator ==(Result<T> a, Result<T> b) =>
a.Equals(b);

/// <summary>
/// Checks whether two results are not equal.
/// Results are equal if both results have values which are equal,
/// or if both results are errors.
/// </summary>
/// <param name="a">The first result to compare.</param>
/// <param name="b">The second result to compare.</param>
[Pure]
public static bool operator !=(Result<T> a, Result<T> b) =>
!a.Equals(b);

/// <summary>
/// Checks whether a result has a value and the value is equal to another value.
/// </summary>
/// <param name="a">The result to compare.</param>
/// <param name="b">The value to check for equality with the value in the result.</param>
[Pure]
public static bool operator ==(Result<T> a, T? b) =>
a.Equals(b);


/// <summary>
/// Checks whether a result either does not have a value, or the value is not equal to another value.
/// </summary>
/// <param name="a">The result to compare.</param>
/// <param name="b">The value to check for inequality with the value in the result.</param>
[Pure]
public static bool operator !=(Result<T> a, T? b) =>
!a.Equals(b);
Expand Down
1 change: 1 addition & 0 deletions src/Rascal/Result_Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Result<TNew> SelectMany<TNew>(Func<T, Result<TNew>> mapping) =>
/// The expression <c>r.SelectMany(x => f(x), (x, y) => g(x, y))</c> can be written as
/// <c>r.Bind(x => f(x).Map(y => g(x, y)))</c>.
/// </remarks>
/// <param name="other">The function used to map the value to an intermediate result.</param>
/// <param name="mapping">The function used to map the value to a new result.</param>
/// <typeparam name="TOther">The type of the intermediate value.</typeparam>
/// <typeparam name="TNew">The type of the new value.</typeparam>
Expand Down
2 changes: 1 addition & 1 deletion src/Rascal/Result_Matching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public T GetValueOrDefault(T @default) =>
/// Gets the value within the result,
/// or a default value if the result does not contain a value.
/// </summary>
/// <param name="default">A function to get a default value to return
/// <param name="getDefault">A function to get a default value to return
/// if the result does not contain a value.</param>
[Pure]
public T GetValueOrDefault(Func<T> getDefault) =>
Expand Down
28 changes: 25 additions & 3 deletions src/Rascal/Result_Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Result<TDerived> ToType<TDerived>(string? error = null)
value))
: new(Error);

#if NETCOREAPP
/// <summary>
/// Checks whether the value in the result matches a predicate,
/// and if not replaces the value with an error.
Expand All @@ -65,12 +66,23 @@ public Result<TDerived> ToType<TDerived>(string? error = null)
/// error produced by <paramref name="getError"/>.
/// If the original result does not contain a value, that result will be returned.</returns>
[Pure]
#if NETCOREAPP
public Result<T> Validate(
Func<T, bool> predicate,
Func<T, Error>? getError = null,
[CallerArgumentExpression(nameof(predicate))] string expr = "") =>
#else
/// <summary>
/// Checks whether the value in the result matches a predicate,
/// and if not replaces the value with an error.
/// </summary>
/// <param name="predicate">The predicate to match the value against.</param>
/// <param name="getError">A function to produce an error
/// if the value doesn't match the predicate.</param>
/// <returns>A result which contains the value of the original result
/// if the value matches <paramref name="predicate"/>, otherwise an
/// error produced by <paramref name="getError"/>.
/// If the original result does not contain a value, that result will be returned.</returns>
[Pure]
public Result<T> Validate(
Func<T, bool> predicate,
Func<T, Error>? getError = null) =>
Expand All @@ -86,6 +98,7 @@ public Result<T> Validate(
#endif
: new(Error);

#if NETCOREAPP
/// <summary>
/// Checks whether the value in the result matches a predicate,
/// and if not replaces the value with an error.
Expand All @@ -98,13 +111,22 @@ public Result<T> Validate(
/// If the original result does not contain a value, that result will be returned.</returns>
[Pure]
public Result<T> Where(
#if NETCOREAPP
Func<T, bool> predicate,
[CallerArgumentExpression(nameof(predicate))] string expr = "") =>
Validate(predicate, null, expr);
#else
/// <summary>
/// Checks whether the value in the result matches a predicate,
/// and if not replaces the value with an error.
/// </summary>
/// <param name="predicate">The predicate to match the value against.</param>
/// <returns>A result which contains the value of the original result
/// if the value matches <paramref name="predicate"/>, otherwise an error.
/// If the original result does not contain a value, that result will be returned.</returns>
[Pure]
public Result<T> Where(
Func<T, bool> predicate) =>
Validate(predicate, null);
Validate(predicate);
#endif

/// <summary>
Expand Down

0 comments on commit 273c0c7

Please sign in to comment.