Skip to content

Commit

Permalink
Removed static things.
Browse files Browse the repository at this point in the history
  • Loading branch information
fuszenecker committed Oct 12, 2023
1 parent 492bf4d commit a37c742
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 58 deletions.
4 changes: 0 additions & 4 deletions .editorconfig

This file was deleted.

7 changes: 0 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"dotnet.defaultSolution": "Promptuarium.sln",
"omnisharp.enableEditorConfigSupport": true,
"cSpell.words": [
"datetime",
"LINQ",
"Promptuarium",
"roslynator"
]
}
5 changes: 3 additions & 2 deletions src/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,12 @@ public static async Task<Stream> FromElementAsync(Element value)
/// var tree = await Data.AsTreeAsync(stream);
/// </code>
/// </example>
public static async Task<Element> AsElementAsync(this Stream stream)
public static async Task<Element> AsElementAsync(this Stream stream, Element loaderElement = default)

Check warning on line 893 in src/Data.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 893 in src/Data.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
loaderElement ??= new Element();
Element result;
stream.Position = 0;
result = await Element.LoadAsync(stream).ConfigureAwait(false);
result = await loaderElement.LoadAsync(stream).ConfigureAwait(false);
stream.Position = 0;
return result;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ElementEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public partial class Element
/// <summary>
/// Event handler for all Promptuarium data loading events
/// </summary>
public static event EventHandler<PromptuariumLoadingEventArgs>? OnDataLoading;
public event EventHandler<PromptuariumLoadingEventArgs>? OnDataLoading;

/// <summary>
/// Event handler for all Promptuarium data loaded events
/// </summary>
public static event EventHandler<PromptuariumLoadedEventArgs>? OnDataLoaded;
public event EventHandler<PromptuariumLoadedEventArgs>? OnDataLoaded;

/// <summary>
/// Event handler for all Promptuarium data saving events
Expand All @@ -89,12 +89,12 @@ public partial class Element
/// <summary>
/// Event handler for all Promptuarium metadata loading events
/// </summary>
public static event EventHandler<PromptuariumLoadingEventArgs>? OnMetaDataLoading;
public event EventHandler<PromptuariumLoadingEventArgs>? OnMetaDataLoading;

/// <summary>
/// Event handler for all Promptuarium metadata loaded events
/// </summary>
public static event EventHandler<PromptuariumLoadedEventArgs>? OnMetaDataLoaded;
public event EventHandler<PromptuariumLoadedEventArgs>? OnMetaDataLoaded;

/// <summary>
/// Event handler for all Promptuarium metadata saving events
Expand Down
8 changes: 4 additions & 4 deletions src/ElementIo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class Element
/// var tree = await Element.LoadAsync(stream, cancellationToken);
/// </code>
/// </example>
public static Task<Element> LoadAsync(Stream stream, CancellationToken cancellationToken)
public Task<Element> LoadAsync(Stream stream, CancellationToken cancellationToken)
{
return DeserializeAsync(stream, cancellationToken);
}
Expand All @@ -38,7 +38,7 @@ public static Task<Element> LoadAsync(Stream stream, CancellationToken cancellat
/// var tree = await Element.LoadAsync(stream);
/// </code>
/// </example>
public static Task<Element> LoadAsync(Stream stream)
public Task<Element> LoadAsync(Stream stream)
{
return LoadAsync(stream, CancellationToken.None);
}
Expand All @@ -55,7 +55,7 @@ public static Task<Element> LoadAsync(Stream stream)
/// var tree = await Element.LoadAsync("test.prm", cancellationToken);
/// </code>
/// </example>
public static async Task<Element> LoadAsync(string fileName, CancellationToken cancellationToken)
public async Task<Element> LoadAsync(string fileName, CancellationToken cancellationToken)
{
using var fileStream = new FileStream(fileName, FileMode.Open);
return await LoadAsync(fileStream, cancellationToken).ConfigureAwait(false);
Expand All @@ -72,7 +72,7 @@ public static async Task<Element> LoadAsync(string fileName, CancellationToken c
/// var tree = await Element.LoadAsync("test.prm");
/// </code>
/// </example>
public static Task<Element> LoadAsync(string fileName)
public Task<Element> LoadAsync(string fileName)
{
return LoadAsync(fileName, CancellationToken.None);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ElementSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static bool Contains(Stream? stream)

#region Deserialization routines

private static async Task<Element> DeserializeAsync(Stream stream, CancellationToken cancellationToken)
private async Task<Element> DeserializeAsync(Stream stream, CancellationToken cancellationToken)
{
var root = new Element();
Element parent = root;
Expand Down
18 changes: 4 additions & 14 deletions src/ElementTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,24 +534,14 @@ public async Task<string> ToBase64StringAsync(CancellationToken cancellationToke
/// var tree = await Element.FromBase64StringAsync(base64String);
/// </code>
/// </example>
public static async Task<Element> FromBase64StringAsync(string base64String, CancellationToken cancellationToken)
public static async Task<Element> FromBase64StringAsync(string base64String,
Element loaderElement = default, CancellationToken cancellationToken = default)

Check warning on line 538 in src/ElementTree.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 538 in src/ElementTree.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
loaderElement ??= new Element();
using var memoryStream = new MemoryStream(Convert.FromBase64String(base64String));
return await LoadAsync(memoryStream, cancellationToken).ConfigureAwait(false);
return await loaderElement.LoadAsync(memoryStream, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Creates a tree from a Base64 string.
/// </summary>
/// <param name="base64String">The tree in Base64.</param>
/// <returns>The tree.</returns>
/// <example>
/// <code>
/// var tree = await Element.FromBase64StringAsync(base64String);
/// </code>
/// </example>
public static Task<Element> FromBase64StringAsync(string base64String) => FromBase64StringAsync(base64String, CancellationToken.None);

#endregion

#region IEnumerable<Element> implementation
Expand Down
2 changes: 1 addition & 1 deletion tests/StabilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private async Task CreateAndVerifyTrees(int maxTests, int minChildren, int maxCh

Assert.IsTrue(new FileInfo(fileName).Length > 0);

Element currentTree = await Element.LoadAsync(fileName).ConfigureAwait(false);
Element currentTree = await new Element().LoadAsync(fileName).ConfigureAwait(false);
string currentTreeString = currentTree.TreeToString();

Assert.AreEqual(originalTreeString, currentTreeString);
Expand Down
43 changes: 22 additions & 21 deletions tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public async Task Load()

using var stream = new MemoryStream(data);

var tree = await Element.LoadAsync(stream).ConfigureAwait(false);
var tree = await new Element().LoadAsync(stream).ConfigureAwait(false);

Assert.AreEqual((byte)0x80, tree.Data?.AsByte());
Assert.AreEqual((short)0x7819, tree[0].Data?.AsShort());
Expand All @@ -310,7 +310,7 @@ public async Task TreeToStringTest()
var tree = CreateTestTree();
await tree.SaveAsync(TestFileName).ConfigureAwait(false);

var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

string treeString = tree.TreeToString();
string treeString2 = tree2.TreeToString();
Expand All @@ -331,7 +331,7 @@ public async Task FromInt()
tree.Add(subNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(0x55aa00ff, tree[tree.Children.Count - 1].Data?.AsInt());
Assert.AreEqual(0x55aa00ff, tree2[tree2.Children.Count - 1].Data?.AsInt());
Expand All @@ -343,7 +343,7 @@ public async Task TreeToString()
var tree = CreateTestTree();

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

string treeString = tree.TreeToString();
string treeString2 = tree2.TreeToString();
Expand All @@ -364,7 +364,7 @@ public async Task Long()
tree[0].Add(numericNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(0x55aa00ff19781986, tree2[0][1].Data?.AsLong());
}
Expand All @@ -382,7 +382,7 @@ public async Task Double()
tree[0].Add(numericNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

double? value = tree2[0][1].MetaData?.AsDouble();
Assert.IsTrue(value > 3.1415926 && value < 3.1415927);
Expand All @@ -403,7 +403,7 @@ public async Task GuidTest()
tree.Add(guidNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(0, testGuid.CompareTo(tree2[0].Data?.AsGuid()));
}
Expand All @@ -421,7 +421,7 @@ public async Task Decimal()
tree.Add(decimalNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(1.978M, tree2[0].MetaData?.AsDecimal());
}
Expand All @@ -441,7 +441,7 @@ public async Task ByteArray()
tree.Add(byteArrayNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

byte[]? testBytes2 = tree2[0].Data?.AsByteArray();

Expand All @@ -465,7 +465,7 @@ public async Task Unicode()
tree.Add(unicodeNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual("Hello, UTF-32LE world!", tree2[0].Data?.AsUtf32String());
Assert.AreEqual("Hello, UTF-16LE world!", tree2[0].MetaData?.AsUtf16String());
Expand All @@ -485,7 +485,7 @@ public async Task ElementTest()

await outerTree.SaveAsync(TestFileName).ConfigureAwait(false);

var outerTree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var outerTree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);
var innerTree2 = await outerTree2.Data!.AsElementAsync().ConfigureAwait(false);

Assert.AreEqual((short)1000, outerTree2.MetaData?.AsShort());
Expand All @@ -507,7 +507,7 @@ public async Task DateTimeTimeSpan()
};

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(0, birthDay.CompareTo(tree2[0].Data?.AsDateTime()));
Assert.AreEqual(0, testTimeSpan.CompareTo(tree2[0].MetaData?.AsTimeSpan()));
Expand All @@ -531,7 +531,7 @@ public async Task DateTimeOffset()
};

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(0, birthDay.CompareTo(tree2[0].Data?.AsDateTimeOffset() ?? throw new ArgumentNullException("Test failed.")));

Expand All @@ -554,7 +554,7 @@ public async Task Char()
tree.Add(unicodeNode);

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual('Ä', tree2[0].Data?.AsChar());
Assert.AreEqual('Ő', tree2[0].MetaData?.AsChar());
Expand All @@ -580,7 +580,7 @@ public async Task StringPerformance()
}

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(tree.TreeToString(), tree2.TreeToString());
}
Expand All @@ -601,7 +601,7 @@ public async Task IntPerformance()
}

await tree.SaveAsync(TestFileName).ConfigureAwait(false);
var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await new Element().LoadAsync(TestFileName).ConfigureAwait(false);
Assert.AreEqual(tree.TreeToString(), tree2.TreeToString());
}

Expand Down Expand Up @@ -629,8 +629,8 @@ public async Task StreamCompatibility()

stream.Position = 0;

var tree2A = await Element.LoadAsync(stream).ConfigureAwait(false);
var tree2B = await Element.LoadAsync(stream).ConfigureAwait(false);
var tree2A = await new Element().LoadAsync(stream).ConfigureAwait(false);
var tree2B = await new Element().LoadAsync(stream).ConfigureAwait(false);

string sTreeA = treeA.TreeToString();
string sTreeB = treeB.TreeToString();
Expand Down Expand Up @@ -810,10 +810,11 @@ public async Task Events()
var loadingNodes = new List<string>();
var loadedNodes = new List<string>();

Element.OnDataLoading += (s, _) => loadingNodes.Add(((Element)s!).Data!.AsUtf8String());
Element.OnDataLoaded += (s, _) => loadedNodes.Add(((Element)s!).Data!.AsUtf8String());
var loaderElement = new Element();
loaderElement.OnDataLoading += (s, _) => loadingNodes.Add(((Element)s!).Data!.AsUtf8String());
loaderElement.OnDataLoaded += (s, _) => loadedNodes.Add(((Element)s!).Data!.AsUtf8String());

var tree2 = await Element.LoadAsync(TestFileName).ConfigureAwait(false);
var tree2 = await loaderElement.LoadAsync(TestFileName).ConfigureAwait(false);

Assert.AreEqual(1, savingEventFired);
Assert.AreEqual(1, savedEventFired);
Expand Down

0 comments on commit a37c742

Please sign in to comment.