Skip to content

Commit

Permalink
Simplify and unify manifest reading implementation
Browse files Browse the repository at this point in the history
No need for a separate Manifest class, just extend the existing SponsorLink class.
  • Loading branch information
kzu committed Jun 7, 2024
1 parent a0ae727 commit 4fca946
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 241 deletions.
28 changes: 14 additions & 14 deletions src/SponsorLink/SponsorLink/DiagnosticsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class DiagnosticsManager
/// <param name="sponsorable">The names of the sponsorable accounts that can be funded for the given product.</param>
/// <param name="product">The product or project developed by the sponsorable(s).</param>
/// <param name="prefix">Custom prefix to use for diagnostic IDs.</param>
/// <param name="kind">The kind of diagnostic to create.</param>
/// <param name="status">The kind of status diagnostic to create.</param>
/// <returns>The given <see cref="DiagnosticDescriptor"/>.</returns>
/// <exception cref="NotImplementedException">The <paramref name="kind"/> is not one of the known ones.</exception>
public DiagnosticDescriptor GetDescriptor(string[] sponsorable, string product, string prefix, DiagnosticKind kind) => kind switch
/// <exception cref="NotImplementedException">The <paramref name="status"/> is not one of the known ones.</exception>
public DiagnosticDescriptor GetDescriptor(string[] sponsorable, string product, string prefix, SponsorStatus status) => status switch
{
DiagnosticKind.Unknown => CreateUnknown(sponsorable, product, prefix),
DiagnosticKind.Sponsor => CreateSponsor(sponsorable, prefix),
DiagnosticKind.Expiring => CreateExpiring(sponsorable, prefix),
DiagnosticKind.Expired => CreateExpired(sponsorable, prefix),
SponsorStatus.Unknown => CreateUnknown(sponsorable, product, prefix),
SponsorStatus.Sponsor => CreateSponsor(sponsorable, prefix),
SponsorStatus.Expiring => CreateExpiring(sponsorable, prefix),
SponsorStatus.Expired => CreateExpired(sponsorable, prefix),
_ => throw new NotImplementedException(),
};

Expand Down Expand Up @@ -67,23 +67,23 @@ public Diagnostic Push(string product, Diagnostic diagnostic)
/// Gets the status of the given product based on a previously stored diagnostic.
/// </summary>
/// <param name="product">The product to check status for.</param>
/// <returns>Optional <see cref="DiagnosticKind"/> that was reported, if any.</returns>
public DiagnosticKind? GetStatus(string product)
/// <returns>Optional <see cref="SponsorStatus"/> that was reported, if any.</returns>
public SponsorStatus? GetStatus(string product)
{
// NOTE: the SponsorLinkAnalyzer.SetStatus uses diagnostic properties to store the
// kind of diagnostic as a simple string instead of the enum. We do this so that
// multiple analyzers or versions even across multiple products, which all would
// have their own enum, can still share the same diagnostic kind.
if (Diagnostics.TryGetValue(product, out var diagnostic) &&
diagnostic.Properties.TryGetValue(nameof(DiagnosticKind), out var value))
diagnostic.Properties.TryGetValue(nameof(SponsorStatus), out var value))
{
// Switch on value matching DiagnosticKind names
return value switch
{
nameof(DiagnosticKind.Unknown) => DiagnosticKind.Unknown,
nameof(DiagnosticKind.Sponsor) => DiagnosticKind.Sponsor,
nameof(DiagnosticKind.Expiring) => DiagnosticKind.Expiring,
nameof(DiagnosticKind.Expired) => DiagnosticKind.Expired,
nameof(SponsorStatus.Unknown) => SponsorStatus.Unknown,
nameof(SponsorStatus.Sponsor) => SponsorStatus.Sponsor,
nameof(SponsorStatus.Expiring) => SponsorStatus.Expiring,
nameof(SponsorStatus.Expired) => SponsorStatus.Expired,
_ => null,
};
}
Expand Down
178 changes: 0 additions & 178 deletions src/SponsorLink/SponsorLink/Manifest.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/SponsorLink/SponsorLink/ManifestStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <autogenerated />
#nullable enable
namespace Devlooped.Sponsors;

/// <summary>
/// The resulting status from validation.
/// </summary>
public enum ManifestStatus
{
/// <summary>
/// The manifest couldn't be read at all.
/// </summary>
Unknown,
/// <summary>
/// The manifest was read and is valid (not expired and properly signed).
/// </summary>
Valid,
/// <summary>
/// The manifest was read but has expired.
/// </summary>
Expired,
/// <summary>
/// The manifest was read, but its signature is invalid.
/// </summary>
Invalid,
}
Loading

0 comments on commit 4fca946

Please sign in to comment.