Skip to content

Commit 16ca4d8

Browse files
Refactor to use Outcome
1 parent 8318a16 commit 16ca4d8

File tree

5 files changed

+263
-132
lines changed

5 files changed

+263
-132
lines changed

.editorconfig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ dotnet_analyzer_diagnostic.category-Globalization.severity = error
1212
dotnet_analyzer_diagnostic.category-Documentation.severity = error
1313
dotnet_analyzer_diagnostic.category-Readability.severity = error
1414
dotnet_analyzer_diagnostic.category-Ordering.severity = error
15-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.OrderingRules.severity = none
1615

1716
# Nullability
1817
# CS8602: Dereference of a possibly null reference.
@@ -41,7 +40,6 @@ dotnet_diagnostic.CA2000.severity = error
4140
dotnet_diagnostic.CA2201.severity = error
4241
dotnet_diagnostic.CS1591.severity = error
4342
dotnet_diagnostic.IDE0022.severity = error
44-
dotnet_diagnostic.CA1054.severity = error
4543
dotnet_diagnostic.CS8600.severity = error
4644
dotnet_diagnostic.CS8601.severity = error
4745
dotnet_diagnostic.CS8603.severity = error
@@ -373,9 +371,15 @@ dotnet_diagnostic.SA1400.severity = none
373371
dotnet_diagnostic.SA1114.severity = none
374372
dotnet_diagnostic.SA1118.severity = none
375373
dotnet_diagnostic.SA1649.severity = none
374+
dotnet_diagnostic.CA1054.severity = none
376375

376+
dotnet_diagnostic.SA1200.severity = none
377377

378378

379+
# TODO: these are nice. Put back
380+
dotnet_diagnostic.SA1201.severity = none
381+
dotnet_diagnostic.SA1202.severity = none
382+
dotnet_diagnostic.SA1204.severity = none
379383

380384

381385

RestClient.Net.OpenApiGenerator.Cli/Program.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
Outcome.HttpError<string>
77
>;
88
using ExceptionErrorString = Outcome.HttpError<string>.ExceptionError;
9+
using GeneratorError = Outcome.Result<
10+
RestClient.Net.OpenApiGenerator.GeneratorResult,
11+
string
12+
>.Error<RestClient.Net.OpenApiGenerator.GeneratorResult, string>;
13+
using GeneratorOk = Outcome.Result<RestClient.Net.OpenApiGenerator.GeneratorResult, string>.Ok<
14+
RestClient.Net.OpenApiGenerator.GeneratorResult,
15+
string
16+
>;
917
using OkString = Outcome.Result<string, Outcome.HttpError<string>>.Ok<
1018
string,
1119
Outcome.HttpError<string>
@@ -186,29 +194,34 @@ await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false),
186194
Console.WriteLine($"Downloaded {openApiSpec.Length} characters\n");
187195
Console.WriteLine("Generating C# code from OpenAPI spec...");
188196

189-
var generatorResult = OpenApiCodeGenerator.Generate(
197+
var result = OpenApiCodeGenerator.Generate(
190198
openApiSpec,
191199
@namespace: config.Namespace,
192200
className: config.ClassName,
193201
outputPath: config.OutputPath,
194202
baseUrlOverride: config.BaseUrl
195203
);
196204

197-
Console.WriteLine(
198-
$"Generated {generatorResult.ExtensionMethodsCode.Length} "
199-
+ "characters of extension methods"
200-
);
201-
Console.WriteLine($"Generated {generatorResult.ModelsCode.Length} " + "characters of models");
202-
203-
if (generatorResult.ExtensionMethodsCode.StartsWith("//", StringComparison.Ordinal))
205+
#pragma warning disable IDE0010
206+
switch (result)
207+
#pragma warning restore IDE0010
204208
{
205-
Console.WriteLine("\nError in generated code:");
206-
Console.WriteLine(generatorResult.ExtensionMethodsCode);
207-
return;
209+
case GeneratorOk(var generatorResult):
210+
Console.WriteLine(
211+
$"Generated {generatorResult.ExtensionMethodsCode.Length} "
212+
+ "characters of extension methods"
213+
);
214+
Console.WriteLine(
215+
$"Generated {generatorResult.ModelsCode.Length} " + "characters of models"
216+
);
217+
Console.WriteLine($"\nSaved files to: {config.OutputPath}");
218+
Console.WriteLine("\nGeneration completed successfully!");
219+
break;
220+
case GeneratorError(var error):
221+
Console.WriteLine("\nCode generation failed:");
222+
Console.WriteLine(error);
223+
break;
208224
}
209-
210-
Console.WriteLine($"\nSaved files to: {config.OutputPath}");
211-
Console.WriteLine("\nGeneration completed successfully!");
212225
}
213226

214227
static string HandleDownloadError(string message)

0 commit comments

Comments
 (0)