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

Add IPStatus extension method GetLocalizedDescription #32

Merged
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ dotnet_diagnostic.S1135.severity = suggestion # https://github.com/atc-net
dotnet_diagnostic.CA1003.severity = none # Change the event to replace the type 'System.Action?' with a generic EventHandler, for example EventHandler<T>, where T is a valid EventArgs
dotnet_diagnostic.CA1056.severity = none # URI properties should not be strings
dotnet_diagnostic.CA1812.severity = none # Internal class that is apparently never instantiated.
dotnet_diagnostic.CA1859.severity = suggestion # Use concrete types when possible for improved performance
dotnet_diagnostic.CA1860.severity = suggestion # Prefer comparing 'Count' to 0 rather than using 'Any()', both for clarity and for performance
dotnet_diagnostic.CA1861.severity = suggestion # 'static readonly' fields over constant array arguments if the called method is called repeatedly and is not mutating the passed array
dotnet_diagnostic.CA2007.severity = none # Consider calling ConfigureAwait on the awaited task
dotnet_diagnostic.CA2227.severity = none # Collection properties should be read only
dotnet_diagnostic.CA5400.severity = none # Ensure HttpClient certificate revocation list check is not disabled
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.82" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.118" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.8.0.76515" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.14.0.81108" PrivateAssets="All" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions docs/CodeDoc/Atc.Network/Atc.Network.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,38 @@ ConnectionStateEventArgs.



<br />

## IPStatusExtensions
Provides extension methods for the `System.Net.NetworkInformation.IPStatus` enumeration.
><b>Remarks:</b> This static class extends the `System.Net.NetworkInformation.IPStatus` enumeration, allowing for easy retrieval of localized string descriptions for each IP status value. This can be particularly useful for displaying user-friendly status messages in applications that perform network operations and diagnostics.

>```csharp
>public static class IPStatusExtensions
>```

### Static Methods

#### GetLocalizedDescription
>```csharp
>string GetLocalizedDescription(this IPStatus ipStatus)
>```
><b>Summary:</b> Retrieves a localized description for a specified `System.Net.NetworkInformation.IPStatus` value.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`ipStatus`&nbsp;&nbsp;-&nbsp;&nbsp;The value for which a localized description is needed.<br />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in markdown-generator for "see cref", this will be fixed in Atc first.

>
><b>Returns:</b> A localized string representation of the specified `System.Net.NetworkInformation.IPStatus` value.
>
><b>Code example:</b>
>```csharp
>This example shows how to call the method on an instance of the enumeration:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous whitespace in 2 places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in markdown-generator for "see cref", this will be fixed in Atc first.

>
>var status = IPStatus.TimedOut;
>var description = status.GetLocalizedDescription();
>Console.WriteLine(description); // Outputs the localized description for the IPStatus.TimedOut
>```

<br />

## LoggingEventIdConstants
Expand Down
1 change: 1 addition & 0 deletions docs/CodeDoc/Atc.Network/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [IPScannerConfigExtensions](Atc.Network.md#ipscannerconfigextensions)
- [IPScannerProgressReportingType](Atc.Network.md#ipscannerprogressreportingtype)
- [IPServicePortExaminationLevel](Atc.Network.md#ipserviceportexaminationlevel)
- [IPStatusExtensions](Atc.Network.md#ipstatusextensions)
- [LoggingEventIdConstants](Atc.Network.md#loggingeventidconstants)
- [NetworkQualityCategoryType](Atc.Network.md#networkqualitycategorytype)
- [ServiceProtocolType](Atc.Network.md#serviceprotocoltype)
Expand Down
3 changes: 3 additions & 0 deletions docs/CodeDoc/Atc.Network/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
- GetTasksToProcessCount(this IPScannerConfig ipScannerConfig)
- [IPScannerProgressReportingType](Atc.Network.md#ipscannerprogressreportingtype)
- [IPServicePortExaminationLevel](Atc.Network.md#ipserviceportexaminationlevel)
- [IPStatusExtensions](Atc.Network.md#ipstatusextensions)
- Static Methods
- GetLocalizedDescription(this IPStatus ipStatus)
- [LoggingEventIdConstants](Atc.Network.md#loggingeventidconstants)
- [NetworkQualityCategoryType](Atc.Network.md#networkqualitycategorytype)
- [ServiceProtocolType](Atc.Network.md#serviceprotocoltype)
Expand Down
5 changes: 2 additions & 3 deletions sample/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ATC coding rules - https://github.com/atc-net/atc-coding-rules
# Version: 1.0.9
# Updated: 02-08-2022
# Updated: 01-02-2022
# Location: sample
# Distribution: DotNet6
# Inspired by: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options
Expand Down Expand Up @@ -49,5 +49,4 @@
dotnet_diagnostic.CA1016.severity = none # Mark assemblies with assembly version
dotnet_diagnostic.CA1303.severity = none # Do not pass literals as localized parameters

dotnet_diagnostic.S3904.severity = none # Provide an 'AssemblyVersion' attribute for assembly

dotnet_diagnostic.S3904.severity = none # Provide an 'AssemblyVersion' attribute for assembly
6 changes: 3 additions & 3 deletions sample/Atc.Network.Console.Tcp/Atc.Network.Console.Tcp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.349" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Atc" Version="2.0.395" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions sample/Atc.Network.Console.Udp/Atc.Network.Console.Udp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.349" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Atc" Version="2.0.395" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.Network/Atc.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.349" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Atc" Version="2.0.395" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
63 changes: 63 additions & 0 deletions src/Atc.Network/Extensions/IPStatusExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ReSharper disable once CheckNamespace
// ReSharper disable LocalizableElement
namespace Atc.Network;

/// <summary>
/// Provides extension methods for the <see cref="IPStatus"/> enumeration.
/// </summary>
/// <remarks>
/// This static class extends the <see cref="IPStatus"/> enumeration,
/// allowing for easy retrieval of localized string descriptions for each IP status value.
/// This can be particularly useful for displaying user-friendly status messages in applications
/// that perform network operations and diagnostics.
/// </remarks>
public static class IPStatusExtensions
{
/// <summary>
/// Retrieves a localized description for a specified <see cref="IPStatus"/> value.
/// </summary>
/// <param name="ipStatus">The <see cref="IPStatus"/> value for which a localized description is needed.</param>
/// <returns>
/// A localized string representation of the specified <see cref="IPStatus"/> value.
/// </returns>
/// <exception cref="SwitchCaseDefaultException">
/// Thrown when the provided <paramref name="ipStatus"/> does not match any of the known cases.
/// </exception>
/// <example>
/// This example shows how to call the <see cref="GetLocalizedDescription"/> method on an instance of the <see cref="IPStatus"/> enumeration:
/// <code>
/// var status = IPStatus.TimedOut;
/// var description = status.GetLocalizedDescription();
/// Console.WriteLine(description); // Outputs the localized description for the IPStatus.TimedOut
/// </code>
/// </example>
public static string GetLocalizedDescription(
this IPStatus ipStatus)
=> ipStatus switch
{
IPStatus.Unknown => EnumResources.IPStatusUnknown,
IPStatus.Success => EnumResources.IPStatusSuccess,
IPStatus.DestinationNetworkUnreachable => EnumResources.IPStatusDestinationNetworkUnreachable,
IPStatus.DestinationHostUnreachable => EnumResources.IPStatusDestinationHostUnreachable,
IPStatus.DestinationProhibited => EnumResources.IPStatusDestinationProhibited,
IPStatus.DestinationPortUnreachable => EnumResources.IPStatusDestinationPortUnreachable,
IPStatus.NoResources => EnumResources.IPStatusNoResources,
IPStatus.BadOption => EnumResources.IPStatusBadOption,
IPStatus.HardwareError => EnumResources.IPStatusHardwareError,
IPStatus.PacketTooBig => EnumResources.IPStatusPacketTooBig,
IPStatus.TimedOut => EnumResources.IPStatusTimedOut,
IPStatus.BadRoute => EnumResources.IPStatusBadRoute,
IPStatus.TtlExpired => EnumResources.IPStatusTtlExpired,
IPStatus.TtlReassemblyTimeExceeded => EnumResources.IPStatusTtlReassemblyTimeExceeded,
IPStatus.ParameterProblem => EnumResources.IPStatusParameterProblem,
IPStatus.SourceQuench => EnumResources.IPStatusSourceQuench,
IPStatus.BadDestination => EnumResources.IPStatusBadDestination,
IPStatus.DestinationUnreachable => EnumResources.IPStatusDestinationUnreachable,
IPStatus.TimeExceeded => EnumResources.IPStatusTimeExceeded,
IPStatus.BadHeader => EnumResources.IPStatusBadHeader,
IPStatus.UnrecognizedNextHeader => EnumResources.IPStatusUnrecognizedNextHeader,
IPStatus.IcmpError => EnumResources.IPStatusIcmpError,
IPStatus.DestinationScopeMismatch => EnumResources.IPStatusDestinationScopeMismatch,
_ => throw new SwitchCaseDefaultException(ipStatus),
};
}
1 change: 1 addition & 0 deletions src/Atc.Network/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
global using System.ComponentModel.DataAnnotations;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;

global using System.Net;
global using System.Net.NetworkInformation;
global using System.Net.Sockets;
Expand Down
Loading
Loading