Skip to content

Commit 6fc8191

Browse files
Simplified the exception messaging in Unit string formatting.
1 parent b094d17 commit 6fc8191

File tree

12 files changed

+75
-60
lines changed

12 files changed

+75
-60
lines changed

OnixLabs.Units/Area.To.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public readonly partial struct Area<T>
3232
/// <param name="format">A standard or custom format string.</param>
3333
/// <param name="formatProvider">An object that provides culture-specific formatting information.</param>
3434
/// <returns>Returns a string representation of the current instance in the specified format.</returns>
35-
public string ToString(string? format, IFormatProvider? formatProvider = null) =>
36-
ToString(format.AsSpan(), formatProvider);
35+
public string ToString(string? format, IFormatProvider? formatProvider = null) => ToString(format.AsSpan(), formatProvider);
3736

3837
/// <summary>
3938
/// Returns a string representation of the current instance using the specified format and format provider.
@@ -74,12 +73,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
7473
SquareMilesSpecifier => SquareMiles,
7574
AcresSpecifier => Acres,
7675
HectaresSpecifier => Hectares,
77-
_ => throw new ArgumentException(
78-
$"Format '{format.ToString()}' is invalid. Valid format specifiers are: " +
79-
"sqym, sqzm, sqam, sqfm, sqpm, sqnm, squm, sqmm, sqcm, sqdm, sqm, sqdam, sqhm, sqkm, " +
80-
"sqMm, sqGm, sqTm, sqPm, sqEm, sqZm, sqYm, sqin, sqft, sqyd, sqmi, ac, ha. " +
81-
"Format specifiers may also be suffixed with a scale value.",
82-
nameof(format))
76+
_ => throw ArgumentException.InvalidFormat(format,
77+
"sqym, sqzm, sqam, sqfm, sqpm, sqnm, squm, sqmm, sqcm, " +
78+
"sqdm, sqm, sqdam, sqhm, sqkm, sqMm, sqGm, sqTm, sqPm, " +
79+
"sqEm, sqZm, sqYm, sqin, sqft, sqyd, sqmi, ac, and ha")
8380
};
8481

8582
T rounded = scale > 0 ? T.Round(value, scale) : value;

OnixLabs.Units/DataSize.To.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
8080
YobiBytesSpecifier => YobiBytes,
8181
YottaBitsSpecifier => YottaBits,
8282
YottaBytesSpecifier => YottaBytes,
83-
_ => throw new ArgumentException(
84-
$"Format '{format}' is invalid. Valid format specifiers are: " +
85-
"b, B, Kib, KiB, Kb, KB, Mib, MiB, Mb, MB, Gib, GiB, Gb, GB, " +
86-
"Tib, TiB, Tb, TB, Pib, PiB, Pb, PB, Eib, EiB, Eb, EB, " +
87-
"Zib, ZiB, Zb, ZB, Yib, YiB, Yb, YB. " +
88-
"Format specifiers may also be suffixed with a scale value.",
89-
nameof(format))
83+
_ => throw ArgumentException.InvalidFormat(format,
84+
"b, B, Kib, KiB, Kb, KB, Mib, MiB, Mb, MB, Gib, GiB, " +
85+
"Gb, GB, Tib, TiB, Tb, TB, Pib, PiB, Pb, PB, Eib, EiB, " +
86+
"Eb, EB, Zib, ZiB, Zb, ZB, Yib, YiB, Yb, and YB")
9087
};
9188

9289
T rounded = scale > 0 ? T.Round(value, scale) : value;

OnixLabs.Units/Distance.To.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
8181
AstronomicalUnitsSpecifier => AstronomicalUnits,
8282
LightYearsSpecifier => LightYears,
8383
ParsecsSpecifier => Parsecs,
84-
_ => throw new ArgumentException(
85-
$"Format '{format.ToString()}' is invalid. " +
86-
"Valid format specifiers are: " +
87-
"qm, rm, ym, zm, am, fm, pm, nm, um, mm, cm, dm, m, dam, hm, km, Mm, Gm, Tm, Pm, Em, Zm, Ym, Rm, Qm, " +
88-
"in, ft, yd, mi, nmi, fmi, a, au, ly, pc. " +
89-
"Format specifiers may also be suffixed with a scale value.",
90-
nameof(format))
84+
_ => throw ArgumentException.InvalidFormat(format,
85+
"qm, rm, ym, zm, am, fm, pm, nm, um, mm, cm, dm, " +
86+
"m, dam, hm, km, Mm, Gm, Tm, Pm, Em, Zm, Ym, Rm, " +
87+
"Qm, in, ft, yd, mi, nmi, fmi, a, au, ly, and pc")
9188
};
9289

9390
T rounded = scale > 0 ? T.Round(value, scale) : value;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.Runtime.CompilerServices;
17+
18+
namespace OnixLabs.Units;
19+
20+
internal static class ArgumentExceptionExtensions
21+
{
22+
extension(ArgumentException)
23+
{
24+
public static ArgumentException InvalidFormat(
25+
ReadOnlySpan<char> format,
26+
string specifiers,
27+
[CallerArgumentExpression(nameof(format))]
28+
string? parameterName = null
29+
) => new($"Format '{format.ToString()}' is invalid. Valid format specifiers are: {specifiers}. " +
30+
$"Format specifiers may also be suffixed with a scale value.", parameterName);
31+
}
32+
}

OnixLabs.Units/Force.To.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public readonly partial struct Force<T>
3232
/// <param name="format">The format to use, or null to use the default format.</param>
3333
/// <param name="formatProvider">The provider to use to format the value.</param>
3434
/// <returns>Returns the value of the current instance in the specified format.</returns>
35-
public string ToString(string? format, IFormatProvider? formatProvider = null) =>
36-
ToString(format.AsSpan(), formatProvider);
35+
public string ToString(string? format, IFormatProvider? formatProvider = null) => ToString(format.AsSpan(), formatProvider);
3736

3837
/// <summary>
3938
/// Formats the value of the current instance using the specified format.
@@ -73,13 +72,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
7372
PoundalsSpecifier => Poundals,
7473
ShortTonForceSpecifier => ShortTonForce,
7574
LongTonForceSpecifier => LongTonForce,
76-
_ => throw new ArgumentException(
77-
$"Format '{format.ToString()}' is invalid. " +
78-
"Valid format specifiers are: " +
79-
"yN, zN, aN, fN, pN, nN, uN, mN, N, kN, MN, GN, TN, PN, EN, ZN, YN, " +
80-
"dyn, kgf, gf, tf, lbf, ozf, pdl, tonf, ltf. " +
81-
"Format specifiers may also be suffixed with a scale value.",
82-
nameof(format))
75+
_ => throw ArgumentException.InvalidFormat(format,
76+
"yN, zN, aN, fN, pN, nN, uN, mN, N, " +
77+
"kN, MN, GN, TN, PN, EN, ZN, YN, dyn, " +
78+
"kgf, gf, tf, lbf, ozf, pdl, tonf, and ltf")
8379
};
8480

8581
T rounded = scale > 0 ? T.Round(value, scale) : value;

OnixLabs.Units/Mass.To.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
7676
TroyPoundsSpecifier => TroyPounds,
7777
TroyOuncesSpecifier => TroyOunces,
7878
PennyweightsSpecifier => Pennyweights,
79-
_ => throw new ArgumentException(
80-
$"Format '{format.ToString()}' is invalid. " +
81-
"Valid format specifiers are: " +
82-
"yg, zg, ag, fg, pg, ng, ug, mg, g, kg, Mg, t, Gg, Tg, Pg, Eg, Zg, Yg, " +
83-
"lb, oz, st, gr, ton, lt, cwtUS, cwtUK, qr, lbt, ozt, dwt. " +
84-
"Format specifiers may also be suffixed with a scale value.",
85-
nameof(format))
79+
_ => throw ArgumentException.InvalidFormat(format,
80+
"yg, zg, ag, fg, pg, ng, ug, mg, g, kg, Mg, " +
81+
"t, Gg, Tg, Pg, Eg, Zg, Yg, lb, oz, st, gr, " +
82+
"ton, lt, cwtUS, cwtUK, qr, lbt, ozt, and dwt")
8683
};
8784

8885
T rounded = scale > 0 ? T.Round(value, scale) : value;

OnixLabs.Units/Pressure.To.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
8484
BaryeSpecifier => Barye,
8585
MillimetersOfWaterColumnSpecifier => MillimetersOfWaterColumn,
8686
InchesOfWaterColumnSpecifier => InchesOfWaterColumn,
87-
_ => throw new ArgumentException(
88-
$"Format '{format.ToString()}' is invalid. " +
89-
"Valid format specifiers are: " +
90-
"qPa, rPa, yPa, zPa, aPa, fPa, pPa, nPa, uPa, mPa, cPa, dPa, Pa, daPa, hPa, kPa, MPa, GPa, TPa, " +
91-
"PPa, EPa, ZPa, YPa, RPa, QPa, bar, mbar, atm, at, Torr, mmHg, inHg, psi, psf, Ba, mmH2O, inH2O. " +
92-
"Format specifiers may also be suffixed with a scale value.",
93-
nameof(format))
87+
_ => throw ArgumentException.InvalidFormat(format,
88+
"qPa, rPa, yPa, zPa, aPa, fPa, pPa, nPa, uPa, mPa, cPa, dPa, Pa, " +
89+
"daPa, hPa, kPa, MPa, GPa, TPa, PPa, EPa, ZPa, YPa, RPa, QPa, bar, " +
90+
"mbar, atm, at, Torr, mmHg, inHg, psi, psf, Ba, mmwc, and inwc")
9491
};
9592

9693
T rounded = scale > 0 ? T.Round(value, scale) : value;

OnixLabs.Units/Pressure.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ namespace OnixLabs.Units;
211211
public T Barye => QuectoPascals / T.CreateChecked(0.1e30);
212212

213213
/// <summary>
214-
/// Gets the pressure in millimetres of water column (mmH₂O).
214+
/// Gets the pressure in millimetres of water column (mmwc).
215215
/// </summary>
216216
public T MillimetersOfWaterColumn => QuectoPascals / T.CreateChecked(9.80665e30);
217217

218218
/// <summary>
219-
/// Gets the pressure in inches of water column (inH₂O).
219+
/// Gets the pressure in inches of water column (inwc).
220220
/// </summary>
221221
public T InchesOfWaterColumn => QuectoPascals / T.CreateChecked(249.08891e30);
222222
}

OnixLabs.Units/Speed.To.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
7777
KilometersPerHourSpecifier => KilometersPerHour,
7878
MilesPerHourSpecifier => MilesPerHour,
7979
KnotsSpecifier => Knots,
80-
_ => throw new ArgumentException(
81-
$"Format '{format.ToString()}' is invalid. Valid format specifiers are: " +
82-
"qmps, rmps, ymps, zmps, amps, fmps, pmps, nmps, umps, mmps, cmps, dmps, mps, damps, " +
83-
"hmps, kmps, Mmps, Gmps, Tmps, Pmps, Emps, Zmps, Ymps, Rmps, Qmps, ips, fps, kmph, mph, kt. " +
84-
"Format specifiers may also be suffixed with a scale value.",
85-
nameof(format))
80+
_ => throw ArgumentException.InvalidFormat(format,
81+
"qmps, rmps, ymps, zmps, amps, fmps, pmps, nmps, umps, mmps, " +
82+
"cmps, dmps, mps, damps, hmps, kmps, Mmps, Gmps, Tmps, Pmps, " +
83+
"Emps, Zmps, Ymps, Rmps, Qmps, ips, fps, kmph, mph, and kt")
8684
};
8785

8886
T rounded = scale > 0 ? T.Round(value, scale) : value;

OnixLabs.Units/Temperature.To.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvide
5454
RankineSpecifier => (Rankine, RankineSymbol),
5555
ReaumurSpecifier => (Reaumur, ReaumurSymbol),
5656
RomerSpecifier => (Romer, RomerSymbol),
57-
_ => throw new ArgumentException($"Format '{format}' is invalid. Valid format specifiers are: C, De, F, K, N, R, Re, and Ro. Format specifiers may also be suffixed with a scale value.", nameof(format))
57+
_ => throw ArgumentException.InvalidFormat(format, "C, De, F, K, N, R, Re, and Ro")
5858
};
5959

6060
T rounded = scale > 0 ? T.Round(value, scale) : value;

0 commit comments

Comments
 (0)