tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
SemverType.cs
Go to the documentation of this file.
1using System;
2
3using HotChocolate.Language;
4using HotChocolate.Types;
6
8{
12 public sealed class SemverType : ScalarType<Version, StringValueNode>
13 {
17 public SemverType()
18 : base("Semver")
19 {
20 Description = "Represents a version in semantic versioning format";
21 SpecifiedBy = new Uri("https://semver.org/spec/v2.0.0.html");
22 }
23
25 public override IValueNode ParseResult(object? resultValue)
26 => ParseValue(resultValue);
27
29 public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
30 {
31 if (resultValue is not string resultString)
32 {
33 runtimeValue = null;
34 return false;
35 }
36
37 var result = Version.TryParse(resultString, out var resultVersion);
38 runtimeValue = resultVersion;
39 return result;
40 }
41
43 public override bool TrySerialize(object? runtimeValue, out object? resultValue)
44 {
45 if (runtimeValue is not Version runtimeVersion)
46 {
47 resultValue = null;
48 return false;
49 }
50
51 resultValue = runtimeVersion.Semver().ToString();
52 return true;
53 }
54
56 protected override Version ParseLiteral(StringValueNode valueSyntax)
57 {
58 ArgumentNullException.ThrowIfNull(valueSyntax);
59 return Version.Parse(valueSyntax.Value);
60 }
61
63 protected override StringValueNode ParseValue(Version runtimeValue)
64 => new(runtimeValue.Semver().ToString());
65
67 protected override bool IsInstanceOfType(StringValueNode valueSyntax)
68 {
69 ArgumentNullException.ThrowIfNull(valueSyntax);
70 return Version.TryParse(valueSyntax.Value, out var parsedVersion)
71 && IsInstanceOfType(parsedVersion);
72 }
73
75 protected override bool IsInstanceOfType(Version runtimeValue)
76 {
77 ArgumentNullException.ThrowIfNull(runtimeValue);
78 return runtimeValue.Build != -1 && runtimeValue.Revision == -1;
79 }
80 }
81}
A ScalarType<TRuntimeType, TLiteral> for semantic Versions.
Definition SemverType.cs:13
override bool TryDeserialize(object? resultValue, out object? runtimeValue)
Definition SemverType.cs:29
override IValueNode ParseResult(object? resultValue)
override bool IsInstanceOfType(StringValueNode valueSyntax)
Definition SemverType.cs:67
override Version ParseLiteral(StringValueNode valueSyntax)
Definition SemverType.cs:56
SemverType()
Initializes a new instance of the SemverType class.
Definition SemverType.cs:17
override bool IsInstanceOfType(Version runtimeValue)
Definition SemverType.cs:75
override StringValueNode ParseValue(Version runtimeValue)
override bool TrySerialize(object? runtimeValue, out object? resultValue)
Definition SemverType.cs:43