diff --git a/.gitignore b/.gitignore index ac16142..b7f4e76 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vs bin obj +*.user diff --git a/Utility.Result.Tests/ObjectResultTests.cs b/Utility.Result.Tests/ObjectResultTests.cs new file mode 100644 index 0000000..f4ed005 --- /dev/null +++ b/Utility.Result.Tests/ObjectResultTests.cs @@ -0,0 +1,17 @@ +using Musts; + +namespace Utility; + +public class ObjectResultTests +{ + [Test] + public void Ok_Result_WithValue_Returns_Success() + { + ObjectResult result = OkObjectResult.Ok(99); + + result.Value.MustBeEqual(99); + result.MustBeSuccess(); + result.Error.MustBeNullOrEmpty(); + result.ErrorCode.MustBeZero(); + } +} diff --git a/Utility.Result.Tests/ResultTests.cs b/Utility.Result.Tests/ResultTests.cs index 2287ba2..9a46b1b 100644 --- a/Utility.Result.Tests/ResultTests.cs +++ b/Utility.Result.Tests/ResultTests.cs @@ -10,10 +10,10 @@ public void OperatorAnd_Result_Returns_Success() Result result = Result.Ok() && Result.Ok(); result.MustBeSuccess(); - result = Result.Fail("Failed") && Result.Ok(); + result = ErrorResult.Fail("Failed") && Result.Ok(); result.MustBeFailure(); - result = Result.Ok() && Result.Fail("Failed"); + result = Result.Ok() && ErrorResult.Fail("Failed"); result.MustBeFailure(); } @@ -27,30 +27,19 @@ public void Ok_Result_Returns_Success() result.ErrorCode.MustBeZero(); } - [Test] - public void Ok_Result_WithValue_Returns_Success() - { - Result result = Result.Ok(99); - - result.Value.MustBeEqual(99); - result.MustBeSuccess(); - result.Error.MustBeNullOrEmpty(); - result.ErrorCode.MustBeZero(); - } - [Test] public void Fail_Result_Returns_Failure() { - Result result = Result.Fail("The operation failed because of ..."); + Result result = ErrorResult.Fail("The operation failed because of ..."); result.MustBeFailure(); - result.ErrorCode.MustBeZero(); + result.ErrorCode.MustBeEqual(-1); } [Test] public void Fail_Result_WithErrorCode_Returns_Failure() { - Result result = Result.Fail("The operation failed because of ...", -1); + Result result = ErrorResult.Fail("The operation failed because of ...", -1); result.MustBeFailure(); result.ErrorCode.MustBeEqual(-1); diff --git a/Utility.Result.lutconfig b/Utility.Result.lutconfig new file mode 100644 index 0000000..596a860 --- /dev/null +++ b/Utility.Result.lutconfig @@ -0,0 +1,6 @@ + + + true + true + 180000 + \ No newline at end of file diff --git a/Utility.Result/ErrorObjectResult.cs b/Utility.Result/ErrorObjectResult.cs new file mode 100644 index 0000000..96e23c1 --- /dev/null +++ b/Utility.Result/ErrorObjectResult.cs @@ -0,0 +1,14 @@ +namespace Utility; + +public record ErrorObjectResult : ObjectResult +{ + protected internal ErrorObjectResult(T value, string error, int errorCode) : base(value, false, error, errorCode) + { + Value = value; + } + + public static ErrorObjectResult Fail(string message, int errorCode = 0) + { + return new ErrorObjectResult(default(T), message, errorCode); + } +} diff --git a/Utility.Result/ErrorResult.cs b/Utility.Result/ErrorResult.cs new file mode 100644 index 0000000..1039f7c --- /dev/null +++ b/Utility.Result/ErrorResult.cs @@ -0,0 +1,27 @@ +namespace Utility; + +/// +/// Represents an error result with an associated error message and error code. +/// +public record ErrorResult : Result +{ + /// + /// Initializes a new instance of the class. + /// + /// The error message associated with the result. + /// The error code associated with the result (optional). + internal ErrorResult(string error, int errorCode = -1) : base(false, error, errorCode) + { + } + + /// + /// Creates a failure result with the specified error message and error code. + /// + /// The error message. + /// The error code (optional). + /// An error result. + public static ErrorResult Fail(string message, int errorCode = -1) + { + return new ErrorResult(message, errorCode); + } +} diff --git a/Utility.Result/ObjectResult.cs b/Utility.Result/ObjectResult.cs new file mode 100644 index 0000000..8f75666 --- /dev/null +++ b/Utility.Result/ObjectResult.cs @@ -0,0 +1,11 @@ +namespace Utility; + +public record ObjectResult : Result +{ + public T Value { get; set; } + + protected internal ObjectResult(T value, bool success, string error, int errorCode) : base(success, error, errorCode) + { + Value = value; + } +} diff --git a/Utility.Result/OkObjectResult.cs b/Utility.Result/OkObjectResult.cs new file mode 100644 index 0000000..70e5d7b --- /dev/null +++ b/Utility.Result/OkObjectResult.cs @@ -0,0 +1,14 @@ +namespace Utility; + +public record OkObjectResult : ObjectResult +{ + protected internal OkObjectResult(T value) : base(value, true, string.Empty, 0) + { + Value = value; + } + + public static OkObjectResult Ok(T value) + { + return new OkObjectResult(value); + } +} diff --git a/Utility.Result/OkResult.cs b/Utility.Result/OkResult.cs new file mode 100644 index 0000000..f47cf56 --- /dev/null +++ b/Utility.Result/OkResult.cs @@ -0,0 +1,14 @@ +namespace Utility; + +/// +/// Represents a successful result with no associated value. +/// +public record OkResult : Result +{ + /// + /// Initializes a new instance of the class. + /// + internal OkResult() : base(true) + { + } +} diff --git a/Utility.Result/Result.cs b/Utility.Result/Result.cs index 424d78c..7e0f0c6 100644 --- a/Utility.Result/Result.cs +++ b/Utility.Result/Result.cs @@ -88,69 +88,22 @@ protected Result(bool success, string error, int errorCode) } /// - /// Creates a failure result with the specified error message and error code. - /// - /// The error message. - /// The error code (optional). - /// A failure result. - public static Result Fail(string message, int errorCode = 0) - { - return new Result(false, message, errorCode); - } - - /// - /// Creates a failure result with the specified error message and error code. + /// Initializes a new instance of the class. /// - /// The type of the value associated with the result. - /// The error message. - /// The error code (optional). - /// A failure result. - public static Result Fail(string message, int errorCode = 0) + /// A value indicating whether the result is a success. + protected Result(bool success) { - return new Result(default(T), false, message, errorCode); + Success = success; + Error = string.Empty; + ErrorCode = 0; } /// /// Creates a success result. /// /// A success result. - public static Result Ok() - { - return new Result(true, string.Empty, 0); - } - - /// - /// Creates a success result with the specified value. - /// - /// The type of the value associated with the result. - /// The value associated with the result. - /// A success result. - public static Result Ok(T value) - { - return new Result(value, true, string.Empty, 0); - } -} - -/// -/// A result from a task which can be a success or failure, with an associated value. -/// -/// The type of the value associated with the result. -public record Result : Result -{ - /// - /// Gets or sets the value associated with the result. - /// - public T Value { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// The value associated with the result. - /// A value indicating whether the result is a success. - /// The error message associated with the result. - /// The error code associated with the result. - protected internal Result(T value, bool success, string error, int errorCode) : base(success, error, errorCode) + public static OkResult Ok() { - Value = value; + return new OkResult(); } }