diff --git a/VisualCard.ShowContacts/Program.cs b/VisualCard.ShowContacts/Program.cs index 56bada9..6728b02 100644 --- a/VisualCard.ShowContacts/Program.cs +++ b/VisualCard.ShowContacts/Program.cs @@ -87,10 +87,10 @@ static void Main(string[] args) { TextWriterColor.WriteColor("----------------------------", ConsoleColors.Green); TextWriterColor.WriteColor("Name: {0}", ConsoleColors.Green, Contact.GetString(StringsEnum.FullName)); - TextWriterColor.WriteColor("Revision: {0}", ConsoleColors.Green, Contact.GetPart(PartsEnum.Revision)); + TextWriterColor.WriteColor("Revision: {0}", ConsoleColors.Green, Contact.GetPart()); // List names - foreach (NameInfo name in Contact.GetPartsArray(PartsArrayEnum.Names)) + foreach (var name in Contact.GetPartsArray()) { TextWriterColor.Write("First name: {0}", name.ContactFirstName); TextWriterColor.Write("Last name: {0}", name.ContactLastName); @@ -98,14 +98,14 @@ static void Main(string[] args) } // List titles - foreach (TitleInfo title in Contact.GetPartsArray(PartsArrayEnum.Titles)) + foreach (var title in Contact.GetPartsArray()) { TextWriterColor.Write("Title or Job: {0}", title.ContactTitle); TextWriterColor.Write("ALTID: {0}", title.AltId); } // List addresses - foreach (AddressInfo Address in Contact.GetPartsArray(PartsArrayEnum.Addresses)) + foreach (var Address in Contact.GetPartsArray()) { TextWriterColor.Write("P.O. Box: {0}", Address.PostOfficeBox); TextWriterColor.Write("Extended Address: {0}", Address.ExtendedAddress); @@ -117,13 +117,13 @@ static void Main(string[] args) } // List e-mails - foreach (EmailInfo Email in Contact.GetPartsArray(PartsArrayEnum.Mails)) + foreach (var Email in Contact.GetPartsArray()) { TextWriterColor.Write("Email address: {0}", Email.ContactEmailAddress); } // List organizations - foreach (OrganizationInfo Organization in Contact.GetPartsArray(PartsArrayEnum.Organizations)) + foreach (var Organization in Contact.GetPartsArray()) { TextWriterColor.Write("Organization Name: {0}", Organization.Name); TextWriterColor.Write("Organization Unit: {0}", Organization.Unit); @@ -131,13 +131,13 @@ static void Main(string[] args) } // List telephones - foreach (TelephoneInfo Telephone in Contact.GetPartsArray(PartsArrayEnum.Telephones)) + foreach (var Telephone in Contact.GetPartsArray()) { TextWriterColor.Write("Phone number: {0}", Telephone.ContactPhoneNumber); } // List photos - foreach (PhotoInfo Photo in Contact.GetPartsArray(PartsArrayEnum.Photos)) + foreach (var Photo in Contact.GetPartsArray()) { TextWriterColor.Write("Photo encoding: {0}", Photo.Encoding); TextWriterColor.Write("Photo value type: {0}", Photo.ValueType); @@ -146,21 +146,21 @@ static void Main(string[] args) } // List roles - foreach (RoleInfo Role in Contact.GetPartsArray(PartsArrayEnum.Roles)) + foreach (var Role in Contact.GetPartsArray()) { TextWriterColor.Write("Role: {0}", Role.ContactRole); TextWriterColor.Write("ALTID: {0}", Role.AltId); } // List remaining - var birth = Contact.GetPart(PartsEnum.Birthdate); - var wed = Contact.GetPart(PartsEnum.Anniversary); - var gnd = Contact.GetPart(PartsEnum.Gender); - if (birth is BirthDateInfo bday) - TextWriterColor.Write("Contact birthdate: {0}", bday.BirthDate); - if (wed is AnniversaryInfo adate) - TextWriterColor.Write("Contact wedding date: {0}", adate.Anniversary); - if (gnd is GenderInfo gender) + var birth = Contact.GetPart(); + var wedding = Contact.GetPart(); + var gender = Contact.GetPart(); + if (birth is not null) + TextWriterColor.Write("Contact birthdate: {0}", birth.BirthDate); + if (wedding is not null) + TextWriterColor.Write("Contact wedding date: {0}", wedding.Anniversary); + if (gender is not null) TextWriterColor.Write("Contact gender {0} [{1}]", gender.Gender.ToString(), gender.GenderDescription); TextWriterColor.Write("Contact mailer: {0}", Contact.GetString(StringsEnum.Mailer)); TextWriterColor.Write("Contact URL: {0}", Contact.GetString(StringsEnum.Url)); diff --git a/VisualCard/Parsers/VcardParser.cs b/VisualCard/Parsers/VcardParser.cs index 5c1eda1..88c7bed 100644 --- a/VisualCard/Parsers/VcardParser.cs +++ b/VisualCard/Parsers/VcardParser.cs @@ -253,7 +253,7 @@ internal void ValidateCard(Card card) // Requirement checks if (expectedFields.Contains(VcardConstants._nameSpecifier)) { - var names = card.GetPartsArray(PartsArrayEnum.Names); + var names = card.GetPartsArray(PartsArrayEnum.Names); bool exists = names is not null && names.Length > 0; if (exists) actualFields.Add(VcardConstants._nameSpecifier); diff --git a/VisualCard/Parsers/VcardParserTools.cs b/VisualCard/Parsers/VcardParserTools.cs index 60c9be8..20671e3 100644 --- a/VisualCard/Parsers/VcardParserTools.cs +++ b/VisualCard/Parsers/VcardParserTools.cs @@ -194,6 +194,72 @@ internal static string GetPrefixFromPartsArrayEnum(PartsArrayEnum partsArrayEnum throw new NotImplementedException($"String enumeration {partsArrayEnum} is not implemented.") }; + internal static PartsEnum GetPartsEnumFromType(Type partsType) + { + if (partsType == null) + throw new NotImplementedException("Type is not provided."); + + // Now, iterate through every type + if (partsType == typeof(RevisionInfo)) + return PartsEnum.Revision; + else if (partsType == typeof(BirthDateInfo)) + return PartsEnum.Birthdate; + else if (partsType == typeof(AnniversaryInfo)) + return PartsEnum.Anniversary; + else if (partsType == typeof(GenderInfo)) + return PartsEnum.Gender; + throw new NotImplementedException($"Type {partsType.Name} doesn't represent any part."); + } + + internal static PartsArrayEnum GetPartsArrayEnumFromType(Type partsArrayType) + { + if (partsArrayType == null) + throw new NotImplementedException("Type is not provided."); + + // Now, iterate through every type + if (partsArrayType == typeof(NameInfo)) + return PartsArrayEnum.Names; + else if (partsArrayType == typeof(TelephoneInfo)) + return PartsArrayEnum.Telephones; + else if (partsArrayType == typeof(AddressInfo)) + return PartsArrayEnum.Addresses; + else if (partsArrayType == typeof(LabelAddressInfo)) + return PartsArrayEnum.Labels; + else if (partsArrayType == typeof(AgentInfo)) + return PartsArrayEnum.Agents; + else if (partsArrayType == typeof(EmailInfo)) + return PartsArrayEnum.Mails; + else if (partsArrayType == typeof(OrganizationInfo)) + return PartsArrayEnum.Organizations; + else if (partsArrayType == typeof(TitleInfo)) + return PartsArrayEnum.Titles; + else if (partsArrayType == typeof(PhotoInfo)) + return PartsArrayEnum.Photos; + else if (partsArrayType == typeof(NicknameInfo)) + return PartsArrayEnum.Nicknames; + else if (partsArrayType == typeof(RoleInfo)) + return PartsArrayEnum.Roles; + else if (partsArrayType == typeof(LogoInfo)) + return PartsArrayEnum.Logos; + else if (partsArrayType == typeof(TimeDateZoneInfo)) + return PartsArrayEnum.TimeZone; + else if (partsArrayType == typeof(GeoInfo)) + return PartsArrayEnum.Geo; + else if (partsArrayType == typeof(SoundInfo)) + return PartsArrayEnum.Sounds; + else if (partsArrayType == typeof(ImppInfo)) + return PartsArrayEnum.Impps; + else if (partsArrayType == typeof(CategoryInfo)) + return PartsArrayEnum.Categories; + else if (partsArrayType == typeof(LangInfo)) + return PartsArrayEnum.Langs; + else if (partsArrayType == typeof(XmlInfo)) + return PartsArrayEnum.Xml; + else if (partsArrayType == typeof(XNameInfo)) + return PartsArrayEnum.NonstandardNames; + throw new NotImplementedException($"Type {partsArrayType.Name} doesn't represent any part array."); + } + internal static (PartType type, object enumeration, Type enumType, Func fromStringFunc, string defaultType, string defaultValue) GetPartType(string prefix) => prefix switch { diff --git a/VisualCard/Parts/Card.cs b/VisualCard/Parts/Card.cs index a1a50f1..0da826c 100644 --- a/VisualCard/Parts/Card.cs +++ b/VisualCard/Parts/Card.cs @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Text; using Textify.General; using VisualCard.Parsers; @@ -47,27 +48,67 @@ public class Card : IEquatable public Version CardVersion => version; + /// + /// Gets a part array from a specified key + /// + /// An array of values or an empty part array [] + public TPart[] GetPartsArray() where TPart : BaseCardPartInfo + { + // Get the parts enumeration according to the type + var key = VcardParserTools.GetPartsArrayEnumFromType(typeof(TPart)); + + // Now, return the value + return GetPartsArray(key); + } + /// /// Gets a part array from a specified key /// /// A key to use /// An array of values or an empty part array [] - public BaseCardPartInfo[] GetPartsArray(PartsArrayEnum key) + public TPart[] GetPartsArray(PartsArrayEnum key) where TPart : BaseCardPartInfo { // Check for version support if (!VcardParserTools.EnumArrayTypeSupported(key, CardVersion)) return null; + // Get the parts enumeration according to the type + var type = typeof(TPart); + if (type != typeof(BaseCardPartInfo)) + { + // We don't need the base, but a derivative of it. Check it. + var partsArrayEnum = VcardParserTools.GetPartsArrayEnumFromType(typeof(TPart)); + if (key != partsArrayEnum) + throw new InvalidOperationException($"Parts array enumeration [{key}] is different from the expected one [{partsArrayEnum}] according to type {typeof(TPart).Name}."); + } + // Get the fallback value - BaseCardPartInfo[] fallback = []; + TPart[] fallback = []; // Check to see if the partarray has a value or not - bool hasValue = partsArray.TryGetValue(key, out List value); + bool hasValue = partsArray.ContainsKey(key); if (!hasValue) return fallback; + // Cast the values + var value = partsArray[key]; + TPart[] parts = value.Cast().ToArray(); + // Now, return the value - return [.. value]; + return parts; + } + + /// + /// Gets a part from a specified key + /// + /// A value or an empty part if any other type doesn't exist + public TPart GetPart() where TPart : BaseCardPartInfo + { + // Get the parts enumeration according to the type + var key = VcardParserTools.GetPartsEnumFromType(typeof(TPart)); + + // Now, return the value + return GetPart(key); } /// @@ -75,22 +116,35 @@ public BaseCardPartInfo[] GetPartsArray(PartsArrayEnum key) /// /// A key to use /// A value or an empty part if any other type doesn't exist - public BaseCardPartInfo GetPart(PartsEnum key) + public TPart GetPart(PartsEnum key) where TPart : BaseCardPartInfo { // Check for version support if (!VcardParserTools.EnumTypeSupported(key, CardVersion)) return null; + // Get the parts enumeration according to the type + var type = typeof(TPart); + if (type != typeof(BaseCardPartInfo)) + { + // We don't need the base, but a derivative of it. Check it. + var partsEnum = VcardParserTools.GetPartsEnumFromType(typeof(TPart)); + if (key != partsEnum) + throw new InvalidOperationException($"Part enumeration [{key}] is different from the expected one [{partsEnum}] according to type {typeof(TPart).Name}."); + } + // Get the fallback value - BaseCardPartInfo fallback = default; + TPart fallback = default; // Check to see if the part has a value or not - bool hasValue = parts.TryGetValue(key, out BaseCardPartInfo value); + bool hasValue = parts.ContainsKey(key); if (!hasValue) return fallback; + // Cast the values + TPart part = (TPart)parts[key]; + // Now, return the value - return value; + return part; } /// @@ -153,7 +207,7 @@ public string SaveToString() foreach (PartsArrayEnum partsArrayEnum in partsArrayEnums) { // Get the array value - var array = GetPartsArray(partsArrayEnum); + var array = GetPartsArray(partsArrayEnum); if (array is null || array.Length == 0) continue; @@ -182,7 +236,7 @@ public string SaveToString() foreach (PartsEnum partsEnum in partsEnums) { // Get the part value - var part = GetPart(partsEnum); + var part = GetPart(partsEnum); if (part is null) continue;