Skip to content

Commit

Permalink
refactor: Simplify parsing by removing switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Crown0815 committed Oct 29, 2023
1 parent ea31e2b commit a7efe1f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/ConventionalChangelog/Conventional/CommitMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace ConventionalChangelog.Conventional;
public record CommitMessage(string TypeIndicator, string Description, string Body, IReadOnlyCollection<Footer>
Footers) : IPrintable
{
internal static readonly CommitMessage Empty = new("", "", "", Array.Empty<Footer>());

public record Footer(string Token, string Value);

public string Hash { get; internal init; } = Guid.NewGuid().ToString();
Expand Down
18 changes: 6 additions & 12 deletions src/ConventionalChangelog/Conventional/MessageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,31 @@ public MessageParser(IConfiguration configuration)
_configuration = configuration;
}

public CommitMessage Parse(string rawMessage) => rawMessage switch
{
"" => Empty,
_ => InnerParse(rawMessage),
};

private CommitMessage InnerParse(string rawMessage)
public CommitMessage Parse(string rawMessage)
{
using var lines = new StringReader(rawMessage);
return Read(lines);
}

private CommitMessage Read(TextReader lines)
{
var (typeIndicator, description) = HeaderFrom(lines.ReadLine()!);
var (typeIndicator, description) = HeaderFrom(lines.ReadLine());
var (body, footers) = BodyFrom(lines);
typeIndicator = _configuration.TypeFor(typeIndicator, footers);

return new CommitMessage(typeIndicator, description, body, footers);
}

#if NET6_0
private static (string, string) HeaderFrom(string header)
private static (string, string) HeaderFrom(string? header)
{
var twoParts = header.Split(Separator);
return twoParts.Length == 2
var twoParts = header?.Split(Separator);
return twoParts?.Length == 2
? (twoParts.First(), twoParts.Last().Trim())
: ("", "");
}
#elif NET7_0_OR_GREATER
private static (string, string) HeaderFrom(string header) => header.Split(Separator) is [var first, var second]
private static (string, string) HeaderFrom(string? header) => header?.Split(Separator) is [var first, var second]
? (first,second.Trim())
: ("", "");
#endif
Expand Down

0 comments on commit a7efe1f

Please sign in to comment.