Skip to content

Commit 23bf7b7

Browse files
authored
Minor updates (#6)
* Minor updates * Doc comment updates/clarifications * Moved GetOrdering to an extension method for IFormatProvider. * Renamed test namespace for consistency * minor comment add to re-trigger actions (hopefully) --------- Co-authored-by: smaillet <25911635+smaillet@users.noreply.github.com>
1 parent 722418b commit 23bf7b7

7 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/Ubiquity.NET.Versioning/AlphaNumericOrdering.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Ubiquity.NET.Versioning
99
/// <summary>Identifies the sort ordering expected for a given version</summary>
1010
public enum AlphaNumericOrdering
1111
{
12-
/// <summary>Indicates no sort ordering</summary>
12+
/// <summary>Indicates an invalid sort ordering</summary>
1313
/// <remarks>
1414
/// This value is the default for this type and is ALWAYS invalid.
1515
/// </remarks>

src/Ubiquity.NET.Versioning/AppContextSwitches.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static bool CSemVerCIOnlySupportsBuildMetaOnZeroTimedVersions
6262
set => AppContext.SetSwitch(CSemVerCIOnlySupportsBuildMetaOnZeroTimedVersionsName, value);
6363
}
6464

65+
// internal utility to read a switch and default to false if not found.
6566
private static bool GetSwitchValue(string name)
6667
{
6768
bool found = AppContext.TryGetSwitch(name, out bool currentVal);

src/Ubiquity.NET.Versioning/CSemVer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public static CSemVer From( ParsedBuildVersionXml parsedBuildVersionXml, Immutab
262262
/// <inheritdoc/>
263263
public static new CSemVer Parse( string s, IFormatProvider? provider )
264264
{
265+
// Throw if provider isn't one that is ignorable.
265266
provider.ThrowIfCaseSensitive();
266267
return TryParse( s, out CSemVer? retVal, out Exception? ex ) ? retVal : throw ex;
267268
}
@@ -272,6 +273,7 @@ public static CSemVer From( ParsedBuildVersionXml parsedBuildVersionXml, Immutab
272273
/// </remarks>
273274
public static bool TryParse( [NotNullWhen( true )] string? s, IFormatProvider? provider, [MaybeNullWhen( false )] out CSemVer result )
274275
{
276+
// Throw if provider isn't one that is ignorable.
275277
provider.ThrowIfCaseSensitive();
276278
return TryParse( s, out result, out _ );
277279
}

src/Ubiquity.NET.Versioning/CSemVerCI.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,15 @@ public static bool TryFrom(
214214
/// <inheritdoc/>
215215
public static new CSemVerCI Parse( string s, IFormatProvider? provider )
216216
{
217+
// Throw if provider isn't one that is ignorable.
217218
provider.ThrowIfCaseSensitive();
218219
return TryParse( s, out CSemVerCI? retVal, out Exception? ex ) ? retVal : throw ex;
219220
}
220221

221222
/// <inheritdoc/>
222223
public static bool TryParse( [NotNullWhen( true )] string? s, IFormatProvider? provider, [MaybeNullWhen( false )] out CSemVerCI result )
223224
{
225+
// Throw if provider isn't one that is ignorable.
224226
provider.ThrowIfCaseSensitive();
225227
return TryParse( s, out result, out _ );
226228
}

src/Ubiquity.NET.Versioning/FormatProviderExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,12 @@ public static void ThrowIfCaseSensitive(this IFormatProvider? provider, [CallerA
2525
throw new ArgumentException("Format provider must be <null> or provide a CaseInsensitive comparison", exp);
2626
}
2727
}
28+
29+
public static AlphaNumericOrdering GetOrdering(this IFormatProvider provider)
30+
{
31+
ArgumentNullException.ThrowIfNull(provider);
32+
33+
return (AlphaNumericOrdering?)provider.GetFormat(typeof(AlphaNumericOrdering)) ?? AlphaNumericOrdering.None;
34+
}
2835
}
2936
}

src/Ubiquity.NET.Versioning/PackageReadMe.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@ sort ordering of the versions according to the rules of SemVer (Which, CSemVer a
4343
follow with the exception of explicit case insensitivity for AphaNumeric IDs)
4444

4545
>[!WARNING]
46-
> The formal 'spec' for [CSemVer](https://csemver.org) remains mostly silent on the point of
47-
> the short format. See this [known issue](https://github.com/CK-Build/csemver.org/issues/2).
46+
> The formal 'spec' for [CSemVer](https://csemver.org) remains mostly silent on the point
47+
> of the short format. See this [known issue](https://github.com/CK-Build/csemver.org/issues/2).
4848
> Since, the existence of that form was to support NuGet V2, which is now obsolete, this
4949
> library does not support the short form at all. (This choice keeps documentation clarity
5050
> [NOT SUPPORTED] and implementation simplicity)
5151
5252
------
53-
<sup><a id="footnote_1">1</a></sup> `SemVer` contains constructors accepting an
54-
`AlhpanumericOrdering` enumeration to identify the ordering expected for a given instance.
55-
Unfortunately, major repositories using SemVer have chosen to use different comparisons. Thus,
56-
a consumer is required to specify if the version is compared insensitive or not.
57-
53+
<sup><a id="footnote_1">1</a></sup>Unfortunately, major repositories using SemVer have
54+
chosen to use different comparisons. Thus, a consumer is required to know a-priori if the
55+
version is compared insensitive or not. Thus all constructors accept an enum indicating
56+
the sort ordering to use. Additional, parsing accepts an IFormatProvider, which should
57+
provide an `AlphaNumeirvOrdering` value to specify the ordering. If none is provided, the
58+
default is used. (SemVer uses CaseSensitive comparisons, CSemVer and CSemVerCI ALWAYS use
59+
case insensitive) `IComparer<SemVer>` instances are available for cases where the versions
60+
are from mixed sources and the application wishes to order the versions.

src/Ubiquity.NET.Versioning/SemVer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ internal static bool TryParse(
278278
ArgumentNullException.ThrowIfNull( s );
279279

280280
provider ??= SemVerFormatProvider.CaseSensitive;
281-
IResult<SemVer> parseResult = SemVerGrammar.SemanticVersion(GetOrdering(provider)).TryParse(s);
281+
IResult<SemVer> parseResult = SemVerGrammar.SemanticVersion(provider.GetOrdering()).TryParse(s);
282282
if(parseResult.Failed(out ex))
283283
{
284284
result = default;
@@ -364,10 +364,5 @@ private static ImmutableArray<string> ValidateElementsWithParser(
364364

365365
return value.Value;
366366
}
367-
368-
private static AlphaNumericOrdering GetOrdering(IFormatProvider? provider)
369-
{
370-
return (AlphaNumericOrdering?)provider?.GetFormat(typeof(AlphaNumericOrdering)) ?? AlphaNumericOrdering.None;
371-
}
372367
}
373368
}

0 commit comments

Comments
 (0)