Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better throw helper and switch/matches #58

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading