diff --git a/src/LightObjects.Generated/GeneratedIdentifierSourceGenerator.cs b/src/LightObjects.Generated/GeneratedIdentifierSourceGenerator.cs index 2e02fa4..69e2385 100644 --- a/src/LightObjects.Generated/GeneratedIdentifierSourceGenerator.cs +++ b/src/LightObjects.Generated/GeneratedIdentifierSourceGenerator.cs @@ -89,19 +89,23 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation string? declaredValueType; string? fullValueType; + var supportsProviderBasedTryParse = false; switch (typeArgument.SpecialType) { case SpecialType.System_Int16: declaredValueType = "short"; fullValueType = "Int16"; + supportsProviderBasedTryParse = true; break; case SpecialType.System_Int32: declaredValueType = "int"; fullValueType = "Int32"; + supportsProviderBasedTryParse = true; break; case SpecialType.System_Int64: declaredValueType = "long"; fullValueType = "Int64"; + supportsProviderBasedTryParse = true; break; case SpecialType.System_String: declaredValueType = "string"; @@ -115,7 +119,15 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation break; } - var symbol = new Identifier(containingDeclarations, symbolName, isStruct, isPublic, declaredValueType, fullValueType); + var symbol = new Identifier( + containingDeclarations, + symbolName, + isStruct, + isPublic, + declaredValueType, + fullValueType, + supportsProviderBasedTryParse + ); return symbol; } @@ -131,6 +143,8 @@ private static void GenerateIdentifier(SourceProductionContext context, Immutabl var declaredValueType = symbol.DeclaredValueType; var fullValueType = symbol.FullValueType; + var supportsProviderBasedTryParse = symbol.SupportsProviderBasedTryParse; + var source = new StringBuilder(); source.AppendLine($""" @@ -277,19 +291,22 @@ public static bool TryParse(string s, out {{symbolName}} identifier) """ ); - source.AppendLine($$""" - /// - public static bool TryParse(string s, IFormatProvider provider, out {{symbolName}} identifier) - { - if ({{declaredValueType}}.TryParse(s, provider, out var value)) - return TryCreate(value).IsSuccess(out identifier); - - identifier = default; - return false; - } + if (supportsProviderBasedTryParse) + { + source.AppendLine($$""" + /// + public static bool TryParse(string s, IFormatProvider provider, out {{symbolName}} identifier) + { + if ({{declaredValueType}}.TryParse(s, provider, out var value)) + return TryCreate(value).IsSuccess(out identifier); - """ - ); + identifier = default; + return false; + } + + """ + ); + } } if (isStruct) @@ -743,7 +760,8 @@ private readonly record struct Identifier( bool IsStruct, bool IsPublic, string DeclaredValueType, - string FullValueType + string FullValueType, + bool SupportsProviderBasedTryParse ) { public EquatableImmutableArray ContainingDeclarations { get; } = ContainingDeclarations; @@ -752,5 +770,6 @@ string FullValueType public bool IsPublic { get; } = IsPublic; public string DeclaredValueType { get; } = DeclaredValueType; public string FullValueType { get; } = FullValueType; + public bool SupportsProviderBasedTryParse { get; } = SupportsProviderBasedTryParse; } } diff --git a/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt b/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt index 70ca29e..2c2e937 100644 --- a/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt +++ b/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt @@ -90,16 +90,6 @@ public readonly partial struct TestGuidId : return false; } - /// - public static bool TryParse(string s, IFormatProvider provider, out TestGuidId identifier) - { - if (Guid.TryParse(s, provider, out var value)) - return TryCreate(value).IsSuccess(out identifier); - - identifier = default; - return false; - } - /// public bool Equals(TestGuidId other) { diff --git a/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt b/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt index 5cda603..95c6017 100644 --- a/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt +++ b/tests/LightObjects.Generated.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt @@ -88,16 +88,6 @@ public readonly partial struct TestGuidId : return false; } - /// - public static bool TryParse(string s, IFormatProvider provider, out TestGuidId identifier) - { - if (Guid.TryParse(s, provider, out var value)) - return TryCreate(value).IsSuccess(out identifier); - - identifier = default; - return false; - } - /// public bool Equals(TestGuidId other) {