-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring & AsFormatted Extension method added for IMorestachioError
- Loading branch information
Showing
5 changed files
with
164 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
Morestachio/Parsing/ParserErrors/MorestachioErrorBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
Morestachio/Parsing/ParserErrors/MorestachioErrorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |