From 52ec386801502f62123143a795ab1542bf5131d3 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Mon, 7 Sep 2020 00:08:16 -0400 Subject: [PATCH] refactor: use some better syntax --- NoSoliciting/Definitions.cs | 22 ++++++++++------------ NoSoliciting/PluginUI.cs | 28 +++++++++------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/NoSoliciting/Definitions.cs b/NoSoliciting/Definitions.cs index d32d85d..7f4e4b6 100644 --- a/NoSoliciting/Definitions.cs +++ b/NoSoliciting/Definitions.cs @@ -36,7 +36,7 @@ namespace NoSoliciting { Definitions defs = null; - var download = await Download().ConfigureAwait(true); + Tuple download = await Download().ConfigureAwait(true); if (download != null) { defs = download.Item1; @@ -52,7 +52,7 @@ namespace NoSoliciting { } public static Definitions Load(string text) { - var de = new DeserializerBuilder() + IDeserializer de = new DeserializerBuilder() .WithNamingConvention(UnderscoredNamingConvention.Instance) .WithTypeConverter(new MatcherConverter()) .IgnoreUnmatchedProperties() @@ -82,7 +82,7 @@ namespace NoSoliciting { } string text; - using (var file = File.OpenText(cachedPath)) { + using (StreamReader file = File.OpenText(cachedPath)) { text = await file.ReadToEndAsync().ConfigureAwait(true); } @@ -102,11 +102,10 @@ namespace NoSoliciting { private static async Task> Download() { try { - using (WebClient client = new WebClient()) { - string text = await client.DownloadStringTaskAsync(URL).ConfigureAwait(true); - LastError = null; - return Tuple.Create(Load(text), text); - } + using WebClient client = new WebClient(); + string text = await client.DownloadStringTaskAsync(URL).ConfigureAwait(true); + LastError = null; + return Tuple.Create(Load(text), text); } catch (Exception e) when (e is WebException || e is YamlException) { PluginLog.Log($"Could not download newest definitions."); PluginLog.Log(e.ToString()); @@ -122,9 +121,8 @@ namespace NoSoliciting { byte[] b = Encoding.UTF8.GetBytes(defs); - using (var file = File.OpenWrite(cachePath)) { - await file.WriteAsync(b, 0, b.Length).ConfigureAwait(true); - } + using FileStream file = File.OpenWrite(cachePath); + await file.WriteAsync(b, 0, b.Length).ConfigureAwait(true); } internal void Initialise(Plugin plugin) { @@ -221,7 +219,7 @@ namespace NoSoliciting { // calculate likelihood int likelihood = 0; - foreach (var matchers in this.LikelyMatchers) { + foreach (List matchers in this.LikelyMatchers) { if (matchers.Any(matcher => matcher.Matches(text))) { likelihood += 1; } diff --git a/NoSoliciting/PluginUI.cs b/NoSoliciting/PluginUI.cs index b2e4b0e..01f0862 100644 --- a/NoSoliciting/PluginUI.cs +++ b/NoSoliciting/PluginUI.cs @@ -256,21 +256,12 @@ namespace NoSoliciting { ImGui.Text("Click on one of the entries below to report it to the developer as miscategorised."); if (this.lastReportStatus != ReportStatus.None) { - string status; - switch (this.lastReportStatus) { - case ReportStatus.Failure: - status = "failed to send"; - break; - case ReportStatus.Successful: - status = "sent successfully"; - break; - case ReportStatus.InProgress: - status = "sending"; - break; - default: - status = "unknown"; - break; - } + string status = this.lastReportStatus switch { + ReportStatus.Failure => "failed to send", + ReportStatus.Successful => "sent successfully", + ReportStatus.InProgress => "sending", + _ => "unknown", + }; ImGui.Text($"Last report status: {status}"); } @@ -394,10 +385,9 @@ namespace NoSoliciting { Task.Run(async () => { string resp = null; try { - using (WebClient client = new WebClient()) { - this.lastReportStatus = ReportStatus.InProgress; - resp = await client.UploadStringTaskAsync(this.plugin.Definitions.ReportUrl, message.ToJson()).ConfigureAwait(true); - } + using WebClient client = new WebClient(); + this.lastReportStatus = ReportStatus.InProgress; + resp = await client.UploadStringTaskAsync(this.plugin.Definitions.ReportUrl, message.ToJson()).ConfigureAwait(true); #pragma warning disable CA1031 // Do not catch general exception types } catch (Exception) { } #pragma warning restore CA1031 // Do not catch general exception types