diff --git a/README.md b/README.md index e241b9e..b8bde23 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,23 @@ if (parseResult.Card.FirstName.HasError) } ``` +You can iterate through all errors that occurred: + +```csharp +if (parseResult.Errors.Count > 0) +{ + foreach (var error in parseResult.Errors) + { + // All errors have a Message describing what went wrong. + Console.WriteLine($"Error: {error.Message}"); + + // Element-level errors will have an Element ID and the element's raw value. + Console.WriteLine($"Element ID: {error.ElementId}"); + Console.WriteLine($"Raw Value: {error.RawValue}"); + } +} +``` + You can also check to see whether a field was present in the scanned ID text: ```csharp diff --git a/src/IdParser.Core/Barcode.cs b/src/IdParser.Core/Barcode.cs index 4927897..cfb4ac6 100644 --- a/src/IdParser.Core/Barcode.cs +++ b/src/IdParser.Core/Barcode.cs @@ -8,53 +8,6 @@ namespace IdParser.Core; -/// -/// Any element with an unrecognized 3-character element ID and its associated value. -/// -/// The 3-character element ID. -/// The raw value from the scanned ID text. -public record UnhandledElement(string ElementId, string? RawValue); - -/// -/// Details about failure to parse or extract a meaningful value from an element's raw value obtained from -/// the scanned ID text. -/// -/// The 3-character element ID. -/// The element's raw value from the scanned ID text. -/// A message describing the error that occurred. -public record ElementParseError(string ElementId, string? RawValue, string Error); - -/// -/// Contains the result of parsing a scanned ID: the ID card, a collection of unhandled fields, and any -/// field-level parsing errors that occurred. -/// -public class BarcodeParseResult -{ - /// - /// Contains values of any elements extracted from the scanned ID text. - /// - public IdentificationCard Card { get; } - - public IReadOnlyCollection UnhandledElements { get; } - - /// - /// Contains element-level errors that occurred while trying to extract a meaningful value from the element's raw value. - /// - public IReadOnlyCollection ElementParseErrors { get; } - - - public BarcodeParseResult(IdentificationCard card, IReadOnlyCollection unhandledElements, IReadOnlyCollection elementParseErrors) - { - ArgumentNullException.ThrowIfNull(card); - ArgumentNullException.ThrowIfNull(unhandledElements); - ArgumentNullException.ThrowIfNull(elementParseErrors); - - Card = card; - UnhandledElements = unhandledElements; - ElementParseErrors = elementParseErrors; - } -} - public static partial class Barcode { /// @@ -138,7 +91,7 @@ public static BarcodeParseResult Parse(string rawPdf417Input, Validation validat logger.UnhandledElementIds(string.Join(", ", populateResult.UnhandledElements.Select(ue => ue.ElementId))); } - return new BarcodeParseResult(idCard, populateResult.UnhandledElements, populateResult.ElementErrors); + return new BarcodeParseResult(idCard, populateResult.UnhandledElements, populateResult.Errors); } @@ -442,12 +395,12 @@ private static ParseCountryResult ParseCountry(IssuerIdentificationNumber iin, A } - private readonly record struct PopulateResult(List UnhandledElements, List ElementErrors); + private readonly record struct PopulateResult(List UnhandledElements, List Errors); private static PopulateResult PopulateIdCard(IdentificationCard idCard, AAMVAVersion version, Country country, Dictionary subfileRecords, ILogger logger) { List unhandledElements = new(); - List elementErrors = new(); + List errors = new(); foreach (var elementId in subfileRecords.Keys) { @@ -464,7 +417,7 @@ private static PopulateResult PopulateIdCard(IdentificationCard idCard, AAMVAVer var parseAndSetResult = Parser.ParseAndSetIdCardElement(elementId: elementId, rawValue: rawValue, country, version, idCard); if (parseAndSetResult.ElementHandled) { - AddErrorIfParseAndSetFailed(parseAndSetResult, elementErrors); + AddErrorIfParseAndSetFailed(parseAndSetResult, errors); // Element handled. No need for further processing. continue; @@ -475,7 +428,7 @@ private static PopulateResult PopulateIdCard(IdentificationCard idCard, AAMVAVer parseAndSetResult = Parser.ParseAndSetDriversLicenseElement(elementId: elementId, rawValue: rawValue, country, version, driversLicense); if (parseAndSetResult.ElementHandled) { - AddErrorIfParseAndSetFailed(parseAndSetResult, elementErrors); + AddErrorIfParseAndSetFailed(parseAndSetResult, errors); // Element handled. No need for further processing. continue; @@ -495,16 +448,16 @@ private static PopulateResult PopulateIdCard(IdentificationCard idCard, AAMVAVer } } - return new PopulateResult(unhandledElements, elementErrors); + return new PopulateResult(unhandledElements, errors); } - private static void AddErrorIfParseAndSetFailed(Parser.ParseAndSetElementResult parseAndSetResult, List elementErrors) + private static void AddErrorIfParseAndSetFailed(Parser.ParseAndSetElementResult parseAndSetResult, List errors) { if (!parseAndSetResult.HasError) { return; } - elementErrors.Add(parseAndSetResult.ElementParseError); + errors.Add(parseAndSetResult.Error); } } diff --git a/src/IdParser.Core/BarcodeParseResult.cs b/src/IdParser.Core/BarcodeParseResult.cs new file mode 100644 index 0000000..af9f3e9 --- /dev/null +++ b/src/IdParser.Core/BarcodeParseResult.cs @@ -0,0 +1,35 @@ +namespace IdParser.Core; + +/// +/// Contains the result of parsing a scanned ID: the ID card, a collection of unhandled fields, and any +/// field-level parsing errors that occurred. +/// +public class BarcodeParseResult +{ + /// + /// Contains values of any elements extracted from the scanned ID text. + /// + public IdentificationCard Card { get; } + + /// + /// Contains any unhandled element IDs and their raw values. + /// + public IReadOnlyCollection UnhandledElements { get; } + + /// + /// Contains errors that occurred while trying to parse the scanned ID text. + /// + public IReadOnlyCollection Errors { get; } + + + internal BarcodeParseResult(IdentificationCard card, IReadOnlyCollection unhandledElements, IReadOnlyCollection errors) + { + ArgumentNullException.ThrowIfNull(card); + ArgumentNullException.ThrowIfNull(unhandledElements); + ArgumentNullException.ThrowIfNull(errors); + + Card = card; + UnhandledElements = unhandledElements; + Errors = errors; + } +} diff --git a/src/IdParser.Core/IdParser.Core.csproj b/src/IdParser.Core/IdParser.Core.csproj index 3d12a7c..7c5da3a 100644 --- a/src/IdParser.Core/IdParser.Core.csproj +++ b/src/IdParser.Core/IdParser.Core.csproj @@ -6,7 +6,7 @@ enable - 3.0.0-alpha2 + 3.0.0-beta1 3.0.0 3.0.0 Connor O'Shea, Jon Sagara diff --git a/src/IdParser.Core/ParseError.cs b/src/IdParser.Core/ParseError.cs new file mode 100644 index 0000000..4c0cac3 --- /dev/null +++ b/src/IdParser.Core/ParseError.cs @@ -0,0 +1,15 @@ +namespace IdParser.Core; + +/// +/// Details about failure to parse or extract a meaningful value from an element's raw value obtained from +/// the scanned ID text. +/// +/// +/// NOTE: Name ParseError instead of Error because otherwise the compiler complains: CA1716: Rename type +/// Error so that it no longer conflicts with the reserved language keyword 'Error'. Using a reserved keyword +/// as the name of a type makes it harder for consumers in other languages to use the type. +/// +/// A message describing the error that occurred. +/// If element-specific, the 3-character element ID; otherwise, null. +/// If element-specific, the element's raw value from the scanned ID text; otherwise, null. +public record ParseError(string Message, string? ElementId, string? RawValue); diff --git a/src/IdParser.Core/Parsers/Parser.cs b/src/IdParser.Core/Parsers/Parser.cs index 8f77f28..3465e02 100644 --- a/src/IdParser.Core/Parsers/Parser.cs +++ b/src/IdParser.Core/Parsers/Parser.cs @@ -11,11 +11,11 @@ internal readonly record struct ParseAndSetElementResult { internal required bool ElementHandled { get; init; } - internal required ElementParseError? ElementParseError { get; init; } + internal required ParseError? Error { get; init; } - [MemberNotNullWhen(true, nameof(ElementParseError))] + [MemberNotNullWhen(true, nameof(Error))] internal bool HasError - => ElementParseError is not null; + => Error is not null; } @@ -34,52 +34,52 @@ internal static ParseAndSetElementResult ParseAndSetIdCardElement(string element ArgumentNullException.ThrowIfNull(idCard); var elementHandled = true; - ElementParseError? elementParseError = null; + ParseError? error = null; switch (elementId) { case SubfileElementIds.AliasFirstName: - (idCard.AliasFirstName, elementParseError) = ParseElement(AliasFirstNameParser.Parse, elementId: elementId, rawValue: rawValue, version); + (idCard.AliasFirstName, error) = ParseElement(AliasFirstNameParser.Parse, elementId: elementId, rawValue: rawValue, version); break; case SubfileElementIds.AliasLastName: - (idCard.AliasLastName, elementParseError) = ParseElement(AliasLastNameParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.AliasLastName, error) = ParseElement(AliasLastNameParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.AliasSuffix: - (idCard.AliasSuffix, elementParseError) = ParseElement(AliasSuffixParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.AliasSuffix, error) = ParseElement(AliasSuffixParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.AuditInformation: - (idCard.AuditInformation, elementParseError) = ParseElement(AuditInformationParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.AuditInformation, error) = ParseElement(AuditInformationParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.City: - (idCard.City, elementParseError) = ParseElement(CityParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.City, error) = ParseElement(CityParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.ComplianceType: - (idCard.ComplianceType, elementParseError) = ParseElement(ComplianceTypeParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.ComplianceType, error) = ParseElement(ComplianceTypeParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.DateOfBirth: - (idCard.DateOfBirth, elementParseError) = ParseElement(DateOfBirthParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.DateOfBirth, error) = ParseElement(DateOfBirthParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.DocumentDiscriminator: - (idCard.DocumentDiscriminator, elementParseError) = ParseElement(DocumentDiscriminatorParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.DocumentDiscriminator, error) = ParseElement(DocumentDiscriminatorParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.Ethnicity: - (idCard.Ethnicity, elementParseError) = ParseElement(EthnicityParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.Ethnicity, error) = ParseElement(EthnicityParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.ExpirationDate: - (idCard.ExpirationDate, elementParseError) = ParseElement(ExpirationDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.ExpirationDate, error) = ParseElement(ExpirationDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.EyeColor: - (idCard.EyeColor, elementParseError) = ParseElement(EyeColorParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.EyeColor, error) = ParseElement(EyeColorParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.FirstName: @@ -99,53 +99,53 @@ internal static ParseAndSetElementResult ParseAndSetIdCardElement(string element break; case SubfileElementIds.HairColor: - (idCard.HairColor, elementParseError) = ParseElement(HairColorParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.HairColor, error) = ParseElement(HairColorParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.HasTemporaryLawfulStatus: - (idCard.HasTemporaryLawfulStatus, elementParseError) = ParseElement(HasTemporaryLawfulStatusParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.HasTemporaryLawfulStatus, error) = ParseElement(HasTemporaryLawfulStatusParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.Height: - (idCard.Height, elementParseError) = ParseElement(HeightParser.Parse, elementId: elementId, rawValue: rawValue, version); + (idCard.Height, error) = ParseElement(HeightParser.Parse, elementId: elementId, rawValue: rawValue, version); break; case SubfileElementIds.IdNumber: - (idCard.IdNumber, elementParseError) = ParseElement(IdNumberParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.IdNumber, error) = ParseElement(IdNumberParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.InventoryControlNumber: - (idCard.InventoryControlNumber, elementParseError) = ParseElement(InventoryControlNumberParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.InventoryControlNumber, error) = ParseElement(InventoryControlNumberParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.IsOrganDonor: - (idCard.IsOrganDonor, elementParseError) = ParseElement(IsOrganDonorParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.IsOrganDonor, error) = ParseElement(IsOrganDonorParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.IsOrganDonorLegacy: - (idCard.IsOrganDonor, elementParseError) = ParseElement(IsOrganDonorLegacyParser.Parse, elementId: elementId, rawValue: rawValue, version); + (idCard.IsOrganDonor, error) = ParseElement(IsOrganDonorLegacyParser.Parse, elementId: elementId, rawValue: rawValue, version); break; case SubfileElementIds.IssueDate: - (idCard.IssueDate, elementParseError) = ParseElement(IssueDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.IssueDate, error) = ParseElement(IssueDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.IsVeteran: - (idCard.IsVeteran, elementParseError) = ParseElement(IsVeteranParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.IsVeteran, error) = ParseElement(IsVeteranParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.JurisdictionCode: - (idCard.JurisdictionCode, elementParseError) = ParseElement(JurisdictionCodeParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.JurisdictionCode, error) = ParseElement(JurisdictionCodeParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.LastName: - (idCard.LastName, elementParseError) = ParseElement(LastNameParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.LastName, error) = ParseElement(LastNameParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.MiddleName: // Some jurisdictions like Wyoming put the middle initial in the FirstName field. If we have // already written that, and if middle name is null, keep the one parsed from first name. - (idCard.MiddleName, elementParseError) = ParseElement(MiddleNameParser.Parse, elementId: elementId, rawValue: rawValue ?? idCard.MiddleName.Value); + (idCard.MiddleName, error) = ParseElement(MiddleNameParser.Parse, elementId: elementId, rawValue: rawValue ?? idCard.MiddleName.Value); break; case SubfileElementIds.Name: @@ -158,71 +158,71 @@ internal static ParseAndSetElementResult ParseAndSetIdCardElement(string element break; case SubfileElementIds.NameSuffix: - (idCard.Suffix, elementParseError) = ParseElement(NameSuffixParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.Suffix, error) = ParseElement(NameSuffixParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.PlaceOfBirth: - (idCard.PlaceOfBirth, elementParseError) = ParseElement(PlaceOfBirthParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.PlaceOfBirth, error) = ParseElement(PlaceOfBirthParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.PostalCode: - (idCard.PostalCode, elementParseError) = ParseElement(PostalCodeParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.PostalCode, error) = ParseElement(PostalCodeParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.RevisionDate: - (idCard.RevisionDate, elementParseError) = ParseElement(RevisionDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.RevisionDate, error) = ParseElement(RevisionDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.Sex: - (idCard.Sex, elementParseError) = ParseElement(SexParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.Sex, error) = ParseElement(SexParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.StreetLine1: - (idCard.StreetLine1, elementParseError) = ParseElement(StreetLine1Parser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.StreetLine1, error) = ParseElement(StreetLine1Parser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.StreetLine1Legacy: - (idCard.StreetLine1, elementParseError) = ParseElement(StreetLine1LegacyParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.StreetLine1, error) = ParseElement(StreetLine1LegacyParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.StreetLine2: - (idCard.StreetLine2, elementParseError) = ParseElement(StreetLine2Parser.Parse, elementId: elementId, rawValue: rawValue, city: idCard.City.Value, jurisdictionCode: idCard.JurisdictionCode.Value, postalCode: idCard.PostalCode.Value); + (idCard.StreetLine2, error) = ParseElement(StreetLine2Parser.Parse, elementId: elementId, rawValue: rawValue, city: idCard.City.Value, jurisdictionCode: idCard.JurisdictionCode.Value, postalCode: idCard.PostalCode.Value); break; case SubfileElementIds.Under18Until: - (idCard.Under18Until, elementParseError) = ParseElement(Under18UntilParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.Under18Until, error) = ParseElement(Under18UntilParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.Under19Until: - (idCard.Under19Until, elementParseError) = ParseElement(Under19UntilParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.Under19Until, error) = ParseElement(Under19UntilParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.Under21Until: - (idCard.Under21Until, elementParseError) = ParseElement(Under21UntilParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (idCard.Under21Until, error) = ParseElement(Under21UntilParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.WasFirstNameTruncated: - (idCard.WasFirstNameTruncated, elementParseError) = ParseElement(WasFirstNameTruncatedParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.WasFirstNameTruncated, error) = ParseElement(WasFirstNameTruncatedParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.WasLastNameTruncated: - (idCard.WasLastNameTruncated, elementParseError) = ParseElement(WasLastNameTruncatedParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.WasLastNameTruncated, error) = ParseElement(WasLastNameTruncatedParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.WasMiddleNameTruncated: - (idCard.WasMiddleNameTruncated, elementParseError) = ParseElement(WasMiddleNameTruncatedParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.WasMiddleNameTruncated, error) = ParseElement(WasMiddleNameTruncatedParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.WeightInKilograms: - (idCard.Weight, elementParseError) = ParseElement(WeightInKilogramsParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.Weight, error) = ParseElement(WeightInKilogramsParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.WeightInPounds: - (idCard.Weight, elementParseError) = ParseElement(WeightInPoundsParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.Weight, error) = ParseElement(WeightInPoundsParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.WeightRange: - (idCard.WeightRange, elementParseError) = ParseElement(WeightRangeParser.Parse, elementId: elementId, rawValue: rawValue); + (idCard.WeightRange, error) = ParseElement(WeightRangeParser.Parse, elementId: elementId, rawValue: rawValue); break; default: @@ -233,7 +233,7 @@ internal static ParseAndSetElementResult ParseAndSetIdCardElement(string element return new ParseAndSetElementResult { ElementHandled = elementHandled, - ElementParseError = elementParseError, + Error = error, }; } @@ -252,60 +252,60 @@ internal static ParseAndSetElementResult ParseAndSetDriversLicenseElement(string ArgumentNullException.ThrowIfNull(driversLicense); var elementHandled = true; - ElementParseError? elementParseError = null; + ParseError? error = null; switch (elementId) { case SubfileElementIds.EndorsementCodeDescription: - (driversLicense.EndorsementCodeDescription, elementParseError) = ParseElement(EndorsementCodeDescriptionParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.EndorsementCodeDescription, error) = ParseElement(EndorsementCodeDescriptionParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.EndorsementCodes: - (driversLicense.EndorsementCodes, elementParseError) = ParseElement(EndorsementCodesParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.EndorsementCodes, error) = ParseElement(EndorsementCodesParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.EndorsementCodesLegacy: - (driversLicense.EndorsementCodes, elementParseError) = ParseElement(EndorsementCodesLegacyParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.EndorsementCodes, error) = ParseElement(EndorsementCodesLegacyParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.HazmatEndorsementExpirationDate: - (driversLicense.HazmatEndorsementExpirationDate, elementParseError) = ParseElement(HazmatEndorsementExpirationDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); + (driversLicense.HazmatEndorsementExpirationDate, error) = ParseElement(HazmatEndorsementExpirationDateParser.Parse, elementId: elementId, rawValue: rawValue, country, version); break; case SubfileElementIds.RestrictionCodeDescription: - (driversLicense.RestrictionCodeDescription, elementParseError) = ParseElement(RestrictionCodeDescriptionParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.RestrictionCodeDescription, error) = ParseElement(RestrictionCodeDescriptionParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.RestrictionCodes: - (driversLicense.RestrictionCodes, elementParseError) = ParseElement(RestrictionCodesParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.RestrictionCodes, error) = ParseElement(RestrictionCodesParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.RestrictionCodesLegacy: - (driversLicense.RestrictionCodes, elementParseError) = ParseElement(RestrictionCodesLegacyParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.RestrictionCodes, error) = ParseElement(RestrictionCodesLegacyParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.StandardEndorsementCode: - (driversLicense.StandardEndorsementCode, elementParseError) = ParseElement(StandardEndorsementCodeParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.StandardEndorsementCode, error) = ParseElement(StandardEndorsementCodeParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.StandardRestrictionCode: - (driversLicense.StandardRestrictionCode, elementParseError) = ParseElement(StandardRestrictionCodeParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.StandardRestrictionCode, error) = ParseElement(StandardRestrictionCodeParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.StandardVehicleClassification: - (driversLicense.StandardVehicleClassification, elementParseError) = ParseElement(StandardVehicleClassificationParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.StandardVehicleClassification, error) = ParseElement(StandardVehicleClassificationParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.VehicleClassificationDescription: - (driversLicense.VehicleClassificationDescription, elementParseError) = ParseElement(VehicleClassificationDescriptionParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.VehicleClassificationDescription, error) = ParseElement(VehicleClassificationDescriptionParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.VehicleClass: - (driversLicense.VehicleClass, elementParseError) = ParseElement(VehicleClassParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.VehicleClass, error) = ParseElement(VehicleClassParser.Parse, elementId: elementId, rawValue: rawValue); break; case SubfileElementIds.VehicleClassLegacy: - (driversLicense.VehicleClass, elementParseError) = ParseElement(VehicleClassLegacyParser.Parse, elementId: elementId, rawValue: rawValue); + (driversLicense.VehicleClass, error) = ParseElement(VehicleClassLegacyParser.Parse, elementId: elementId, rawValue: rawValue); break; default: @@ -316,7 +316,7 @@ internal static ParseAndSetElementResult ParseAndSetDriversLicenseElement(string return new ParseAndSetElementResult { ElementHandled = elementHandled, - ElementParseError = elementParseError, + Error = error, }; } @@ -325,7 +325,7 @@ internal static ParseAndSetElementResult ParseAndSetDriversLicenseElement(string // Private methods // - private readonly record struct ParseElementResult(Field Field, ElementParseError? ElementParseError); + private readonly record struct ParseElementResult(Field Field, ParseError? Error); private delegate Field ParseFunction(string elementId, string? rawValue); @@ -343,7 +343,7 @@ private static ParseElementResult ParseElement(ParseFunction parseFunc, var field = parseFunc(elementId: elementId, rawValue: rawValue); var parseError = field.HasError - ? new ElementParseError(elementId, rawValue, field.Error!) + ? new ParseError(Message: field.Error!, ElementId: elementId, RawValue: rawValue) : null; return new ParseElementResult(field, parseError); @@ -356,7 +356,7 @@ private static ParseElementResult ParseElement(ParseWithVersionFunction var field = parseFunc(elementId: elementId, rawValue: rawValue, version); var parseError = field.HasError - ? new ElementParseError(elementId, rawValue, field.Error!) + ? new ParseError(Message: field.Error!, ElementId: elementId, RawValue: rawValue) : null; return new ParseElementResult(field, parseError); @@ -369,7 +369,7 @@ private static ParseElementResult ParseElement(ParseWithCountryAndVersionF var field = parseFunc(elementId: elementId, rawValue: rawValue, country, version); var parseError = field.HasError - ? new ElementParseError(elementId, rawValue, field.Error!) + ? new ParseError(Message: field.Error!, ElementId: elementId, RawValue: rawValue) : null; return new ParseElementResult(field, parseError); @@ -382,7 +382,7 @@ private static ParseElementResult ParseElement(ParseStreetAddressFunction< var field = parseFunc(elementId: elementId, rawValue: rawValue, city: city, jurisdictionCode: jurisdictionCode, postalCode: postalCode); var parseError = field.HasError - ? new ElementParseError(elementId, rawValue, field.Error!) + ? new ParseError(Message: field.Error!, ElementId: elementId, RawValue: rawValue) : null; return new ParseElementResult(field, parseError); diff --git a/src/IdParser.Core/UnhandledElement.cs b/src/IdParser.Core/UnhandledElement.cs new file mode 100644 index 0000000..b111670 --- /dev/null +++ b/src/IdParser.Core/UnhandledElement.cs @@ -0,0 +1,8 @@ +namespace IdParser.Core; + +/// +/// Any element with an unrecognized 3-character element ID and its associated value. +/// +/// The 3-character element ID. +/// The raw value from the scanned ID text. +public record UnhandledElement(string ElementId, string? RawValue);