Skip to content

Commit 4bafb55

Browse files
committed
Refactored removed Diagnostics dependency
1 parent 67e3842 commit 4bafb55

File tree

9 files changed

+195
-136
lines changed

9 files changed

+195
-136
lines changed

Microsoft.Toolkit.Uwp.UI.Media/Geometry/CanvasPathBuilderExtensions.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Linq;
88
using System.Numerics;
99
using Microsoft.Graphics.Canvas.Geometry;
10-
using Microsoft.Toolkit.Diagnostics;
1110
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Core;
1211

1312
namespace Microsoft.Toolkit.Uwp.UI.Media.Geometry
@@ -98,11 +97,12 @@ public static void AddEllipseFigure(this CanvasPathBuilder pathBuilder, float x,
9897
}
9998
catch (ArgumentException)
10099
{
101-
// An ArgumentException will be raised if another figure was already begun( and not ended)
102-
// before calling AddEllipseFigure() method.
103-
ThrowHelper.ThrowInvalidOperationException("A call to CanvasPathBuilder.AddEllipseFigure occurred, " +
104-
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
105-
"before calling CanvasPathBuilder.AddEllipseFigure, to end the previous figure.");
100+
// An ArgumentException will be raised if another figure was already begun( and not ended) before calling AddEllipseFigure() method.
101+
static void Throw() => throw new InvalidOperationException("A call to CanvasPathBuilder.AddEllipseFigure occurred, " +
102+
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
103+
"before calling CanvasPathBuilder.AddEllipseFigure, to end the previous figure.");
104+
105+
Throw();
106106
}
107107

108108
// First Semi-Ellipse
@@ -143,7 +143,10 @@ public static void AddPolygonFigure(this CanvasPathBuilder pathBuilder, int numS
143143
radius = Math.Abs(radius);
144144

145145
// A polygon should have at least 3 sides
146-
Guard.IsGreaterThan(numSides, 2, nameof(numSides));
146+
if (numSides <= 2)
147+
{
148+
ThrowArgumentOutOfRangeException();
149+
}
147150

148151
// Calculate the first vertex location based on the number of sides
149152
var angle = Scalar.TwoPi / numSides;
@@ -158,11 +161,12 @@ public static void AddPolygonFigure(this CanvasPathBuilder pathBuilder, int numS
158161
}
159162
catch (ArgumentException)
160163
{
161-
// An ArgumentException will be raised if another figure was already begun( and not ended)
162-
// before calling AddPolygonFigure() method.
163-
ThrowHelper.ThrowInvalidOperationException("A call to CanvasPathBuilder.AddPolygonFigure occurred, " +
164-
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
165-
"before calling CanvasPathBuilder.AddPolygonFigure, to end the previous figure.");
164+
// An ArgumentException will be raised if another figure was already begun( and not ended) before calling AddPolygonFigure() method.
165+
static void Throw() => throw new InvalidOperationException("A call to CanvasPathBuilder.AddPolygonFigure occurred, " +
166+
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
167+
"before calling CanvasPathBuilder.AddPolygonFigure, to end the previous figure.");
168+
169+
Throw();
166170
}
167171

168172
// Add lines to the remaining vertices
@@ -178,6 +182,8 @@ public static void AddPolygonFigure(this CanvasPathBuilder pathBuilder, int numS
178182

179183
// End the Figure
180184
pathBuilder.EndFigure(CanvasFigureLoop.Closed);
185+
186+
static void ThrowArgumentOutOfRangeException() => throw new ArgumentOutOfRangeException($"Parameter {nameof(numSides)} must be greater than 2.");
181187
}
182188

183189
/// <summary>
@@ -202,11 +208,12 @@ public static void AddRectangleFigure(this CanvasPathBuilder pathBuilder, float
202208
}
203209
catch (ArgumentException)
204210
{
205-
// An ArgumentException will be raised if another figure was already begun( and not ended)
206-
// before calling AddPolygonFigure() method.
207-
ThrowHelper.ThrowInvalidOperationException("A call to CanvasPathBuilder.AddRectangleFigure occurred, " +
208-
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
209-
"before calling CanvasPathBuilder.AddRectangleFigure, to end the previous figure.");
211+
// An ArgumentException will be raised if another figure was already begun( and not ended) before calling AddPolygonFigure() method.
212+
static void Throw() => throw new InvalidOperationException("A call to CanvasPathBuilder.AddRectangleFigure occurred, " +
213+
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
214+
"before calling CanvasPathBuilder.AddRectangleFigure, to end the previous figure.");
215+
216+
Throw();
210217
}
211218

212219
// Top Side
@@ -267,11 +274,12 @@ internal static void AddRoundedRectangleFigure(this CanvasPathBuilder pathBuilde
267274
return;
268275
}
269276

270-
// An ArgumentException will be raised if another figure was already begun( and not ended)
271-
// before calling AddPolygonFigure() method.
272-
ThrowHelper.ThrowInvalidOperationException("A call to CanvasPathBuilder.AddRoundedRectangleFigure occurred, " +
273-
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
274-
"before calling CanvasPathBuilder.AddRoundedRectangleFigure, to end the previous figure.");
277+
// An ArgumentException will be raised if another figure was already begun( and not ended) before calling AddPolygonFigure() method.
278+
static void Throw() => throw new InvalidOperationException("A call to CanvasPathBuilder.AddRoundedRectangleFigure occurred, " +
279+
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
280+
"before calling CanvasPathBuilder.AddRoundedRectangleFigure, to end the previous figure.");
281+
282+
Throw();
275283
}
276284

277285
// Top line
@@ -356,11 +364,12 @@ internal static void AddSquircleFigure(this CanvasPathBuilder pathBuilder, ref C
356364
return;
357365
}
358366

359-
// An ArgumentException will be raised if another figure was already begun( and not ended)
360-
// before calling AddPolygonFigure() method.
361-
ThrowHelper.ThrowInvalidOperationException("A call to CanvasPathBuilder.AddSquircleFigure occurred, " +
362-
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
363-
"before calling CanvasPathBuilder.AddSquircleFigure, to end the previous figure.");
367+
// An ArgumentException will be raised if another figure was already begun( and not ended) before calling AddPolygonFigure() method.
368+
static void Throw() => throw new InvalidOperationException("A call to CanvasPathBuilder.AddSquircleFigure occurred, " +
369+
"when another figure was already begun. Please call CanvasPathBuilder.EndFigure method, " +
370+
"before calling CanvasPathBuilder.AddSquircleFigure, to end the previous figure.");
371+
372+
Throw();
364373
}
365374

366375
// Top line

Microsoft.Toolkit.Uwp.UI.Media/Geometry/Core/PathElementFactory.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Text.RegularExpressions;
7-
using Microsoft.Toolkit.Diagnostics;
87
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Elements.Path;
98

109
namespace Microsoft.Toolkit.Uwp.UI.Media.Geometry.Core
@@ -21,18 +20,14 @@ internal static class PathElementFactory
2120
/// <returns>ICanvasPathElement</returns>
2221
internal static ICanvasPathElement CreateDefaultPathElement(PathFigureType figureType)
2322
{
24-
ICanvasPathElement result = null;
25-
2623
if (figureType == PathFigureType.FillRule)
2724
{
28-
result = new FillRuleElement();
29-
}
30-
else
31-
{
32-
ThrowHelper.ThrowArgumentException("Creation of Only Default FillRuleElement is supported.", nameof(figureType));
25+
return new FillRuleElement();
3326
}
3427

35-
return result;
28+
static ICanvasPathElement Throw() => throw new ArgumentException("Creation of Only Default FillRuleElement is supported.");
29+
30+
return Throw();
3631
}
3732

3833
/// <summary>
@@ -42,18 +37,14 @@ internal static ICanvasPathElement CreateDefaultPathElement(PathFigureType figur
4237
/// <returns>ICanvasPathElement</returns>
4338
internal static ICanvasPathElement CreateDefaultPathElement(PathElementType elementType)
4439
{
45-
ICanvasPathElement result = null;
46-
4740
if (elementType == PathElementType.ClosePath)
4841
{
49-
result = new ClosePathElement();
50-
}
51-
else
52-
{
53-
ThrowHelper.ThrowArgumentException("Creation of Only Default ClosePathElement is supported.", nameof(elementType));
42+
return new ClosePathElement();
5443
}
5544

56-
return result;
45+
static ICanvasPathElement Throw() => throw new ArgumentException("Creation of Only Default ClosePathElement is supported.");
46+
47+
return Throw();
5748
}
5849

5950
/// <summary>

Microsoft.Toolkit.Uwp.UI.Media/Geometry/Elements/Stroke/CanvasStrokeElement.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Text.RegularExpressions;
77
using Microsoft.Graphics.Canvas;
8-
using Microsoft.Toolkit.Diagnostics;
98
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Core;
109
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Elements.Brush;
1110
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Parsers;
@@ -70,8 +69,9 @@ protected override void GetAttributes(Match match)
7069
// If the ICanvasBrushElement was not created, then the ICanvasStroke cannot be created
7170
if (_brush == null)
7271
{
73-
ThrowHelper.ThrowArgumentException("Unable to create a valid ICanvasBrush for the " +
74-
$"ICanvasStroke with the following Brush data - '{group.Value}'");
72+
static void Throw(string value) => throw new ArgumentException($"Unable to create a valid ICanvasBrush for the ICanvasStroke with the following Brush data - '{value}'.");
73+
74+
Throw(group.Value);
7575
}
7676

7777
// Stroke Style

Microsoft.Toolkit.Uwp.UI.Media/Geometry/Parsers/CanvasBrushParser.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Runtime.CompilerServices;
67
using Microsoft.Graphics.Canvas;
78
using Microsoft.Graphics.Canvas.Brushes;
8-
using Microsoft.Toolkit.Diagnostics;
99
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Core;
1010
using Microsoft.Toolkit.Uwp.UI.Media.Geometry.Elements.Brush;
1111

@@ -26,14 +26,17 @@ internal static ICanvasBrushElement Parse(string brushData)
2626
var matches = RegexFactory.CanvasBrushRegex.Matches(brushData);
2727

2828
// If no match is found or no captures in the match, then it means that the brush data is invalid.
29-
Guard.IsFalse(matches.Count == 0, "(brushData matches.Count == 0)", $"BRUSH_ERR001:Invalid Brush data! No matching brush data found!\nBrush Data: {brushData}");
29+
if (matches.Count == 0)
30+
{
31+
ThrowForZeroCount();
32+
}
3033

31-
// If the match contains more than one captures, it means that there are multiple brushes present in the brush data. There should be only one brush defined in the brush data.
32-
Guard.IsFalse(matches.Count > 1, "(brushData matches.Count > 1)", "BRUSH_ERR002:Multiple Brushes defined in Brush Data! " +
33-
"There should be only one Brush definition within the Brush Data. " +
34-
"You can either remove Brush definitions or split the Brush Data " +
35-
"into multiple Brush Data and call the CanvasPathGeometry.CreateBrush() method on each of them." +
36-
$"\nBrush Data: {brushData}");
34+
// If the match contains more than one captures, it means that there are multiple brushes present in the brush data.
35+
// There should be only one brush defined in the brush data.
36+
if (matches.Count > 1)
37+
{
38+
ThrowForNotOneCount();
39+
}
3740

3841
// There should be only one match
3942
var match = matches[0];
@@ -71,24 +74,34 @@ internal static ICanvasBrushElement Parse(string brushData)
7174
// If there are invalid characters, extract them and add them to the ArgumentException message
7275
if (preValidationCount != postValidationCount)
7376
{
74-
var parseIndex = 0;
75-
if (!string.IsNullOrWhiteSpace(brushElement.Data))
77+
[MethodImpl(MethodImplOptions.NoInlining)]
78+
static void ThrowForInvalidCharacters(AbstractCanvasBrushElement brushElement, string brushData)
7679
{
77-
parseIndex = brushData.IndexOf(brushElement.Data, parseIndex, StringComparison.Ordinal);
78-
}
80+
var parseIndex = 0;
81+
if (!string.IsNullOrWhiteSpace(brushElement.Data))
82+
{
83+
parseIndex = brushData.IndexOf(brushElement.Data, parseIndex, StringComparison.Ordinal);
84+
}
7985

80-
var errorString = brushData.Substring(parseIndex);
81-
if (errorString.Length > 30)
82-
{
83-
errorString = $"{errorString.Substring(0, 30)}...";
84-
}
86+
var errorString = brushData.Substring(parseIndex);
87+
if (errorString.Length > 30)
88+
{
89+
errorString = $"{errorString.Substring(0, 30)}...";
90+
}
8591

86-
errorString = $"BRUSH_ERR003:Brush data contains invalid characters!\nIndex: {parseIndex}\n{errorString}";
92+
throw new ArgumentException($"BRUSH_ERR003:Brush data contains invalid characters!\nIndex: {parseIndex}\n{errorString}");
93+
}
8794

88-
ThrowHelper.ThrowArgumentException(errorString);
95+
ThrowForInvalidCharacters(brushElement, brushData);
8996
}
9097

9198
return brushElement;
99+
100+
static void ThrowForZeroCount() => throw new ArgumentException("BRUSH_ERR001:Invalid Brush data! No matching brush data found!");
101+
static void ThrowForNotOneCount() => throw new ArgumentException("BRUSH_ERR002:Multiple Brushes defined in Brush Data! " +
102+
"There should be only one Brush definition within the Brush Data. " +
103+
"You can either remove Brush definitions or split the Brush Data " +
104+
"into multiple Brush Data and call the CanvasPathGeometry.CreateBrush() method on each of them.");
92105
}
93106

94107
/// <summary>

0 commit comments

Comments
 (0)