refactor: use some better syntax

This commit is contained in:
Anna 2020-09-07 00:08:16 -04:00
parent fff3ce820b
commit 52ec386801
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
2 changed files with 19 additions and 31 deletions

View File

@ -36,7 +36,7 @@ namespace NoSoliciting {
Definitions defs = null; Definitions defs = null;
var download = await Download().ConfigureAwait(true); Tuple<Definitions, string> download = await Download().ConfigureAwait(true);
if (download != null) { if (download != null) {
defs = download.Item1; defs = download.Item1;
@ -52,7 +52,7 @@ namespace NoSoliciting {
} }
public static Definitions Load(string text) { public static Definitions Load(string text) {
var de = new DeserializerBuilder() IDeserializer de = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) .WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeConverter(new MatcherConverter()) .WithTypeConverter(new MatcherConverter())
.IgnoreUnmatchedProperties() .IgnoreUnmatchedProperties()
@ -82,7 +82,7 @@ namespace NoSoliciting {
} }
string text; string text;
using (var file = File.OpenText(cachedPath)) { using (StreamReader file = File.OpenText(cachedPath)) {
text = await file.ReadToEndAsync().ConfigureAwait(true); text = await file.ReadToEndAsync().ConfigureAwait(true);
} }
@ -102,11 +102,10 @@ namespace NoSoliciting {
private static async Task<Tuple<Definitions, string>> Download() { private static async Task<Tuple<Definitions, string>> Download() {
try { try {
using (WebClient client = new WebClient()) { using WebClient client = new WebClient();
string text = await client.DownloadStringTaskAsync(URL).ConfigureAwait(true); string text = await client.DownloadStringTaskAsync(URL).ConfigureAwait(true);
LastError = null; LastError = null;
return Tuple.Create(Load(text), text); return Tuple.Create(Load(text), text);
}
} catch (Exception e) when (e is WebException || e is YamlException) { } catch (Exception e) when (e is WebException || e is YamlException) {
PluginLog.Log($"Could not download newest definitions."); PluginLog.Log($"Could not download newest definitions.");
PluginLog.Log(e.ToString()); PluginLog.Log(e.ToString());
@ -122,9 +121,8 @@ namespace NoSoliciting {
byte[] b = Encoding.UTF8.GetBytes(defs); byte[] b = Encoding.UTF8.GetBytes(defs);
using (var file = File.OpenWrite(cachePath)) { using FileStream file = File.OpenWrite(cachePath);
await file.WriteAsync(b, 0, b.Length).ConfigureAwait(true); await file.WriteAsync(b, 0, b.Length).ConfigureAwait(true);
}
} }
internal void Initialise(Plugin plugin) { internal void Initialise(Plugin plugin) {
@ -221,7 +219,7 @@ namespace NoSoliciting {
// calculate likelihood // calculate likelihood
int likelihood = 0; int likelihood = 0;
foreach (var matchers in this.LikelyMatchers) { foreach (List<Matcher> matchers in this.LikelyMatchers) {
if (matchers.Any(matcher => matcher.Matches(text))) { if (matchers.Any(matcher => matcher.Matches(text))) {
likelihood += 1; likelihood += 1;
} }

View File

@ -256,21 +256,12 @@ namespace NoSoliciting {
ImGui.Text("Click on one of the entries below to report it to the developer as miscategorised."); ImGui.Text("Click on one of the entries below to report it to the developer as miscategorised.");
if (this.lastReportStatus != ReportStatus.None) { if (this.lastReportStatus != ReportStatus.None) {
string status; string status = this.lastReportStatus switch {
switch (this.lastReportStatus) { ReportStatus.Failure => "failed to send",
case ReportStatus.Failure: ReportStatus.Successful => "sent successfully",
status = "failed to send"; ReportStatus.InProgress => "sending",
break; _ => "unknown",
case ReportStatus.Successful: };
status = "sent successfully";
break;
case ReportStatus.InProgress:
status = "sending";
break;
default:
status = "unknown";
break;
}
ImGui.Text($"Last report status: {status}"); ImGui.Text($"Last report status: {status}");
} }
@ -394,10 +385,9 @@ namespace NoSoliciting {
Task.Run(async () => { Task.Run(async () => {
string resp = null; string resp = null;
try { try {
using (WebClient client = new WebClient()) { using WebClient client = new WebClient();
this.lastReportStatus = ReportStatus.InProgress; this.lastReportStatus = ReportStatus.InProgress;
resp = await client.UploadStringTaskAsync(this.plugin.Definitions.ReportUrl, message.ToJson()).ConfigureAwait(true); resp = await client.UploadStringTaskAsync(this.plugin.Definitions.ReportUrl, message.ToJson()).ConfigureAwait(true);
}
#pragma warning disable CA1031 // Do not catch general exception types #pragma warning disable CA1031 // Do not catch general exception types
} catch (Exception) { } } catch (Exception) { }
#pragma warning restore CA1031 // Do not catch general exception types #pragma warning restore CA1031 // Do not catch general exception types