|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.InteropServices; |
4 | 6 | using System.Text;
|
5 | 7 |
|
6 | 8 | namespace Strongly;
|
@@ -85,7 +87,7 @@ static string CreateStrongValue(
|
85 | 87 | var implementations = ctx.Config.Implementations;
|
86 | 88 | var useParsable = implementations.IsSet(StronglyImplementations.Parsable);
|
87 | 89 | var useIFormattable = implementations.IsSet(StronglyImplementations.IFormattable);
|
88 |
| - const bool useIParsable = false; |
| 90 | + var useIParsable = implementations.IsSet(StronglyImplementations.Parsable); |
89 | 91 | var useIEquatable =
|
90 | 92 | !ctx.IsRecord && implementations.IsSet(StronglyImplementations.IEquatable);
|
91 | 93 | var useIComparable =
|
@@ -128,20 +130,23 @@ static string CreateStrongValue(
|
128 | 130 |
|
129 | 131 | var hasCtor = ctx.Constructors.Any(c => c.ArgumentType == resources.InternalType);
|
130 | 132 | var baseDef = EmbeddedSources.BaseTypeDef.Value + resources.Base.Value;
|
131 |
| - baseDef = baseDef.Replace("[CTOR]", hasCtor ? string.Empty : EmbeddedSources.Ctor.Value); |
132 |
| - |
133 | 133 | if (ctx.IsRecord)
|
134 | 134 | {
|
135 |
| - var ctor = baseDef.Split('\n') |
136 |
| - .First(x => x.Trim().StartsWith("public TYPENAME(")) |
137 |
| - .Trim().Split('(', ')')[1]; |
138 |
| - sb.Append($"readonly partial record struct TYPENAME({ctor}): INTERFACES {{ \n "); |
| 135 | + var ctorIndex = |
| 136 | + baseDef.IndexOf(EmbeddedSources.CtorKey, StringComparison.InvariantCulture); |
| 137 | + |
| 138 | + baseDef = baseDef |
| 139 | + .Substring(0, ctorIndex + EmbeddedSources.CtorKey.Length) |
| 140 | + .Replace("readonly partial struct", "readonly partial record struct") |
| 141 | + + Environment.NewLine; |
139 | 142 | }
|
140 |
| - else |
141 |
| - sb.Append(baseDef); |
142 | 143 |
|
143 |
| - ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable, useIFormattable); |
| 144 | + baseDef = baseDef.Replace(EmbeddedSources.CtorKey, |
| 145 | + hasCtor ? string.Empty : EmbeddedSources.Ctor.Value); |
144 | 146 |
|
| 147 | + sb.Append(baseDef); |
| 148 | + |
| 149 | + ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable, useIFormattable); |
145 | 150 |
|
146 | 151 | if (useIComparable) sb.AppendLine(resources.Comparable.Value);
|
147 | 152 | if (useIFormattable) sb.AppendLine(resources.Formattable.Value);
|
@@ -186,8 +191,8 @@ ctx.Config.Math is not (StronglyMath.None or StronglyMath.Default))
|
186 | 191 | ? toStr
|
187 | 192 | : EmbeddedSources.DefaultToString);
|
188 | 193 |
|
189 |
| - sb.Replace(EmbeddedSources.CtorKey, |
190 |
| - resources.TemplateVars.TryGetValue(EmbeddedSources.CtorKey, out var ctorInit) |
| 194 | + sb.Replace(EmbeddedSources.CtorValueKey, |
| 195 | + resources.TemplateVars.TryGetValue(EmbeddedSources.CtorValueKey, out var ctorInit) |
191 | 196 | ? ctorInit
|
192 | 197 | : EmbeddedSources.DefaultCtor);
|
193 | 198 |
|
@@ -219,9 +224,16 @@ bool useIFormattable
|
219 | 224 | var interfaces = new List<string>();
|
220 | 225 | if (useIComparable) interfaces.Add("System.IComparable<TYPENAME>");
|
221 | 226 | if (useIEquatable) interfaces.Add("System.IEquatable<TYPENAME>");
|
222 |
| - if (useIParseable) interfaces.Add("System.IParsable<TYPENAME>"); |
223 | 227 | if (useIFormattable) interfaces.Add("System.IFormattable");
|
224 | 228 |
|
| 229 | + var interfacesNet7 = new List<string>(); |
| 230 | + if (useIParseable) interfacesNet7.Add("System.IParsable<TYPENAME>"); |
| 231 | + |
| 232 | + if (interfaces.Count > 0 || interfacesNet7.Count > 0) |
| 233 | + sb.Replace("INTERFACES_NET7", string.Join(", ", interfaces.Concat(interfacesNet7))); |
| 234 | + else |
| 235 | + sb.Replace(": INTERFACES_NET7", string.Empty); |
| 236 | + |
225 | 237 | if (interfaces.Count > 0)
|
226 | 238 | sb.Replace("INTERFACES", string.Join(", ", interfaces));
|
227 | 239 | else
|
|
0 commit comments