Skip to content

Commit 91fe1e6

Browse files
committed
feat(devices): pure RustPlusErrorCode -> DeviceReachability mapper
1 parent 6adc38c commit 91fe1e6

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using RustPlusApi.Data;
2+
using RustPlusBot.Abstractions.Connections;
3+
4+
namespace RustPlusBot.Features.Connections.Listening;
5+
6+
/// <summary>Maps a Rust+ response outcome to <see cref="DeviceReachability"/>. The ONLY place RustPlusErrorCode is interpreted.</summary>
7+
internal static class ReachabilityMapping
8+
{
9+
/// <summary>Reachable on success; Removed for not_found; NoPrivilege for access_denied; NoResponse otherwise.</summary>
10+
/// <param name="isSuccess">Whether the Rust+ response succeeded.</param>
11+
/// <param name="errorCode">The error code if the response failed, or null.</param>
12+
/// <returns>The mapped <see cref="DeviceReachability"/> state.</returns>
13+
public static DeviceReachability FromResponse(bool isSuccess, RustPlusErrorCode? errorCode)
14+
{
15+
if (isSuccess)
16+
{
17+
return DeviceReachability.Reachable;
18+
}
19+
20+
return errorCode switch
21+
{
22+
RustPlusErrorCode.NotFound => DeviceReachability.Removed,
23+
RustPlusErrorCode.AccessDenied => DeviceReachability.NoPrivilege,
24+
_ => DeviceReachability.NoResponse,
25+
};
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using RustPlusApi.Data;
2+
using RustPlusBot.Abstractions.Connections;
3+
using RustPlusBot.Features.Connections.Listening;
4+
5+
namespace RustPlusBot.Features.Connections.Tests;
6+
7+
public sealed class ReachabilityMappingTests
8+
{
9+
[Fact]
10+
public void Success_IsReachable() =>
11+
Assert.Equal(DeviceReachability.Reachable, ReachabilityMapping.FromResponse(true, null));
12+
13+
[Theory]
14+
[InlineData(RustPlusErrorCode.NotFound, DeviceReachability.Removed)]
15+
[InlineData(RustPlusErrorCode.AccessDenied, DeviceReachability.NoPrivilege)]
16+
[InlineData(RustPlusErrorCode.Unknown, DeviceReachability.NoResponse)]
17+
[InlineData(RustPlusErrorCode.ServerError, DeviceReachability.NoResponse)]
18+
[InlineData(RustPlusErrorCode.RateLimit, DeviceReachability.NoResponse)]
19+
public void Failure_MapsByCode(RustPlusErrorCode code, DeviceReachability expected) =>
20+
Assert.Equal(expected, ReachabilityMapping.FromResponse(false, code));
21+
22+
[Fact]
23+
public void Failure_NullCode_IsNoResponse() =>
24+
Assert.Equal(DeviceReachability.NoResponse, ReachabilityMapping.FromResponse(false, null));
25+
}

0 commit comments

Comments
 (0)