feat: add Description/ShortName attributes to NestPhase with extension methods

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-21 19:38:54 -04:00
parent 9903478d3e
commit b1e872577c
2 changed files with 70 additions and 6 deletions

View File

@@ -1,16 +1,52 @@
using OpenNest.Geometry;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace OpenNest
{
[AttributeUsage(AttributeTargets.Field)]
internal class ShortNameAttribute(string name) : Attribute
{
public string Name { get; } = name;
}
public enum NestPhase
{
Linear,
RectBestFit,
Pairs,
Nfp,
Extents,
Custom
[Description("Trying rotations..."), ShortName("Linear")] Linear,
[Description("Trying best fit..."), ShortName("BestFit")] RectBestFit,
[Description("Trying pairs..."), ShortName("Pairs")] Pairs,
[Description("Trying NFP..."), ShortName("NFP")] Nfp,
[Description("Trying extents..."), ShortName("Extents")] Extents,
[Description("Custom"), ShortName("Custom")] Custom
}
public static class NestPhaseExtensions
{
private static readonly ConcurrentDictionary<NestPhase, string> DisplayNames = new();
private static readonly ConcurrentDictionary<NestPhase, string> ShortNames = new();
public static string DisplayName(this NestPhase phase)
{
return DisplayNames.GetOrAdd(phase, p =>
{
var field = typeof(NestPhase).GetField(p.ToString());
var attr = field?.GetCustomAttribute<DescriptionAttribute>();
return attr?.Description ?? p.ToString();
});
}
public static string ShortName(this NestPhase phase)
{
return ShortNames.GetOrAdd(phase, p =>
{
var field = typeof(NestPhase).GetField(p.ToString());
var attr = field?.GetCustomAttribute<ShortNameAttribute>();
return attr?.Name ?? p.ToString();
});
}
}
public class PhaseResult

View File

@@ -0,0 +1,28 @@
namespace OpenNest.Tests;
public class NestPhaseExtensionsTests
{
[Theory]
[InlineData(NestPhase.Linear, "Trying rotations...")]
[InlineData(NestPhase.RectBestFit, "Trying best fit...")]
[InlineData(NestPhase.Pairs, "Trying pairs...")]
[InlineData(NestPhase.Nfp, "Trying NFP...")]
[InlineData(NestPhase.Extents, "Trying extents...")]
[InlineData(NestPhase.Custom, "Custom")]
public void DisplayName_ReturnsDescription(NestPhase phase, string expected)
{
Assert.Equal(expected, phase.DisplayName());
}
[Theory]
[InlineData(NestPhase.Linear, "Linear")]
[InlineData(NestPhase.RectBestFit, "BestFit")]
[InlineData(NestPhase.Pairs, "Pairs")]
[InlineData(NestPhase.Nfp, "NFP")]
[InlineData(NestPhase.Extents, "Extents")]
[InlineData(NestPhase.Custom, "Custom")]
public void ShortName_ReturnsShortLabel(NestPhase phase, string expected)
{
Assert.Equal(expected, phase.ShortName());
}
}