From cd875d34505e4de215ef46e9e9ddcc86c63bce0e Mon Sep 17 00:00:00 2001 From: Khamraj Rohit Date: Mon, 26 Aug 2024 17:10:38 -0400 Subject: [PATCH] documentation wip --- MaybeError/Errors/Error.cs | 40 ++++++++++++++ MaybeError/Errors/ExceptionError.cs | 36 ++++++++++++ MaybeError/Maybe.cs | 86 +++++++++-------------------- MaybeError/MaybeExtensions.cs | 2 +- MaybeError/ValueMaybe.cs | 23 ++++++++ MaybeErrorTests/MaybeTests.cs | 1 + MaybeErrorTests/ValueMaybeTests.cs | 1 + 7 files changed, 128 insertions(+), 61 deletions(-) create mode 100644 MaybeError/Errors/Error.cs create mode 100644 MaybeError/Errors/ExceptionError.cs diff --git a/MaybeError/Errors/Error.cs b/MaybeError/Errors/Error.cs new file mode 100644 index 0000000..3654bd3 --- /dev/null +++ b/MaybeError/Errors/Error.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MaybeError.Errors; + +/// +/// An information about an error that occured +/// +/// Message +/// Detailed information of the error +/// Even more detailed information that is useful for logging +public class Error(string message, string? details = null, string? devDetails = null) +{ + public readonly string Message = message; + public readonly string? Details = details; + public readonly string? DevDetails = devDetails; + + public virtual Exception GetException() + { + return new Exception(Message); + } + + public override string ToString() + { +#if DEBUG + return $"{Message}\n{Details}\n{DevDetails}"; +#else + return $"{Message}\n{Details}"; +#endif + } + + public static implicit operator string(Error e) + { + return e.ToString(); + } +} + diff --git a/MaybeError/Errors/ExceptionError.cs b/MaybeError/Errors/ExceptionError.cs new file mode 100644 index 0000000..c6ec7c9 --- /dev/null +++ b/MaybeError/Errors/ExceptionError.cs @@ -0,0 +1,36 @@ +namespace MaybeError.Errors; + +public class ExceptionError : ExceptionError +{ + public ExceptionError(string message, Exception exception) : base(message, exception) + { + } + public ExceptionError(Exception exception) : base(exception) + { + } +} + +public class ExceptionError : Error where T : Exception +{ + public T Exception { get; set; } + + public ExceptionError(string message, T exception) : base(message, exception.Message, exception.StackTrace) + { + Exception = exception; + } + + public ExceptionError(T exception) : base(exception.Message, devDetails: exception.StackTrace) + { + Exception = exception; + } + + public override T GetException() + { + return Exception; + } + + public override string ToString() + { + return $"{Message}\n{Exception}"; + } +} \ No newline at end of file diff --git a/MaybeError/Maybe.cs b/MaybeError/Maybe.cs index 4977b01..4e3e4c6 100644 --- a/MaybeError/Maybe.cs +++ b/MaybeError/Maybe.cs @@ -1,4 +1,6 @@ -using System.Diagnostics.CodeAnalysis; +using MaybeError.Errors; + +using System.Diagnostics.CodeAnalysis; namespace MaybeError; @@ -48,23 +50,31 @@ public readonly struct Maybe : IMaybe where T : class where E: Error { [MemberNotNullWhen(true, nameof(Error))] public readonly bool HasError { get; } - public readonly bool HasValue { get; } + public readonly bool HasValue => !HasError; public readonly E? Error { get; } public readonly T Value => HasError ? throw Error.GetException() : _value!; private readonly T? _value; + /// + /// Creates a new with a default(null) value + /// public Maybe() { _value = default; } + /// + /// Creates a new with a + /// public Maybe(T value) { _value = value; - HasValue = true; } + /// + /// Creates a new with an error + /// public Maybe(E e) { Error = e; @@ -95,23 +105,31 @@ public readonly struct Maybe : IMaybe where T : class { [MemberNotNullWhen(true, nameof(Error))] public readonly bool HasError { get; } - public readonly bool HasValue { get; } + public readonly bool HasValue => !HasError; public readonly Error? Error { get; } public readonly T Value => HasError ? throw Error.GetException() : _value!; private readonly T? _value; + /// + /// Creates a new with a default(null) value + /// public Maybe() { _value = default; } + /// + /// Creates a new with a + /// public Maybe(T value) { _value = value; - HasValue = true; } + /// + /// Creates a new with an exception + /// public Maybe(Exception e) { Error = new ExceptionError(e); @@ -119,6 +137,9 @@ public readonly struct Maybe : IMaybe where T : class _value = default; } + /// + /// Creates a new with an error + /// public Maybe(Error e) { Error = e; @@ -151,58 +172,3 @@ public readonly struct Maybe : IMaybe where T : class } -public class Error(string message, string? details = null, string? debugDetails = null) -{ - public readonly string Message = message; - public readonly string? Details = details; - public readonly string? DebugDetails = debugDetails; - - public virtual Exception GetException() - { - return new Exception(Message); - } - - public override string ToString() - { - return $"{Message}\n{Details}\n{DebugDetails}"; - } - - public static implicit operator string(Error e) - { - return e.ToString(); - } -} - -public class ExceptionError : ExceptionError -{ - public ExceptionError(string message, Exception exception) : base(message, exception) - { - } - public ExceptionError(Exception exception) : base(exception) - { - } -} -public class ExceptionError : Error where T : Exception -{ - public T Exception { get; set; } - - public ExceptionError(string message, T exception) : base(message, exception.Message, exception.StackTrace) - { - Exception = exception; - } - - public ExceptionError(T exception) : base(exception.Message, debugDetails: exception.StackTrace) - { - Exception = exception; - } - - public override T GetException() - { - return Exception; - } - - public override string ToString() - { - return $"{Message}\n{Exception}"; - } -} \ No newline at end of file diff --git a/MaybeError/MaybeExtensions.cs b/MaybeError/MaybeExtensions.cs index ae60f1e..7c48e38 100644 --- a/MaybeError/MaybeExtensions.cs +++ b/MaybeError/MaybeExtensions.cs @@ -1,4 +1,4 @@ -#nullable enable +using MaybeError.Errors; namespace MaybeError; diff --git a/MaybeError/ValueMaybe.cs b/MaybeError/ValueMaybe.cs index 09d55bc..e29fce5 100644 --- a/MaybeError/ValueMaybe.cs +++ b/MaybeError/ValueMaybe.cs @@ -1,4 +1,6 @@  +using MaybeError.Errors; + using System.Diagnostics.CodeAnalysis; namespace MaybeError; @@ -22,17 +24,26 @@ public readonly struct ValueMaybe : IValueMaybe where T : struct whe private readonly T _value; + /// + /// Creates a new with a default value + /// public ValueMaybe() { _value = default; } + /// + /// Creates a new with a + /// public ValueMaybe(T value) { _value = value; HasError = false; } + /// + /// Creates a new with an error + /// public ValueMaybe(E e) { Error = e; @@ -68,17 +79,26 @@ public readonly struct ValueMaybe : IValueMaybe where T : struct private readonly T _value; + /// + /// Creates a new with a default value + /// public ValueMaybe() { _value = default; } + /// + /// Creates a new with a + /// public ValueMaybe(T value) { _value = value; HasError = false; } + /// + /// Creates a new with an exception + /// public ValueMaybe(Exception e) { Error = new ExceptionError(e); @@ -86,6 +106,9 @@ public readonly struct ValueMaybe : IValueMaybe where T : struct _value = default; } + /// + /// Creates a new with an error + /// public ValueMaybe(Error e) { Error = e; diff --git a/MaybeErrorTests/MaybeTests.cs b/MaybeErrorTests/MaybeTests.cs index 79f42b8..28b4cc3 100644 --- a/MaybeErrorTests/MaybeTests.cs +++ b/MaybeErrorTests/MaybeTests.cs @@ -1,4 +1,5 @@ using MaybeError; +using MaybeError.Errors; namespace MaybeErrorTests; diff --git a/MaybeErrorTests/ValueMaybeTests.cs b/MaybeErrorTests/ValueMaybeTests.cs index 3a7f6d5..744137e 100644 --- a/MaybeErrorTests/ValueMaybeTests.cs +++ b/MaybeErrorTests/ValueMaybeTests.cs @@ -1,4 +1,5 @@ using MaybeError; +using MaybeError.Errors; namespace MaybeErrorTests;