Add initial project structure with Result type and extension methods
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
.idea/
|
||||||
|
/packages/
|
||||||
|
riderModule.iml
|
||||||
|
/_ReSharper.Caches/
|
||||||
16
Rehlert.RoP.sln
Normal file
16
Rehlert.RoP.sln
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rehlert.RoP", "Rehlert.RoP\Rehlert.RoP.csproj", "{C043B9E4-712D-4710-972E-1D89A31A9E2D}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{C043B9E4-712D-4710-972E-1D89A31A9E2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C043B9E4-712D-4710-972E-1D89A31A9E2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C043B9E4-712D-4710-972E-1D89A31A9E2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C043B9E4-712D-4710-972E-1D89A31A9E2D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
28
Rehlert.RoP/Extensions/FallbackExtensions.cs
Normal file
28
Rehlert.RoP/Extensions/FallbackExtensions.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
namespace Rehlert.RoP.Extensions;
|
||||||
|
|
||||||
|
public static class FallbackExtensions
|
||||||
|
{
|
||||||
|
public static TIn FallbackWith<TIn, TErr>(this Result<TIn, TErr> value, TIn defaultValue)
|
||||||
|
{
|
||||||
|
var result = value.Match(
|
||||||
|
ok => ok.Value,
|
||||||
|
_ => defaultValue
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<TIn> FallbackWith<TIn, TErr>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
TIn defaultValue
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
var result = valRes.Match(
|
||||||
|
ok => ok.Value,
|
||||||
|
_ => defaultValue
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
Rehlert.RoP/Extensions/OnErrorExtensions.cs
Normal file
55
Rehlert.RoP/Extensions/OnErrorExtensions.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
namespace Rehlert.RoP.Extensions;
|
||||||
|
|
||||||
|
public static class OnErrorExtensions
|
||||||
|
{
|
||||||
|
public static Result<TIn, TErr> OnError<TIn, TErr>(this Result<TIn, TErr> value, Action<TErr> action)
|
||||||
|
{
|
||||||
|
value.Match(
|
||||||
|
_ => { },
|
||||||
|
err => action(err.Value)
|
||||||
|
);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<TIn, TErr>> OnError<TIn, TErr>(
|
||||||
|
this Result<TIn, TErr> value,
|
||||||
|
Func<TErr, Task> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
await value.Match(
|
||||||
|
_ => Task.CompletedTask,
|
||||||
|
err => func(err.Value)
|
||||||
|
);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<TIn, TErr>> OnError<TIn, TErr>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
Func<TErr, Task> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
await valRes.Match(
|
||||||
|
_ => Task.CompletedTask,
|
||||||
|
err => func(err.Value)
|
||||||
|
);
|
||||||
|
|
||||||
|
return valRes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<TIn, TErr>> OnError<TIn, TErr>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
Action<TErr> action
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
valRes.Match(
|
||||||
|
_ => { },
|
||||||
|
err => action(err.Value)
|
||||||
|
);
|
||||||
|
|
||||||
|
return valRes;
|
||||||
|
}
|
||||||
|
}
|
||||||
118
Rehlert.RoP/Extensions/OnSuccessExtensions.cs
Normal file
118
Rehlert.RoP/Extensions/OnSuccessExtensions.cs
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
namespace Rehlert.RoP.Extensions;
|
||||||
|
|
||||||
|
public static class OnSuccessExtensions
|
||||||
|
{
|
||||||
|
public static Result<TRes, TErr> OnSuccess<TIn, TErr, TRes>(this Result<TIn, TErr> value,
|
||||||
|
Func<TIn, Result<TRes, TErr>> func)
|
||||||
|
{
|
||||||
|
var result = value.Match(
|
||||||
|
ok => func(ok.Value),
|
||||||
|
err => err.Value
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<TRes, TErr>> OnSuccess<TIn, TErr, TRes>(
|
||||||
|
this Result<TIn, TErr> value,
|
||||||
|
Func<TIn, Task<Result<TRes, TErr>>> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var result = await value.Match(
|
||||||
|
ok => func(ok.Value),
|
||||||
|
err => Task.FromResult<Result<TRes, TErr>>(err.Value)
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<TRes, TErr>> OnSuccess<TIn, TErr, TRes>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
Func<TIn, Task<Result<TRes, TErr>>> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
var result = await valRes.Match(
|
||||||
|
ok => func(ok.Value),
|
||||||
|
err => Task.FromResult<Result<TRes, TErr>>(err.Value)
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<TRes, TErr>> OnSuccess<TIn, TErr, TRes>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
Func<TIn, Result<TRes, TErr>> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
var result = valRes.Match(
|
||||||
|
ok => func(ok.Value),
|
||||||
|
err => err.Value
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task OnSuccess<TIn, TErr>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
Func<TIn, Task> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
await valRes.Match(
|
||||||
|
ok => func(ok.Value),
|
||||||
|
err => Task.CompletedTask
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task OnSuccess<TIn, TErr>(
|
||||||
|
this Task<Result<TIn, TErr>> value,
|
||||||
|
Action<TIn> action
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var valRes = await value;
|
||||||
|
valRes.Match(
|
||||||
|
ok => action(ok.Value),
|
||||||
|
_ => { }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task OnSuccess<TIn, TErr>(
|
||||||
|
this Result<TIn, TErr> value,
|
||||||
|
Func<TIn, Task> func
|
||||||
|
)
|
||||||
|
{
|
||||||
|
await value.Match(
|
||||||
|
ok => func(ok.Value),
|
||||||
|
_ => Task.CompletedTask
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void OnSuccess<TIn, TErr>(
|
||||||
|
this Result<TIn, TErr> value,
|
||||||
|
Action<TIn> action
|
||||||
|
)
|
||||||
|
{
|
||||||
|
value.Match(
|
||||||
|
ok => action(ok.Value),
|
||||||
|
_ => { }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result<(T1, T2), TErr> Combine<T1, T2, TErr>(this Result<T1, TErr> r1, Result<T2, TErr> r2)
|
||||||
|
{
|
||||||
|
if (r1 is Result<T1, TErr>.Error err)
|
||||||
|
return err.Value;
|
||||||
|
if (r2 is Result<T2, TErr>.Error err2)
|
||||||
|
return err2.Value;
|
||||||
|
return (r1.UnwrapOk().Value, r2.UnwrapOk().Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Result<(T1, T2), TErr>> Combine<T1, T2, TErr>(this Task<Result<T1, TErr>> r1,
|
||||||
|
Result<T2, TErr> r2)
|
||||||
|
{
|
||||||
|
var val = await r1;
|
||||||
|
return val.Combine(r2);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Rehlert.RoP/Rehlert.RoP.csproj
Normal file
13
Rehlert.RoP/Rehlert.RoP.csproj
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Dunet" Version="1.11.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
13
Rehlert.RoP/Result.cs
Normal file
13
Rehlert.RoP/Result.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Dunet;
|
||||||
|
|
||||||
|
namespace Rehlert.RoP;
|
||||||
|
|
||||||
|
[Union]
|
||||||
|
public partial record Result<TOk, TError>
|
||||||
|
where TOk : notnull
|
||||||
|
where TError : notnull
|
||||||
|
{
|
||||||
|
public partial record Ok(TOk Value);
|
||||||
|
|
||||||
|
public partial record Error(TError Value);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user