Skip to content

Commit

Permalink
Better throw helper and switch/matches
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 4, 2024
1 parent dab2ebf commit dfe156a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public virtual AbsolutePath GetKnownPath(KnownPath knownPath)
// ReSharper disable once InconsistentNaming
AbsolutePath GetXDGBaseDirectory(string environmentVariable, Func<IFileSystem, AbsolutePath> defaultFunc)
{
if (!OS.IsLinux) throw OS.CreatePlatformNotSupportedException();
if (!OS.IsLinux) OS.ThrowUnsupported();

var value = Environment.GetEnvironmentVariable(environmentVariable, EnvironmentVariableTarget.Process);
return value is null ? defaultFunc(this) : FromUnsanitizedFullPath(value);
Expand Down
122 changes: 57 additions & 65 deletions src/NexusMods.Paths/IOSInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ TOut MatchPlatform<TOut>(
Func<TOut>? onLinux = null,
Func<TOut>? onOSX = null)
{
if (IsWindows) return onWindows is null ? throw CreatePlatformNotSupportedException() : onWindows();
if (IsLinux) return onLinux is null ? throw CreatePlatformNotSupportedException() : onLinux();
if (IsOSX) return onOSX is null ? throw CreatePlatformNotSupportedException() : onOSX();
throw CreatePlatformNotSupportedException();
Func<TOut>? func = null;

if (IsWindows) func = onWindows;
else if (IsLinux) func = onLinux;
else if (IsOSX) func = onOSX;
else ThrowUnsupported();

if (func is null) ThrowUnsupported();
return func();
}

/// <summary>
Expand All @@ -80,10 +85,15 @@ TOut MatchPlatform<TState, TOut>(
FuncRef<TState, TOut>? onLinux = null,
FuncRef<TState, TOut>? onOSX = null)
{
if (IsWindows) return onWindows is null ? throw CreatePlatformNotSupportedException() : onWindows(ref state);
if (IsLinux) return onLinux is null ? throw CreatePlatformNotSupportedException() : onLinux(ref state);
if (IsOSX) return onOSX is null ? throw CreatePlatformNotSupportedException() : onOSX(ref state);
throw CreatePlatformNotSupportedException();
FuncRef<TState, TOut>? func = null;

if (IsWindows) func = onWindows;
else if (IsLinux) func = onLinux;
else if (IsOSX) func = onOSX;
else ThrowUnsupported();

if (func is null) ThrowUnsupported();
return func(ref state);
}

/// <summary>
Expand All @@ -102,39 +112,20 @@ void SwitchPlatform(
Action? onLinux = null,
Action? onOSX = null)
{
if (IsWindows)
{
if (onWindows is null)
throw CreatePlatformNotSupportedException();

onWindows();
return;
}

if (IsLinux)
{
if (onLinux is null)
throw CreatePlatformNotSupportedException();

onLinux();
return;
}

if (IsOSX)
{
if (onOSX is null)
throw CreatePlatformNotSupportedException();

onOSX();
return;
}

throw CreatePlatformNotSupportedException();
Action? action = null;

if (IsWindows) action = onWindows;
else if (IsLinux) action = onLinux;
else if (IsOSX) action = onOSX;
else ThrowUnsupported();

if (action is null) ThrowUnsupported();
action();
}

/// <summary>
/// Switches on the current platform and allows <paramref name="state"/> to be
/// passed to the each handler, preventing lambda allocations.
/// passed to each handler, preventing lambda allocations.
/// </summary>
/// <param name="onWindows"></param>
/// <param name="onLinux"></param>
Expand All @@ -152,34 +143,15 @@ void SwitchPlatform<TState>(
ActionRef<TState>? onLinux = null,
ActionRef<TState>? onOSX = null)
{
if (IsWindows)
{
if (onWindows is null)
throw CreatePlatformNotSupportedException();

onWindows(ref state);
return;
}

if (IsLinux)
{
if (onLinux is null)
throw CreatePlatformNotSupportedException();

onLinux(ref state);
return;
}

if (IsOSX)
{
if (onOSX is null)
throw CreatePlatformNotSupportedException();

onOSX(ref state);
return;
}

throw CreatePlatformNotSupportedException();
ActionRef<TState>? action = null;

if (IsWindows) action = onWindows;
else if (IsLinux) action = onLinux;
else if (IsOSX) action = onOSX;
else ThrowUnsupported();

if (action is null) ThrowUnsupported();
action(ref state);
}

/// <summary>
Expand All @@ -197,10 +169,29 @@ bool IsPlatformSupported()
/// <returns></returns>
bool IsUnix() => IsLinux || IsOSX;

/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> for the current platform.
/// </summary>
/// <exception cref="PlatformNotSupportedException"/>
[DoesNotReturn]
void ThrowUnsupported() => throw new PlatformNotSupportedException($"The operation or feature isn't unsupported on the current platform `{Platform}`");

/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> for the current platform.
/// </summary>
/// <exception cref="PlatformNotSupportedException"/>
[DoesNotReturn]
T ThrowUnsupported<T>()
{
ThrowUnsupported();
return default;
}

/// <summary>
/// Guard statement for platform support.
/// </summary>
/// <exception cref="PlatformNotSupportedException">Thrown when the current platform is not supported.</exception>
[Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")]
void PlatformSupportedGuard()
{
if (!IsPlatformSupported())
Expand All @@ -211,6 +202,7 @@ void PlatformSupportedGuard()
/// Creates a new <see cref="PlatformNotSupportedException"/>.
/// </summary>
/// <returns></returns>
[Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")]
PlatformNotSupportedException CreatePlatformNotSupportedException()
{
return new PlatformNotSupportedException($"The current platform is not supported: {Platform}");
Expand Down
21 changes: 0 additions & 21 deletions tests/NexusMods.Paths.Tests/OSInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ namespace NexusMods.Paths.Tests;
// ReSharper disable once InconsistentNaming
public class OSInformationTests
{
[Theory]
[MemberData(nameof(PlatformsMemberData))]
public void Test_IsPlatformSupported_True(OSPlatform platform)
{
IOSInformation info = new OSInformation(platform);
info.IsPlatformSupported().Should().BeTrue();

info.Invoking(x => x.PlatformSupportedGuard())
.Should().NotThrow<PlatformNotSupportedException>();
}

[Theory, AutoData]
public void Test_IsPlatformSupported_False(string platformName)
{
IOSInformation info = new OSInformation(OSPlatform.Create(platformName));
info.IsPlatformSupported().Should().BeFalse();

info.Invoking(x => x.PlatformSupportedGuard())
.Should().ThrowExactly<PlatformNotSupportedException>();
}

[Theory]
[MemberData(nameof(PlatformsMemberData))]
public void Test_IsWindows(OSPlatform platform)
Expand Down

0 comments on commit dfe156a

Please sign in to comment.