diff --git a/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs b/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs index ed9d602..b1442a6 100644 --- a/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs +++ b/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs @@ -202,7 +202,7 @@ public virtual AbsolutePath GetKnownPath(KnownPath knownPath) // ReSharper disable once InconsistentNaming AbsolutePath GetXDGBaseDirectory(string environmentVariable, Func 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); diff --git a/src/NexusMods.Paths/IOSInformation.cs b/src/NexusMods.Paths/IOSInformation.cs index ba9d399..c8f5e3e 100644 --- a/src/NexusMods.Paths/IOSInformation.cs +++ b/src/NexusMods.Paths/IOSInformation.cs @@ -52,10 +52,15 @@ TOut MatchPlatform( Func? onLinux = null, Func? 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? 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(); } /// @@ -80,10 +85,15 @@ TOut MatchPlatform( FuncRef? onLinux = null, FuncRef? 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? 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); } /// @@ -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(); } /// /// Switches on the current platform and allows to be - /// passed to the each handler, preventing lambda allocations. + /// passed to each handler, preventing lambda allocations. /// /// /// @@ -152,34 +143,15 @@ void SwitchPlatform( ActionRef? onLinux = null, ActionRef? 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? 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); } /// @@ -197,10 +169,29 @@ bool IsPlatformSupported() /// bool IsUnix() => IsLinux || IsOSX; + /// + /// Throws for the current platform. + /// + /// + [DoesNotReturn] + void ThrowUnsupported() => throw new PlatformNotSupportedException($"The operation or feature isn't unsupported on the current platform `{Platform}`"); + + /// + /// Throws for the current platform. + /// + /// + [DoesNotReturn] + T ThrowUnsupported() + { + ThrowUnsupported(); + return default; + } + /// /// Guard statement for platform support. /// /// Thrown when the current platform is not supported. + [Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")] void PlatformSupportedGuard() { if (!IsPlatformSupported()) @@ -211,6 +202,7 @@ void PlatformSupportedGuard() /// Creates a new . /// /// + [Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")] PlatformNotSupportedException CreatePlatformNotSupportedException() { return new PlatformNotSupportedException($"The current platform is not supported: {Platform}"); diff --git a/tests/NexusMods.Paths.Tests/OSInformationTests.cs b/tests/NexusMods.Paths.Tests/OSInformationTests.cs index 5b365cb..d34ca0e 100644 --- a/tests/NexusMods.Paths.Tests/OSInformationTests.cs +++ b/tests/NexusMods.Paths.Tests/OSInformationTests.cs @@ -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(); - } - - [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(); - } - [Theory] [MemberData(nameof(PlatformsMemberData))] public void Test_IsWindows(OSPlatform platform)