Skip to content

Commit

Permalink
imp - prt - Condensed to/from parsers
Browse files Browse the repository at this point in the history
---

This is the first part, so build will fail.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/2
  • Loading branch information
AptiviCEO committed Mar 31, 2024
1 parent f0053ce commit e255c12
Show file tree
Hide file tree
Showing 25 changed files with 234 additions and 374 deletions.
32 changes: 22 additions & 10 deletions VisualCard/Parts/BaseCardPartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ namespace VisualCard.Parts
/// <summary>
/// Base card part class
/// </summary>
[DebuggerDisplay("Base card part = ALTID: {AltId}")]
[DebuggerDisplay("Base card part | ALTID: {AltId}, TYPE: {ElementType}, VALUE: {ValueType}")]
public abstract class BaseCardPartInfo : IEquatable<BaseCardPartInfo>
{
/// <summary>
/// Arguments that follow the AltId
/// Final arguments
/// </summary>
public virtual string[] AltArguments { get; internal set; }
public virtual string[] Arguments { get; internal set; }

/// <summary>
/// Alternative ID. Zero if unspecified.
/// </summary>
public virtual int AltId { get; internal set; }

/// <summary>
/// Card element type (home, work, ...)
/// </summary>
public virtual string[] ElementTypes { get; internal set; }

/// <summary>
/// Value type (usually set by VALUE=)
/// </summary>
public virtual string ValueType { get; internal set; }

/// <summary>
/// Checks to see if both the parts are equal
/// </summary>
Expand All @@ -62,8 +72,10 @@ public bool Equals(BaseCardPartInfo source, BaseCardPartInfo target)

// Check all the properties
return
source.AltArguments.SequenceEqual(target.AltArguments) &&
source.AltId == target.AltId
source.Arguments.SequenceEqual(target.Arguments) &&
source.AltId == target.AltId &&
source.ElementTypes == target.ElementTypes &&
source.ValueType == target.ValueType
;
}

Expand All @@ -74,9 +86,11 @@ public override bool Equals(object obj) =>
/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = -2100286935;
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(AltArguments);
int hashCode = 936749766;
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(Arguments);
hashCode = hashCode * -1521134295 + AltId.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(ElementTypes);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ValueType);
return hashCode;
}

Expand All @@ -88,9 +102,7 @@ public override int GetHashCode()
public static bool operator !=(BaseCardPartInfo left, BaseCardPartInfo right) =>
!(left == right);

internal abstract BaseCardPartInfo FromStringVcardInternal(string value, int altId, Version cardVersion);

internal abstract BaseCardPartInfo FromStringVcardWithTypeInternal(string value, string[] finalArgs, int altId, Version cardVersion);
internal abstract BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion);

internal abstract string ToStringVcardInternal(Version cardVersion);
}
Expand Down
90 changes: 25 additions & 65 deletions VisualCard/Parts/Implementations/AddressInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using VisualCard.Parsers;

Expand All @@ -33,10 +32,6 @@ namespace VisualCard.Parts.Implementations
[DebuggerDisplay("Address = {PostOfficeBox}, {ExtendedAddress}, {StreetAddress}, {Locality}, {Region}, {PostalCode}, {Country}")]
public class AddressInfo : BaseCardPartInfo, IEquatable<AddressInfo>
{
/// <summary>
/// The contact's address types
/// </summary>
public string[] AddressTypes { get; }
/// <summary>
/// The contact's post office box
/// </summary>
Expand Down Expand Up @@ -66,22 +61,19 @@ public class AddressInfo : BaseCardPartInfo, IEquatable<AddressInfo>
/// </summary>
public string Country { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, int altId, Version cardVersion) =>
new AddressInfo().FromStringVcardInternal(value, altId, cardVersion);

internal static BaseCardPartInfo FromStringVcardWithTypeStatic(string value, string[] finalArgs, int altId, Version cardVersion) =>
new AddressInfo().FromStringVcardWithTypeInternal(value, finalArgs, altId, cardVersion);
internal static BaseCardPartInfo FromStringVcardStatic(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion) =>
new AddressInfo().FromStringVcardInternal(value, finalArgs, altId, elementTypes, valueType, cardVersion);

internal override string ToStringVcardInternal(Version cardVersion)
{
bool altIdSupported = cardVersion.Major >= 4;
if (altIdSupported)
{
bool installAltId = AltId >= 0 && AltArguments.Length > 0;
bool installAltId = AltId >= 0 && Arguments.Length > 0;
return
$"{VcardConstants._addressSpecifier};" +
$"{(installAltId ? VcardConstants._altIdArgumentSpecifier + AltId + VcardConstants._fieldDelimiter : "")}" +
$"{VcardConstants._typeArgumentSpecifier}{string.Join(",", AddressTypes)}{VcardConstants._argumentDelimiter}" +
$"{VcardConstants._typeArgumentSpecifier}{string.Join(",", ElementTypes)}{VcardConstants._argumentDelimiter}" +
$"{PostOfficeBox}{VcardConstants._fieldDelimiter}" +
$"{ExtendedAddress}{VcardConstants._fieldDelimiter}" +
$"{StreetAddress}{VcardConstants._fieldDelimiter}" +
Expand All @@ -94,7 +86,7 @@ internal override string ToStringVcardInternal(Version cardVersion)
{
return
$"{VcardConstants._addressSpecifier};" +
$"{VcardConstants._typeArgumentSpecifier}{string.Join(",", AddressTypes)}{VcardConstants._argumentDelimiter}" +
$"{VcardConstants._typeArgumentSpecifier}{string.Join(",", ElementTypes)}{VcardConstants._argumentDelimiter}" +
$"{PostOfficeBox}{VcardConstants._fieldDelimiter}" +
$"{ExtendedAddress}{VcardConstants._fieldDelimiter}" +
$"{StreetAddress}{VcardConstants._fieldDelimiter}" +
Expand All @@ -105,56 +97,27 @@ internal override string ToStringVcardInternal(Version cardVersion)
}
}

internal override BaseCardPartInfo FromStringVcardInternal(string value, int altId, Version cardVersion)
internal override BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion)
{
// Get the value
string adrValue = value.Substring(VcardConstants._addressSpecifier.Length + 1);
string[] splitAdr = adrValue.Split(VcardConstants._argumentDelimiter);

// Check the provided address
string[] splitAddressValues = splitAdr[0].Split(VcardConstants._fieldDelimiter);
if (splitAddressValues.Length < 7)
throw new InvalidDataException("Address information must specify exactly seven values (P.O. Box, extended address, street address, locality, region, postal code, and country)");

// Populate the fields
return InstallInfo([], splitAddressValues, altId, cardVersion);
}
bool altIdSupported = cardVersion.Major >= 4;

internal override BaseCardPartInfo FromStringVcardWithTypeInternal(string value, string[] finalArgs, int altId, Version cardVersion)
{
// Get the value
string adrValue = value.Substring(VcardConstants._addressSpecifier.Length + 1);
string[] splitAdr = adrValue.Split(VcardConstants._argumentDelimiter);
if (splitAdr.Length < 2)
throw new InvalidDataException("Address field must specify exactly two values (Type (optionally prepended with TYPE=), and address information)");
string[] splitAdr = value.Split(VcardConstants._argumentDelimiter);

// Check the provided address
string[] splitAddressValues = splitAdr[1].Split(VcardConstants._fieldDelimiter);
if (splitAddressValues.Length < 7)
if (splitAdr.Length < 7)
throw new InvalidDataException("Address information must specify exactly seven values (P.O. Box, extended address, street address, locality, region, postal code, and country)");

// Populate the fields
return InstallInfo(splitAdr, splitAddressValues, finalArgs, altId, cardVersion);
}

private AddressInfo InstallInfo(string[] splitAdr, string[] splitAddressValues, int altId, Version cardVersion) =>
InstallInfo(splitAdr, splitAddressValues, [], altId, cardVersion);

private AddressInfo InstallInfo(string[] splitAdr, string[] splitAddressValues, string[] finalArgs, int altId, Version cardVersion)
{
bool altIdSupported = cardVersion.Major >= 4;
bool specifierRequired = cardVersion.Major >= 3;

// Populate the fields
string[] _addressTypes = splitAdr.Length == 0 ? ["HOME"] : VcardParserTools.GetTypes(splitAdr, "HOME", specifierRequired);
string _addressPOBox = Regex.Unescape(splitAddressValues[0]);
string _addressExtended = Regex.Unescape(splitAddressValues[1]);
string _addressStreet = Regex.Unescape(splitAddressValues[2]);
string _addressLocality = Regex.Unescape(splitAddressValues[3]);
string _addressRegion = Regex.Unescape(splitAddressValues[4]);
string _addressPostalCode = Regex.Unescape(splitAddressValues[5]);
string _addressCountry = Regex.Unescape(splitAddressValues[6]);
AddressInfo _address = new(altIdSupported ? altId : 0, altIdSupported ? finalArgs : [], _addressTypes, _addressPOBox, _addressExtended, _addressStreet, _addressLocality, _addressRegion, _addressPostalCode, _addressCountry);
string[] _addressTypes = elementTypes.Length >= 0 ? elementTypes : ["HOME"];
string _addressPOBox = Regex.Unescape(splitAdr[0]);
string _addressExtended = Regex.Unescape(splitAdr[1]);
string _addressStreet = Regex.Unescape(splitAdr[2]);
string _addressLocality = Regex.Unescape(splitAdr[3]);
string _addressRegion = Regex.Unescape(splitAdr[4]);
string _addressPostalCode = Regex.Unescape(splitAdr[5]);
string _addressCountry = Regex.Unescape(splitAdr[6]);
AddressInfo _address = new(altIdSupported ? altId : 0, finalArgs, _addressTypes, valueType, _addressPOBox, _addressExtended, _addressStreet, _addressLocality, _addressRegion, _addressPostalCode, _addressCountry);
return _address;
}

Expand Down Expand Up @@ -184,9 +147,7 @@ public bool Equals(AddressInfo source, AddressInfo target)

// Check all the properties
return
source.AddressTypes.SequenceEqual(target.AddressTypes) &&
source.AltArguments.SequenceEqual(target.AltArguments) &&
source.AltId == target.AltId &&
base.Equals(source, target) &&
source.PostOfficeBox == target.PostOfficeBox &&
source.ExtendedAddress == target.ExtendedAddress &&
source.StreetAddress == target.StreetAddress &&
Expand All @@ -200,10 +161,8 @@ public bool Equals(AddressInfo source, AddressInfo target)
/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = -1858114484;
hashCode = hashCode * -1521134295 + AltId.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(AltArguments);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(AddressTypes);
int hashCode = -427937047;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(PostOfficeBox);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ExtendedAddress);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(StreetAddress);
Expand All @@ -224,11 +183,12 @@ public override int GetHashCode()

internal AddressInfo() { }

internal AddressInfo(int altId, string[] altArguments, string[] addressTypes, string postOfficeBox, string extendedAddress, string streetAddress, string locality, string region, string postalCode, string country)
internal AddressInfo(int altId, string[] arguments, string[] elementTypes, string valueType, string postOfficeBox, string extendedAddress, string streetAddress, string locality, string region, string postalCode, string country)
{
AltId = altId;
AltArguments = altArguments;
AddressTypes = addressTypes;
Arguments = arguments;
ElementTypes = elementTypes;
ValueType = valueType;
PostOfficeBox = postOfficeBox;
ExtendedAddress = extendedAddress;
StreetAddress = streetAddress;
Expand Down
62 changes: 14 additions & 48 deletions VisualCard/Parts/Implementations/AgentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public class AgentInfo : BaseCardPartInfo, IEquatable<AgentInfo>
/// </summary>
public Card[] AgentCards { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, int altId, Version cardVersion) =>
new AgentInfo().FromStringVcardInternal(value, altId, cardVersion);

internal static BaseCardPartInfo FromStringVcardWithTypeStatic(string value, string[] finalArgs, int altId, Version cardVersion) =>
new AgentInfo().FromStringVcardWithTypeInternal(value, finalArgs, altId, cardVersion);
internal static BaseCardPartInfo FromStringVcardStatic(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion) =>
new AgentInfo().FromStringVcardInternal(value, finalArgs, altId, elementTypes, valueType, cardVersion);

internal override string ToStringVcardInternal(Version cardVersion)
{
Expand All @@ -55,7 +52,7 @@ internal override string ToStringVcardInternal(Version cardVersion)
{
if (altIdSupported)
{
bool installAltId = AltId >= 0 && AltArguments.Length > 0;
bool installAltId = AltId >= 0 && Arguments.Length > 0;
agents.Append(
$"{VcardConstants._agentSpecifier}" +
$"{(installAltId ? $"{VcardConstants._fieldDelimiter}{VcardConstants._altIdArgumentSpecifier}" + AltId : "")}" +
Expand All @@ -74,49 +71,18 @@ internal override string ToStringVcardInternal(Version cardVersion)
return agents.ToString();
}

internal override BaseCardPartInfo FromStringVcardInternal(string value, int altId, Version cardVersion)
{
// Get the value
string agentValue = value.Substring(VcardConstants._agentSpecifier.Length + 1);
string[] splitAgent = agentValue.Split(VcardConstants._argumentDelimiter);

// Check the provided agent
string[] splitAgentValues = splitAgent[0].Split(VcardConstants._fieldDelimiter);
if (splitAgentValues.Length < 1)
throw new InvalidDataException("Agent information must specify exactly one value (agent vCard contents that have their lines delimited by \\n)");

// Populate the fields
return InstallInfo(agentValue, altId, cardVersion);
}

internal override BaseCardPartInfo FromStringVcardWithTypeInternal(string value, string[] finalArgs, int altId, Version cardVersion)
internal override BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion)
{
// Get the value
string agentValue = value.Substring(VcardConstants._agentSpecifier.Length + 1);
string[] splitAgent = agentValue.Split(VcardConstants._argumentDelimiter);
if (splitAgent.Length < 2)
throw new InvalidDataException("Agent field must specify exactly two values (Type (optionally prepended with TYPE=), and agent information)");
bool altIdSupported = cardVersion.Major >= 4;

// Check the provided agent
string[] splitAgentValues = splitAgent[1].Split(VcardConstants._fieldDelimiter);
if (splitAgentValues.Length < 1)
if (string.IsNullOrEmpty(value))
throw new InvalidDataException("Agent information must specify exactly one value (agent vCard contents that have their lines delimited by \\n)");

// Populate the fields
return InstallInfo(agentValue, finalArgs, altId, cardVersion);
}

private AgentInfo InstallInfo(string value, int altId, Version cardVersion) =>
InstallInfo(value, [], altId, cardVersion);

private AgentInfo InstallInfo(string value, string[] finalArgs, int altId, Version cardVersion)
{
bool altIdSupported = cardVersion.Major >= 4;

// Populate the fields
string _agentVcard = Regex.Unescape(value).Replace("\\n", "\n");
var _agentVcardParsers = CardTools.GetCardsFromString(_agentVcard);
AgentInfo _agent = new(altIdSupported ? altId : 0, altIdSupported ? finalArgs : [], _agentVcardParsers);
AgentInfo _agent = new(altIdSupported ? altId : 0, finalArgs, elementTypes, valueType, _agentVcardParsers);
return _agent;
}

Expand Down Expand Up @@ -146,18 +112,16 @@ public bool Equals(AgentInfo source, AgentInfo target)

// Check all the properties
return
source.AltArguments.SequenceEqual(target.AltArguments) &&
source.AltId == target.AltId &&
base.Equals(source, target) &&
source.AgentCards == target.AgentCards
;
}

/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = -1716393954;
hashCode = hashCode * -1521134295 + AltId.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(AltArguments);
int hashCode = -582546693;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<Card[]>.Default.GetHashCode(AgentCards);
return hashCode;
}
Expand All @@ -172,10 +136,12 @@ public override int GetHashCode()

internal AgentInfo() { }

internal AgentInfo(int altId, string[] altArguments, Card[] agentCard)
internal AgentInfo(int altId, string[] arguments, string[] elementTypes, string valueType, Card[] agentCard)
{
AltId = altId;
AltArguments = altArguments;
Arguments = arguments;
ElementTypes = elementTypes;
ValueType = valueType;
AgentCards = agentCard;
}
}
Expand Down
Loading

0 comments on commit e255c12

Please sign in to comment.