Customizations

Intellenum allows developers to tailor enum behavior to specific needs by utilizing customizable options.

Global customizations:

  • [assembly: IntellenumDefaults(customizations: Customizations.TreatNumberAsStringInSystemTextJson)]: This customization instructs Intellenum to treat enums as strings when serializing them with System.Text.Json. This is particularly useful for scenarios where enums need to be represented as strings in JSON payloads. Source: tests/SnapshotTests/Config/GlobalConfigTests.cs

Example:

using System;
          using Intellenum;
          
          [assembly: IntellenumDefaults(customizations: Customizations.TreatNumberAsStringInSystemTextJson)]
          
          namespace Whatever;
          
          [Intellenum]
          [Member("Normal", 0)]
          [Member("Gold", 1)]
          public partial class CustomerType { }
          

Enum customizations:

Examples:

[Intellenum]
          [Member("Freezing", 0.0f)]
          [Member("Boiling", 100.0f)]
          [Member("AbsoluteZero", -273.15f)]
          public partial class Centigrade;
          
          
          [Intellenum]
          [Member("Unspecified", "[UNSPCFD]")]
          [Member("Invalid", "[INVLD]")]
          [Member("Standard", "[STD]")]
          [Member("Preferred", "[PRF]")]
          public partial class VendorType;
              
          [Intellenum]
          [Member("Salt", 1)]
          [Member("Pepper", 2)]
          public partial class CondimentMixedMembers
          {
              public static readonly CondimentMixedMembers Mayo = new CondimentMixedMembers("Mayo", 5);
              public static readonly CondimentMixedMembers Ketchup = new CondimentMixedMembers("Ketchup", 6);
          
              static CondimentMixedMembers()
              {
                  Member("Vinegar", 3);
                  Member("Mustard", 4);
              }
          }
          

Customizations validation:

Example:

[Fact]
              public async Task Not_valid_customization()
              {
                  var source = @"using System;
          using Intellenum;
          
          [assembly: IntellenumDefaults(customizations: (Customizations)666)]
          
          namespace Whatever;
          
          [Intellenum]
          [Member(""Normal"", 0)]
          [Member(""Gold"", 1)]
          public partial struct CustomerType { }
          ";
          
                  await new TestRunner()
                      .WithSource(source)
                      .ValidateWith(Validate)
                      .RunOnAllFrameworks();
          
                  void Validate(ImmutableArray diagnostics)
                  {
                      diagnostics.Should().HaveCount(1);
          
                      diagnostics.Should().SatisfyRespectively(
                          first =>
                          {
                              first.Id.Should().Be("INTELLENUM019");
                              first.ToString().Should().Be(
                                  "(4,12): error INTELLENUM019: The Customizations specified do not match any known customizations - see the Customizations type");
                          });
                  }
              }
          

Key points:

Top-Level Directory Explanations

samples/ - This directory contains example projects demonstrating the usage of Intellenum.

samples/Intellenum.Examples/ - Contains various example projects demonstrating different aspects of Intellenum, such as serialization, conversion, syntax examples, types, typical scenarios, and more.

samples/WebApplication/ - Contains a sample web application that uses Intellenum.

src/ - This directory contains the source code of the Intellenum library.

src/Benchmarks/ - Contains benchmark tests for the Intellenum library.

src/Intellenum.CodeFixers/ - Contains code fixers for the Intellenum library.

src/Intellenum.SharedTypes/ - Contains shared types used across the Intellenum library.

src/Intellenum/ - Contains the main source code for the Intellenum library. This directory is further divided into subdirectories for diagnostics, extensions, generators, member building, properties, rules, static constructor building, templates, and more.

tests/ - This directory contains test projects for the Intellenum library.

tests/AnalyzerTests/ - Contains unit tests for the Intellenum analyzer.

tests/ConsumerTests/ - Contains tests for consuming the Intellenum library.

tests/Intellenum.Tests/ - Contains additional tests for the Intellenum library.

tests/Shared/ - Contains shared test files.

tests/SnapshotTests/ - Contains snapshot tests for the Intellenum library.