Skip to content

Commit

Permalink
Refactoring & AsFormatted Extension method added for IMorestachioError
Browse files Browse the repository at this point in the history
  • Loading branch information
JPVenson committed Jun 15, 2022
1 parent 7ba0984 commit 7d14700
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Morestachio.Extensions.Logging/MicrosoftLoggerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void Log(string logLevel, string eventId, string message)
_logger.Log(ParseLogLevel(logLevel), new EventId(_eventId, eventId), new LogEntry(message, null), null, FormatMessage);
}

private string FormatMessage(LogEntry entry, Exception? arg2)
private string FormatMessage(LogEntry entry, Exception arg2)
{
if (_format != null)
{
Expand Down
5 changes: 1 addition & 4 deletions Morestachio/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ public static async MorestachioDocumentInfoPromise ParseWithOptionsAsync(ParserO
var tokenzierContext = new TokenzierContext(new List<int>(), parsingOptions.CultureInfo);
var tokenizerResult = await Tokenizer.Tokenize(parsingOptions, tokenzierContext).ConfigureAwait(false);

parsingOptions.Logger?.LogDebug(LoggingFormatter.ParserEventId, "Template Parsed", new Dictionary<string, object>()
{
{"Errors", tokenzierContext.Errors}
});
parsingOptions.Logger?.LogError(LoggingFormatter.ParserEventId, $"Template Parsed. {string.Join("\r\n", tokenzierContext.Errors.Select(f => f.AsFormatted()))}");
//if there are any errors do not parse the template
var documentInfo = new MorestachioDocumentInfo(parsingOptions,
tokenzierContext.Errors.Any() ? null : Parse(tokenizerResult, parsingOptions), tokenzierContext.Errors);
Expand Down
139 changes: 0 additions & 139 deletions Morestachio/Parsing/ParserErrors/IMorestachioError.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Morestachio.Framework.Error;

namespace Morestachio.Parsing.ParserErrors;

Expand Down Expand Up @@ -33,140 +30,4 @@ public interface IMorestachioError : IXmlSerializable, ISerializable, IEquatable
/// </summary>
/// <param name="sb"></param>
void Format(StringBuilder sb);
}

/// <summary>
/// Base class for morestachio errors that support serialization
/// </summary>
public abstract class MorestachioErrorBase : IMorestachioError
{
/// <summary>
/// Serialization constructor
/// </summary>
protected MorestachioErrorBase()
{

}

/// <summary>
/// Serialization constructor
/// </summary>
/// <param name="location"></param>
/// <param name="helpText"></param>
protected MorestachioErrorBase(CharacterLocationExtended location, string helpText = null)
{
Location = location;
HelpText = helpText;
}

/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <param name="c"></param>
protected MorestachioErrorBase(SerializationInfo info, StreamingContext c)
{
HelpText = info.GetString(nameof(HelpText));
Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromBinary(info, c);
}

/// <inheritdoc />
public XmlSchema GetSchema()
{
return null;
}

/// <inheritdoc />
public virtual void ReadXml(XmlReader reader)
{
HelpText = reader.GetAttribute(nameof(HelpText));
reader.ReadStartElement();
Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromXml(reader);

if (!reader.IsEmptyElement)
{
reader.ReadEndElement();
}
}

/// <inheritdoc />
public virtual void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString(nameof(HelpText), HelpText);
writer.WriteStartElement(nameof(Location));
ErrorSerializationHelper.WriteCharacterLocationExtendedFromXml(writer, Location);
writer.WriteEndElement();
}

/// <inheritdoc />
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(HelpText), HelpText);
info.AddValue(nameof(Location), Location);
ErrorSerializationHelper.WriteCharacterLocationExtendedToBinary(info, context, Location);
}

/// <inheritdoc />
public CharacterLocationExtended Location { get; private set; }

/// <inheritdoc />
public string HelpText { get; private set; }

/// <inheritdoc />
public virtual Exception GetException()
{
return new IndexedParseException(Location, HelpText);
}

/// <inheritdoc />
public virtual void Format(StringBuilder sb)
{
sb.Append(IndexedParseException.FormatMessage(HelpText, Location));
}

/// <inheritdoc />
public virtual bool Equals(IMorestachioError other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Location.Equals(other.Location) && HelpText == other.HelpText;
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((IMorestachioError)obj);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (Location.GetHashCode() * 397) ^ (HelpText != null ? HelpText.GetHashCode() : 0);
}
}
}
142 changes: 142 additions & 0 deletions Morestachio/Parsing/ParserErrors/MorestachioErrorBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System.Text;
using System.Xml;
using System.Xml.Schema;
using Morestachio.Framework.Error;

namespace Morestachio.Parsing.ParserErrors;

/// <summary>
/// Base class for morestachio errors that support serialization
/// </summary>
public abstract class MorestachioErrorBase : IMorestachioError
{
/// <summary>
/// Serialization constructor
/// </summary>
protected MorestachioErrorBase()
{

}

/// <summary>
/// Serialization constructor
/// </summary>
/// <param name="location"></param>
/// <param name="helpText"></param>
protected MorestachioErrorBase(CharacterLocationExtended location, string helpText = null)
{
Location = location;
HelpText = helpText;
}

/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <param name="c"></param>
protected MorestachioErrorBase(SerializationInfo info, StreamingContext c)
{
HelpText = info.GetString(nameof(HelpText));
Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromBinary(info, c);
}

/// <inheritdoc />
public XmlSchema GetSchema()
{
return null;
}

/// <inheritdoc />
public virtual void ReadXml(XmlReader reader)
{
HelpText = reader.GetAttribute(nameof(HelpText));
reader.ReadStartElement();
Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromXml(reader);

if (!reader.IsEmptyElement)
{
reader.ReadEndElement();
}
}

/// <inheritdoc />
public virtual void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString(nameof(HelpText), HelpText);
writer.WriteStartElement(nameof(Location));
ErrorSerializationHelper.WriteCharacterLocationExtendedFromXml(writer, Location);
writer.WriteEndElement();
}

/// <inheritdoc />
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(HelpText), HelpText);
info.AddValue(nameof(Location), Location);
ErrorSerializationHelper.WriteCharacterLocationExtendedToBinary(info, context, Location);
}

/// <inheritdoc />
public CharacterLocationExtended Location { get; private set; }

/// <inheritdoc />
public string HelpText { get; private set; }

/// <inheritdoc />
public virtual Exception GetException()
{
return new IndexedParseException(Location, HelpText);
}

/// <inheritdoc />
public virtual void Format(StringBuilder sb)
{
sb.Append(IndexedParseException.FormatMessage(HelpText, Location));
}

/// <inheritdoc />
public virtual bool Equals(IMorestachioError other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Location.Equals(other.Location) && HelpText == other.HelpText;
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((IMorestachioError)obj);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (Location.GetHashCode() * 397) ^ (HelpText != null ? HelpText.GetHashCode() : 0);
}
}
}
20 changes: 20 additions & 0 deletions Morestachio/Parsing/ParserErrors/MorestachioErrorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Morestachio.Parsing.ParserErrors;

/// <summary>
/// Defines extension methods for <see cref="IMorestachioError"/>
/// </summary>
public static class MorestachioErrorExtensions
{
/// <summary>
/// Uses
/// </summary>
/// <param name="error"></param>
/// <returns></returns>
public static string AsFormatted(this IMorestachioError error)
{
var sb = StringBuilderCache.Acquire();
error.Format(sb);

return StringBuilderCache.GetStringAndRelease(sb);
}
}

0 comments on commit 7d14700

Please sign in to comment.