Skip to content

Commit 53d659e

Browse files
committed
Started redesigning internal parsers; identity and boolean implementations
1 parent 95d91f2 commit 53d659e

1 file changed

Lines changed: 13 additions & 105 deletions

File tree

  • src/IGLib.Core/Console/Validation/Implementations/StringParsing

src/IGLib.Core/Console/Validation/Implementations/StringParsing/Parsers.cs

Lines changed: 13 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -5,118 +5,26 @@
55
using System.Linq;
66
using System.Text;
77

8-
namespace IGLib
9-
{
10-
11-
/// <summary>Dummy identity parser - just returns the original string when parsing a string value from a string.</summary>
12-
internal sealed class IdentityStringParser : IStringParser<string>
13-
{
14-
public bool TryParse(string text, out string value)
15-
{
16-
value = text;
17-
return true;
18-
}
19-
}
20-
21-
/// <summary>Parses boolean from a striring.</summary>
22-
internal sealed class BooleanStringParser : IStringParser<bool>
23-
{
24-
public bool TryParse(string text, out bool value) =>
25-
bool.TryParse(text, out value);
26-
}
27-
28-
29-
internal sealed class Int32StringParser : IStringParser<int>
30-
{
31-
private readonly IFormatProvider _formatProvider;
32-
33-
public Int32StringParser(IFormatProvider formatProvider)
34-
{
35-
_formatProvider = formatProvider;
36-
}
37-
38-
public bool TryParse(string text, out int value) =>
39-
int.TryParse(text, NumberStyles.Integer, _formatProvider, out value);
40-
}
41-
42-
43-
44-
internal sealed class DecimalStringParser : IStringParser<decimal>
45-
{
46-
private readonly IFormatProvider _formatProvider;
47-
48-
public DecimalStringParser(IFormatProvider formatProvider)
49-
{
50-
_formatProvider = formatProvider;
51-
}
52-
53-
public bool TryParse(string text, out decimal value) =>
54-
decimal.TryParse(
55-
text,
56-
NumberStyles.Number,
57-
_formatProvider,
58-
out value);
59-
}
60-
8+
namespace IGLib;
619

62-
internal sealed class DoubleStringParser : IStringParser<double>
63-
{
64-
private readonly IFormatProvider _formatProvider;
65-
66-
public DoubleStringParser(IFormatProvider formatProvider)
67-
{
68-
_formatProvider = formatProvider;
69-
}
70-
71-
public bool TryParse(string text, out double value) =>
72-
double.TryParse(
73-
text,
74-
NumberStyles.Float | NumberStyles.AllowThousands,
75-
_formatProvider,
76-
out value);
77-
}
78-
79-
80-
81-
internal sealed class DateTimeStringParser : IStringParser<DateTime>
82-
{
83-
private readonly IFormatProvider _formatProvider;
84-
85-
public DateTimeStringParser(IFormatProvider formatProvider)
86-
{
87-
_formatProvider = formatProvider;
88-
}
89-
90-
public bool TryParse(string text, out DateTime value) =>
91-
DateTime.TryParse(
92-
text,
93-
_formatProvider,
94-
DateTimeStyles.None,
95-
out value);
96-
}
9710

98-
99-
internal sealed class GuidStringParser : IStringParser<Guid>
11+
/// <summary>Dummy identity parser - just returns the original string when parsing a string value from a string.</summary>
12+
internal sealed class IdentityStringParser : IStringParser<string>
13+
{
14+
public bool TryParse(string text, out string value)
10015
{
101-
public bool TryParse(string text, out Guid value) =>
102-
Guid.TryParse(text, out value);
16+
value = text;
17+
return true;
10318
}
19+
}
10420

105-
106-
internal sealed class TimeSpanStringParser : IStringParser<TimeSpan>
21+
internal sealed class BooleanStringParser : IStringParser<bool>
22+
{
23+
public bool TryParse(string text, out bool value)
10724
{
108-
private readonly IFormatProvider _formatProvider;
109-
110-
public TimeSpanStringParser(IFormatProvider formatProvider)
111-
{
112-
_formatProvider = formatProvider;
113-
}
114-
115-
public bool TryParse(string text, out TimeSpan value) =>
116-
TimeSpan.TryParse(text, _formatProvider, out value);
25+
return bool.TryParse(text, out value);
11726
}
27+
}
11828

11929

12030

121-
122-
}

0 commit comments

Comments
 (0)